Skip to content

Fix SQLPrimaryKeys to exclude INCLUDE columns from results#171

Merged
davecramer merged 5 commits intopostgresql-interfaces:mainfrom
davecramer:fix_include_columns
Apr 14, 2026
Merged

Fix SQLPrimaryKeys to exclude INCLUDE columns from results#171
davecramer merged 5 commits intopostgresql-interfaces:mainfrom
davecramer:fix_include_columns

Conversation

@davecramer
Copy link
Copy Markdown
Contributor

When a PRIMARY KEY is created with an INCLUDE clause (PostgreSQL 11+),
SQLPrimaryKeys incorrectly returned both the key columns and the
included columns. For example, given PRIMARY KEY (a, b) INCLUDE (c, d),
all four columns were returned instead of just a and b.

The root cause was that the pg_attribute join on the index relation
iterated over all index attributes without distinguishing key columns
from included columns. Fix by adding a filter on i.indnkeyatts when
connected to PostgreSQL >= 11, which limits results to only the actual
key attributes. Both query paths in PGAPI_PrimaryKeys are fixed.

Add a regression test that creates a table with a composite primary key
using INCLUDE and verifies only the key columns are returned.

fixes #170

  When a PRIMARY KEY is created with an INCLUDE clause (PostgreSQL 11+),
  SQLPrimaryKeys incorrectly returned both the key columns and the
  included columns. For example, given PRIMARY KEY (a, b) INCLUDE (c, d),
  all four columns were returned instead of just a and b.

  The root cause was that the pg_attribute join on the index relation
  iterated over all index attributes without distinguishing key columns
  from included columns. Fix by adding a filter on i.indnkeyatts when
  connected to PostgreSQL >= 11, which limits results to only the actual
  key attributes. Both query paths in PGAPI_PrimaryKeys are fixed.

  Add a regression test that creates a table with a composite primary key
  using INCLUDE and verifies only the key columns are returned.
@davecramer davecramer merged commit 618566f into postgresql-interfaces:main Apr 14, 2026
6 checks passed
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.

Included columns are included in SQLPrimaryKey() output

1 participant