Skip to content

PostgreSQL: Support PRIMARY KEY/UNIQUE USING INDEX#2213

Open
guan404ming wants to merge 2 commits intoapache:mainfrom
guan404ming:pg-primary-key-using-index
Open

PostgreSQL: Support PRIMARY KEY/UNIQUE USING INDEX#2213
guan404ming wants to merge 2 commits intoapache:mainfrom
guan404ming:pg-primary-key-using-index

Conversation

@guan404ming
Copy link
Member

Why

PostgreSQL allows promoting an existing unique index to a PRIMARY KEY or UNIQUE table constraint via ALTER TABLE, which was
not supported by the parser.

How

  • Add ConstraintUsingIndex variant to TableConstraint with display, span, and serialization support
  • Parse USING INDEX index_name after PRIMARY KEY or UNIQUE keywords in constraint parsing
  • Add tests for named/unnamed constraints and deferrable characteristics

@guan404ming guan404ming marked this pull request as ready for review February 9, 2026 09:54
Signed-off-by: Guan-Ming Chiu <guanmingchiu@gmail.com>
@guan404ming guan404ming force-pushed the pg-primary-key-using-index branch from 3cdaa92 to 91a677f Compare February 12, 2026 01:34
Comment on lines 9396 to 9408
if self.parse_keywords(&[Keyword::USING, Keyword::INDEX]) {
let index_name = self.parse_identifier()?;
let characteristics = self.parse_constraint_characteristics()?;
return Ok(Some(
ConstraintUsingIndex {
name,
is_primary_key: true,
index_name,
characteristics,
}
.into(),
));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we pull this out into a e.g. parse_constraint_using_index() function so that it can be reused across both cases?

Also repr wise, I think it would be clearer to have both cases as unique variants on the TableConstraint enum vs using the is_primary_key bool to differentiate. i.e.

TableConstraint::UniqueUsingIndex(ConstraintUsingIndex)
TableConstraint::PrimaryKeyUsingIndex(ConstraintUsingIndex)

Copy link
Member Author

@guan404ming guan404ming Feb 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks for the nice suggestions. I just updated with

  1. Split enum variant: TableConstraint::ConstraintUsingIndex into PrimaryKeyUsingIndex and UniqueUsingIndex, removing the is_primary_key bool from the struct.
  2. Extracted helper: Created parse_constraint_using_index() to deduplicate the parsing logic used by both UNIQUE USING INDEX and PRIMARY KEY USING INDEX.

@guan404ming guan404ming force-pushed the pg-primary-key-using-index branch from 2f4982a to 483ddeb Compare February 18, 2026 14:20
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.

2 participants

Comments