Liking cljdoc? Tell your friends :D

PostgreSQL compatibility

pg-datahike implements PostgreSQL's application-facing SQL and wire-protocol boundary on top of Datahike. It is intended to let PostgreSQL clients and applications use Datahike without adopting a separate query protocol. It is not a drop-in replacement for the PostgreSQL server or its internal storage, planning, administration, replication, and extension machinery.

Compatibility is developed and tested at the boundaries applications observe:

AreaCompatibility target
Wire protocolStartup, authentication, simple and extended queries, prepared statements, portals, result metadata, and SQLSTATE-bearing errors used by supported clients
SQLA growing application-oriented subset of PostgreSQL DDL, DML, expressions, relational queries, transactions, and type semantics
Types and functionsPostgreSQL-compatible behavior for admitted built-ins; coverage varies by type and function family
CatalogsThe catalog relations and metadata queries needed by supported drivers, tools, and ORMs
Datahike semanticsDatahike remains the storage and transaction system; PostgreSQL behavior that conflicts with its model may be unsupported or documented as different

PostgreSQL-specific server features such as procedural languages, server-side extensions, replication, roles and server administration, physical storage features, and planner controls are generally outside this compatibility target. Their syntax may still be recognized where that is useful, but applications must not assume that arbitrary PostgreSQL SQL or extensions will run unchanged.

Recursive queries with a limit

A direct scan of a recursive CTE can stop when enough rows pass the outer filter to satisfy a literal LIMIT. This covers integer counters and Odoo's menu-cycle check, which follows parent IDs until a menu reaches itself or there are no parents left. UNION ignores a row already seen in any generation. A single-row counter using UNION ALL can emit the same value repeatedly.

This execution path supports integer columns, simple projections and filters, and a single recursive CTE. Queries with sorting, aggregation or OFFSET may still return SQLSTATE 0A000. A correlated LATERAL subquery with a limited recursive CTE also returns 0A000.

Nested correlated EXISTS

For ordinary tables, a nested EXISTS can match an outer integer key against a single column projected by a derived table. Odoo uses this to find module categories with at least one group privilege. Duplicate inner keys yield one match per outer row. A null key never compares equal.

The compiler also handles a nested NOT EXISTS whose equality depends only on a more distant outer query. It builds the eligible key sets for each execution, so prepared statements see intervening writes. More complex nested shapes can return SQLSTATE 0A000, including a correlated DELETE whose inner cross join projects an array subquery.

JSONB path queries

jsonb_path_query_first accepts a path string literal rooted at $ with member access ($.name, $."a.b"), object wildcards ($.*) and array wildcards ($[*]). The default lax mode and explicit strict mode follow PostgreSQL's structural-error behaviour. An empty result yields SQL NULL. Object wildcards visit members in PostgreSQL's JSONB key order.

Bare member names may contain non-ASCII characters. Quote names beginning with a digit or containing punctuation such as . or -. Quoted members support standard JSON escapes. Path comments, escapes in bare names, PostgreSQL-specific escapes (such as \x61), filters, variables in path expressions, indexed subscripts, methods and standalone jsonpath casts remain outside this subset.

Sequence defaults and inherited columns

INSERT and COPY reserve sequence defaults independently of the row transaction. The reserved values remain consumed if a constraint rejects a row or the caller rolls back. Explicit NULL suppresses a default. Ordinary inheritance copies a SERIAL column's nextval default, but does not copy identity generation; the inherited NOT NULL constraint still applies.

VALUES, DEFAULT VALUES, and target-column defaults in INSERT SELECT are evaluated one candidate at a time. If an early candidate fails, later target defaults are not requested; already reserved sequence values remain consumed. This applies to plain INSERT and ON CONFLICT, including prepared statements and transactions. COPY also evaluates and validates source rows in order: a failed row consumes defaults evaluated for that row, but no later row's defaults. Its executor batches speculative validation, while the rows become durable only after the complete COPY succeeds. CopyFail, a later-row error, and transaction rollback therefore leave no copied row behind. Text and CSV input retain incomplete UTF-8 code points across wire frames and reject malformed input with SQLSTATE 22021. A raw, unquoted DEFAULT marker is evaluated in COPY column-list order; omitted-column defaults follow in physical column order. Stable time defaults, cancellation, and statement_timeout cover the complete COPY stream, not each wire frame separately.

INSERT SELECT sources run at Execute against the statement snapshot rather than being materialized while the statement is parsed. Stateful SELECT-list expressions are represented without performing their effects while the source is shaped. For each emitted row, pg-datahike resolves those expressions in SELECT-list order, then fills target defaults in physical column order and validates the candidate. A rejected candidate therefore consumes its own sequence values but stops every later source row, and commit never repeats an already materialized effect. When a volatile projection is also an ORDER BY key, it is evaluated for every input row before sorting and LIMIT, matching PostgreSQL's executor stage order.

This ordering guarantee currently covers stateful SELECT-list expressions. Using nextval in predicates, grouping, aggregate arguments, HAVING, or window definitions is rejected with 0A000 until those executor stages can carry the same row-wise effect boundary; pg-datahike does not silently compare deferred values in those positions.

Earlier pg-datahike versions stored SERIAL and GENERATED BY DEFAULT identity with the same identity metadata. Their original declarations cannot be recovered from that metadata. Existing columns retain their recorded identity behavior; they are not automatically reclassified as SERIAL. For a legacy SERIAL column whose default should apply to inherited inserts, explicitly restore its default with ALTER TABLE parent ALTER COLUMN id SET DEFAULT nextval('parent_id_seq'). Fresh SERIAL declarations store an ordinary nextval default and report no identity generation in the PostgreSQL catalogs.

Ordinary table inheritance

New SQL tables created with INHERITS in public store their rows and keys in the child relation. A parent scan includes child rows, ONLY scans the named relation, and tableoid identifies the physical relation for newly created children. A child's declared primary key and foreign keys apply to that relation, so a parent and its child can store the same key value.

The adapter reads both the original shared-row layout and the independent child layout. Once a store contains an independent child, use an adapter that understands that layout for subsequent SQL reads. An older adapter can miss the new child rows in parent scans.

This covers ordinary DDL, INSERT, COPY, scans, and physical-row writes in the admitted PostgreSQL regression cases. ALTER COLUMN ... SET NOT NULL validates existing rows in the target and its physical descendants before enforcing the constraint across the tree. Columns added to an ordinary parent after its children exist also receive independent storage in physical children. A null in any descendant rejects the statement with SQLSTATE 23502. Recursive changes on legacy shared columns, creation of a parent-only NO INHERIT constraint on a table with children, and recursive DROP NOT NULL remain unsupported. A column inherited from several parents enforces NOT NULL contributed by any of them. The generated pg_constraint rows can still report incomplete inheritance counts and locality for these constraints.

Concurrent catalog changes

Writes validate the catalog metadata used to prepare them against the writer's current snapshot. If that metadata has changed, the statement or transaction can fail with SQLSTATE 40001; retry the whole transaction from a fresh snapshot. This also applies to metadata changed through Datahike's native API.

Simple literal INSERTs into ordinary scalar tables can tolerate unrelated catalog changes when their target metadata is unchanged. Other statement forms use a conservative whole-catalog check and may require a retry even when the concurrent change concerns another table. Sequence counter advances and ordinary row writes do not count as catalog changes.

Wire security

pg-datahike implements PostgreSQL's conventional SSLRequest negotiation and cleartext-password authentication exchange. The password exchange is intended to run inside TLS: start-server rejects a non-loopback bind unless both TLS and a password authenticator are configured, and it rejects a plaintext StartupMessage before asking for a password. Standard client modes including sslmode=require and sslmode=verify-full are tested with pgjdbc and psql.

Authentication is a deployment boundary rather than a PostgreSQL role system. The wire layer delegates verification to an application callback, with a fixed user map available for small deployments. Authenticated users are not yet mapped to Datahike's per-database authorization model. PostgreSQL HBA rules, SCRAM, MD5, client-certificate authentication, GSS encryption, and direct TLS negotiation are outside the current surface.

Compatibility guarantees

Supported behavior is established by focused tests, client and framework integration suites, and admitted slices of PostgreSQL's upstream regression suite. Upstream tests are also used as a discovery corpus: a regression file may exercise both useful application behavior and PostgreSQL server internals, so using it during development does not imply that the whole file is supported.

Within the supported surface, pg-datahike aims to:

  • return PostgreSQL-compatible rows, column metadata, type OIDs, and NULL semantics;
  • behave consistently across simple and extended query protocols;
  • reject unsupported or invalid operations with explicit, SQLSTATE-bearing errors rather than internal failures or silent approximations;
  • preserve Datahike's transaction and durability guarantees.

Compatibility is still evolving. Before replacing PostgreSQL for an existing application, run that application's own integration and migration tests against pg-datahike. Please report a minimal query, expected PostgreSQL behavior, actual pg-datahike behavior, and client/driver details for any discrepancy.

Maintainers can find the upstream-suite workflow, admission criteria, and PostgreSQL source map in the regression harness documentation.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close