Skip to content

PostgreSQL: support INCLUDE on PRIMARY KEY / UNIQUE table constraints#2366

Open
amaksimo wants to merge 1 commit into
apache:mainfrom
amaksimo:upstream/feat-constraint-include
Open

PostgreSQL: support INCLUDE on PRIMARY KEY / UNIQUE table constraints#2366
amaksimo wants to merge 1 commit into
apache:mainfrom
amaksimo:upstream/feat-constraint-include

Conversation

@amaksimo
Copy link
Copy Markdown
Contributor

@amaksimo amaksimo commented Jun 5, 2026

Summary

PostgreSQL accepts INCLUDE (col, ...) covering columns on PRIMARY KEY and UNIQUE table constraints, both inline on CREATE TABLE and via ALTER TABLE ADD CONSTRAINT, per the index_parameters grammar:

CREATE TABLE t (
    id INT,
    payload TEXT,
    CONSTRAINT t_pk PRIMARY KEY (id) INCLUDE (payload)
);

ALTER TABLE t ADD CONSTRAINT t_uk UNIQUE (email) INCLUDE (payload);

The parser previously rejected both forms.

Changes

  • Add include: Vec<Ident> to PrimaryKeyConstraint and UniqueConstraint.
  • Display emits the clause between the columns list and index_options, matching PG's index_parameters clause order (INCLUDE -> WITH -> USING INDEX TABLESPACE).
  • New parse_optional_include_columns helper, consumed from both PK and UNIQUE arms of parse_optional_table_constraint. Mirrors the existing INCLUDE handling on CREATE INDEX.

Tests in tests/sqlparser_postgres.rs. cargo test --all-features, cargo fmt --all, cargo clippy --all-targets --all-features -- -D warnings pass locally.

PostgreSQL accepts `INCLUDE (col, ...)` covering columns on PRIMARY KEY
and UNIQUE table constraints, both inline on CREATE TABLE and via
ALTER TABLE ADD CONSTRAINT, per the `index_parameters` grammar at
https://www.postgresql.org/docs/current/sql-createtable.html. The
parser previously rejected both forms.

Adds `include: Vec<Ident>` to `PrimaryKeyConstraint` and `UniqueConstraint`,
emits the clause from `Display` between the columns list and
`index_options` (matching PG's `index_parameters` clause order:
INCLUDE -> WITH -> USING INDEX TABLESPACE), and consumes it from the
table-constraint parser via the new `parse_optional_include_columns`
helper. Mirrors the existing INCLUDE handling on CREATE INDEX.

Column-level inline forms (`id INT PRIMARY KEY`) carry `include: vec![]`
since INCLUDE is not legal there in PG.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant