Declare heap unique and primary key constraints to DataFusion above visibility checking
Author: stuhoodCreated Sep 26, 2026Updated Sep 29, 2026
Labelsfeaturegood first issuepriority-mediumperformance
What feature are you requesting?
Declare single-column and composite heap-level PRIMARY KEY and UNIQUE constraints to DataFusion above visibility checking for a table.
Why are you requesting this feature?
While the legacy BM25 key_field was removed in #6221, PostgreSQL heap relations frequently define genuine primary keys and unique indexes. #6307 introduced PgSearchRelation::unique_fields() for deferred_placement_rule.rs, but table scans and intermediate plans declare no constraints to DataFusion.
These constraints apply above visibility checking for a table, regardless of how high in the plan that happens:
- When a scan is
VisibilityMode::Eager(such as inAggregateScan),VisibilityCheckerfilters out dead MVCC versions during scanning, so the scan output is already above visibility checking and constraints can be declared directly onPgSearchTableProvider. - When a scan is
VisibilityMode::Deferred(such as inJoinScan), dead MVCC versions are pruned higher in the plan atVisibilityFilterExec(logicallyVisibilityFilterNode). Once tuples pass that barrier, the table's rows are visibility-checked, and its constraints can be declared on the output schema of that node.
Declaring these constraints unlocks:
EliminateJoin: Inner joins on primary or unique keys can be rewritten to semi-joins, or redundant outer joins can be eliminated when one side is unreferenced.optimize_projections: InGROUP BY pk, col_a, col_b, dependent columnscol_aandcol_bcan be eliminated from group expressions when the parent query does not require them.ReplaceDistinctWithAggregate: Eliminates redundantDISTINCTwhen grouping or projecting over unique keys.
What is your proposed implementation for this feature?
- Catalog inspection: Generalize
unique_column_attnosonPgSearchRelation(pg_search/src/postgres/rel.rs) to extract both single-column and composite unique indexes / primary keys from the heap relation's catalog metadata. - Eager scans: In
PgSearchTableProvider::constraints(pg_search/src/scan/table_provider.rs), expose discovered primary keys and unique indexes asConstraint::PrimaryKeyandConstraint::Uniquewhen their columns are present in the scan fields, gated onVisibilityMode::Eager. - Deferred scans: When
VisibilityFilterOptimizerRuleinsertsVisibilityFilterNode(and physicallyVisibilityFilterExec) inpg_search/src/postgres/customscan/joinscan/visibility_filter.rs, attach functional dependencies and constraints for the verified relations to the output schema and physical properties.
Source: paradedb/paradedb