PostgreSQL type system registry for the PgWire compatibility layer.
Centralizes all type mappings between PostgreSQL OIDs, SQL type names,
Centralizes all type mappings between PostgreSQL OIDs, SQL type names, Datahike value types, and wire protocol format codes.
Authoritative source: PostgreSQL 19devel src/include/catalog/pg_type.dat
Three directions of mapping:
PostgreSQL type system registry for the PgWire compatibility layer. Centralizes all type mappings between PostgreSQL OIDs, SQL type names, Centralizes all type mappings between PostgreSQL OIDs, SQL type names, Datahike value types, and wire protocol format codes. Authoritative source: PostgreSQL 19devel src/include/catalog/pg_type.dat Three directions of mapping: 1. SQL name → Datahike type (for CREATE TABLE) 2. Datahike type → PG OID (for wire protocol RowDescription) 3. PG OID → SQL name (for format_type() and information_schema) 4. SQL name → category (for CAST type classification)
Inverse of element-oid->array-oid: T[] OID → T OID.
Inverse of element-oid->array-oid: T[] OID → T OID.
(cast-array-elem-kw sql-type-name)For a SQL type name like int[], return the element-type keyword
used by PgArray :elem-type (:int8, :text, :bool, etc.). Returns
nil for non-array target types.
For a SQL type name like `int[]`, return the element-type keyword used by PgArray :elem-type (:int8, :text, :bool, etc.). Returns nil for non-array target types.
SQL type names that cast to a PG bit string — emitted as a string of
ASCII '0'/'1' characters in PG's text format. pgjdbc's testgetBadBoolean
asserts 29::bit(4) serializes as "1101".
SQL type names that cast to a PG bit string — emitted as a string of ASCII '0'/'1' characters in PG's text format. pgjdbc's testgetBadBoolean asserts `29::bit(4)` serializes as "1101".
SQL type names that cast to boolean.
SQL type names that cast to boolean.
SQL type names that cast to a byte array.
SQL type names that cast to a byte array.
(cast-category sql-type-name)Classify a SQL type name for CAST handling.
Returns :integer, :float, :text, :boolean, :date, :time,
:timestamp, :uuid, :bytes, :array, or nil. :date and :time are
checked before :timestamp so callers can emit the
display-appropriate Java type (LocalDate / LocalTime vs Instant).
Any type-name ending in [] classifies as :array; the element
category can be resolved by recursing on the prefix.
Classify a SQL type name for CAST handling. Returns :integer, :float, :text, :boolean, :date, :time, :timestamp, :uuid, :bytes, :array, or nil. :date and :time are checked before :timestamp so callers can emit the display-appropriate Java type (LocalDate / LocalTime vs Instant). Any type-name ending in `[]` classifies as :array; the element category can be resolved by recursing on the prefix.
SQL type names that cast to a DATE (no time component). Needed because PG's DATE/TIME/TIMESTAMP all serialize differently in text format — e.g. DATE is '2017-03-13', TIME is '14:25:48', TIMESTAMP is '2017-03-13 14:25:48'. Datahike stores them all as :db.type/instant, so the distinction is only preserved through CAST expressions (where we know the target display type).
SQL type names that cast to a DATE (no time component). Needed because PG's DATE/TIME/TIMESTAMP all serialize differently in text format — e.g. DATE is '2017-03-13', TIME is '14:25:48', TIMESTAMP is '2017-03-13 14:25:48'. Datahike stores them all as :db.type/instant, so the distinction is only preserved through CAST expressions (where we know the target display type).
SQL type names that cast to floating point (Clojure double).
SQL type names that cast to floating point (Clojure double).
SQL type names that cast to integer (Clojure long).
SQL type names that cast to integer (Clojure long).
SQL type names that cast to text (Clojure string).
SQL type names that cast to text (Clojure string).
SQL type names that cast to a TIME (no date component).
SQL type names that cast to a TIME (no date component).
SQL type names that cast to timestamp/instant.
SQL type names that cast to timestamp/instant.
(decode-numeric-typmod typmod)Inverse of encode-numeric-typmod. Returns [precision scale] or
[nil nil] for typmod -1 (unconstrained NUMERIC).
Inverse of `encode-numeric-typmod`. Returns `[precision scale]` or `[nil nil]` for typmod -1 (unconstrained NUMERIC).
Map Datahike :db/valueType to PostgreSQL type OID for wire protocol.
Map Datahike :db/valueType to PostgreSQL type OID for wire protocol.
Map Datahike :db/valueType to PostgreSQL type name string.
Map Datahike :db/valueType to PostgreSQL type name string.
(dh-type-for-oid oid)Inverse lookup: PG OID → Datahike valueType. Returns nil for unknown OIDs (caller decides fallback).
Inverse lookup: PG OID → Datahike valueType. Returns nil for unknown OIDs (caller decides fallback).
(dh-type-for-sql-name sql-name)Return the Datahike valueType for a SQL type name (lowercased).
Return the Datahike valueType for a SQL type name (lowercased).
Element-type keyword (as stored on PgArray :elem-type) → OID.
Element-type keyword (as stored on PgArray :elem-type) → OID.
Scalar element OID → corresponding T[] OID.
Scalar element OID → corresponding T[] OID.
(encode-numeric-typmod precision scale)Encode PG NUMERIC(precision, scale) → atttypmod integer. Returns -1 when both precision and scale are nil (unconstrained).
Encode PG NUMERIC(precision, scale) → atttypmod integer. Returns -1 when both precision and scale are nil (unconstrained).
(format-type type-oid _typmod)PostgreSQL format_type(oid, typmod) — return type name for an OID.
PostgreSQL format_type(oid, typmod) — return type name for an OID.
(infer-oid-from-value v)Infer a PostgreSQL type OID from a Clojure runtime value.
Infer a PostgreSQL type OID from a Clojure runtime value.
Map PostgreSQL type OID to canonical type name string.
Map PostgreSQL type OID to canonical type name string.
Map OID to type size for RowDescription's typlen field. Positive = fixed size in bytes, -1 = variable length.
Map OID to type size for RowDescription's typlen field. Positive = fixed size in bytes, -1 = variable length.
(oid-for-dh-type vtype)Return the PostgreSQL type OID for a Datahike valueType keyword.
Return the PostgreSQL type OID for a Datahike valueType keyword.
(parse-array-type-name s)Parse a SQL type string for arrays. Returns
{:elem <kw> :pg-name "_T" :ndim N} or nil if not an array.
Strips (p,s) typmod, tolerates int ARRAY / int ARRAY[3]
(PG's alternative array syntax — the size is informational,
PG doesn't enforce it). Element-name is matched against
sql-name->elem-kw so we cover every scalar in our registry.
Multi-dim is reflected by :ndim; we accept arbitrary N at parse
time (DDL can choose to reject N>1 if it isn't ready to handle
them, but the parser doesn't lose information).
"int[]" → {:elem :int4, :pg-name "_int4", :ndim 1}
"text[][]" → {:elem :text, :pg-name "_text", :ndim 2}
"int ARRAY" → {:elem :int4, :pg-name "_int4", :ndim 1}
"int ARRAY[3]" → {:elem :int4, :pg-name "_int4", :ndim 1}
"numeric(p,s)[]" → {:elem :numeric, :pg-name "_numeric", :ndim 1}
Parse a SQL type string for arrays. Returns
`{:elem <kw> :pg-name "_T" :ndim N}` or `nil` if not an array.
Strips `(p,s)` typmod, tolerates `int ARRAY` / `int ARRAY[3]`
(PG's alternative array syntax — the size is informational,
PG doesn't enforce it). Element-name is matched against
`sql-name->elem-kw` so we cover every scalar in our registry.
Multi-dim is reflected by `:ndim`; we accept arbitrary N at parse
time (DDL can choose to reject N>1 if it isn't ready to handle
them, but the parser doesn't lose information).
"int[]" → {:elem :int4, :pg-name "_int4", :ndim 1}
"text[][]" → {:elem :text, :pg-name "_text", :ndim 2}
"int ARRAY" → {:elem :int4, :pg-name "_int4", :ndim 1}
"int ARRAY[3]" → {:elem :int4, :pg-name "_int4", :ndim 1}
"numeric(p,s)[]" → {:elem :numeric, :pg-name "_numeric", :ndim 1}(parse-numeric-args type-str)Parse the JSqlParser ColDataType string "NUMERIC (10, 2)" (or
"DECIMAL(10,2)" etc.) into [precision scale]. Returns
[nil nil] when no parens present (plain NUMERIC).
Parse the JSqlParser ColDataType string `"NUMERIC (10, 2)"` (or `"DECIMAL(10,2)"` etc.) into `[precision scale]`. Returns `[nil nil]` when no parens present (plain `NUMERIC`).
Map a PostgreSQL type name (the string stored on :pg/type when
the column's original SQL type doesn't match the Datahike
valueType 1:1 — e.g. date vs timestamp both collapse to
:db.type/instant) back to its wire OID. Used by
infer-param-oid-for-column so pgjdbc's ParameterDescription sees
the SQL-declared type, not our internal reduction.
Includes paired _T entries for every scalar — _int4 →
oid-int4-array, etc. — so an array column's :pg/type (set
to _int4 etc. by translate-create-table for int[] columns)
resolves directly to its array OID.
Map a PostgreSQL type name (the string stored on :pg/type when the column's original SQL type doesn't match the Datahike valueType 1:1 — e.g. `date` vs `timestamp` both collapse to :db.type/instant) back to its wire OID. Used by infer-param-oid-for-column so pgjdbc's ParameterDescription sees the SQL-declared type, not our internal reduction. Includes paired `_T` entries for every scalar — `_int4` → oid-int4-array, etc. — so an array column's `:pg/type` (set to `_int4` etc. by translate-create-table for `int[]` columns) resolves directly to its array OID.
(pg-name-for-dh-type vtype)Return the PostgreSQL type name string for a Datahike valueType keyword.
Return the PostgreSQL type name string for a Datahike valueType keyword.
Common PostgreSQL types for the pg_type virtual table. Each entry: [oid typname typlen typtype]
Common PostgreSQL types for the pg_type virtual table. Each entry: [oid typname typlen typtype]
Map SQL type names (lowercased) to Datahike :db/valueType keywords. Covers all common SQL and PostgreSQL type name variants.
Map SQL type names (lowercased) to Datahike :db/valueType keywords. Covers all common SQL and PostgreSQL type name variants.
SQL type name (lowercased, no (p,s) parens) → the array-element
keyword used on PgArray :elem-type. More specific than
sql-name->dh-type because integer width matters at the wire
layer (int4[] → _int4, OID 1007; int8[] → _int8, OID 1016)
even though both reduce to :db.type/long in Datahike storage.
SQL type name (lowercased, no `(p,s)` parens) → the array-element keyword used on `PgArray :elem-type`. More specific than `sql-name->dh-type` because integer width matters at the wire layer (`int4[]` → `_int4`, OID 1007; `int8[]` → `_int8`, OID 1016) even though both reduce to `:db.type/long` in Datahike storage.
(wire-size oid)Return the wire protocol type size for an OID.
Return the wire protocol type size for an OID.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |