pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

WITHOUT OVERLAPS and PERIOD clauses are dropped from temporal primary keys and foreign keys (PostgreSQL 18)

Open kenchan0130 opened this issue 8 months ago • 0 comments

Description

pgschema silently drops WITHOUT OVERLAPS from temporal primary keys and PERIOD from temporal foreign keys when generating migration plans or dumping schemas. These are PostgreSQL 18 features for temporal constraints (docs).

Steps to Reproduce

  1. Create a schema file with temporal constraints:
CREATE EXTENSION IF NOT EXISTS btree_gist;

CREATE TABLE contacts (
    id UUID NOT NULL,
    name TEXT NOT NULL,
    valid_period TSTZRANGE NOT NULL DEFAULT tstzrange(now(), 'infinity', '[)'),
    PRIMARY KEY (id, valid_period WITHOUT OVERLAPS)
);

CREATE TABLE conversations (
    id UUID NOT NULL,
    contact_id UUID NOT NULL,
    valid_period TSTZRANGE NOT NULL DEFAULT tstzrange(now(), 'infinity', '[)'),
    PRIMARY KEY (id, valid_period WITHOUT OVERLAPS),
    FOREIGN KEY (contact_id, PERIOD valid_period)
      REFERENCES contacts (id, PERIOD valid_period)
);
  1. Run pgschema plan:
pgschema plan --file schema.sql --db test_temporal ...
  1. Observe the output:
-- PRIMARY KEY loses WITHOUT OVERLAPS:
CONSTRAINT contacts_pkey PRIMARY KEY (id, valid_period)

-- FOREIGN KEY loses PERIOD:
CONSTRAINT conversations_contact_id_valid_period_fkey
  FOREIGN KEY (contact_id, valid_period) REFERENCES contacts (id, valid_period)
  1. pgschema dump also drops these clauses from an existing database that has temporal constraints applied correctly (verified via pg_get_constraintdef()).

Expected Behavior

pgschema should preserve WITHOUT OVERLAPS and PERIOD in both plan output and dump output:

CONSTRAINT contacts_pkey PRIMARY KEY (id, valid_period WITHOUT OVERLAPS)

CONSTRAINT conversations_contact_id_valid_period_fkey
  FOREIGN KEY (contact_id, PERIOD valid_period)
  REFERENCES contacts (id, PERIOD valid_period)

Why This Matters

Without these clauses, the generated DDL creates regular composite primary keys and foreign keys instead of temporal ones. This means:

  • No overlap prevention: Multiple rows with the same id and overlapping time periods are allowed
  • No temporal referential integrity: The database does not enforce that referenced rows exist for the entire referencing period

These are semantically different constraints, and silently downgrading them leads to data integrity issues.

Environment

  • pgschema version: 1.7.4
  • PostgreSQL version: 18.3
  • OS: macOS (darwin/arm64)

Additional Context

PostgreSQL 18 stores these constraints correctly in pg_constraint, as confirmed by:

SELECT conname, pg_get_constraintdef(oid) FROM pg_constraint
WHERE conrelid = 'contacts'::regclass AND contype = 'p';

-- Result:
-- contacts_pkey | PRIMARY KEY (id, valid_period WITHOUT OVERLAPS)

The WITHOUT OVERLAPS clause internally creates a GiST exclusion constraint (requiring the btree_gist extension) rather than a standard B-tree primary key. Similarly, PERIOD foreign keys enforce containment semantics rather than simple equality.

kenchan0130 avatar Mar 22 '26 12:03 kenchan0130