#6506·paradedb

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 in AggregateScan), VisibilityChecker filters out dead MVCC versions during scanning, so the scan output is already above visibility checking and constraints can be declared directly on PgSearchTableProvider.
  • When a scan is VisibilityMode::Deferred (such as in JoinScan), dead MVCC versions are pruned higher in the plan at VisibilityFilterExec (logically VisibilityFilterNode). 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: In GROUP BY pk, col_a, col_b, dependent columns col_a and col_b can be eliminated from group expressions when the parent query does not require them.
  • ReplaceDistinctWithAggregate: Eliminates redundant DISTINCT when grouping or projecting over unique keys.

What is your proposed implementation for this feature?

  • Catalog inspection: Generalize unique_column_attnos on PgSearchRelation (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 as Constraint::PrimaryKey and Constraint::Unique when their columns are present in the scan fields, gated on VisibilityMode::Eager.
  • Deferred scans: When VisibilityFilterOptimizerRule inserts VisibilityFilterNode (and physically VisibilityFilterExec) in pg_search/src/postgres/customscan/joinscan/visibility_filter.rs, attach functional dependencies and constraints for the verified relations to the output schema and physical properties.