Search code examples
sqlpostgresqlconstraintsforeign-data-wrapper

PostgreSQL foreign data wrapper mimic Constraints


Using PostgreSQL 9.6.9

It appears that constraints are not available when using the Postgres foreign data wrapper https://github.com/postgres/postgres/blob/e1ae40f381d0582981b1e63856bd4b060cfe2d53/src/test/regress/expected/foreign_data.out#L692-L711.

  • primary key constraints are not supported on foreign tables
  • foreign key constraints are not supported on foreign tables
  • unique constraints are not supported on foreign tables

I want to run a query that groups by a primary key and selects some other columns in the same table. The query does not work when the primary key constraint is absent. https://www.postgresql.org/docs/current/sql-select.html#SQL-GROUPBY.

A functional dependency exists if the grouped columns (or a subset thereof) are the primary key of the table containing the ungrouped column.

The easy solution would be to add the other columns to the GROUP BY but tech debt makes this another challenge.

Even if the constraint cannot be enforced, is there a way to mimic or partially enable the constraint so the query works?


Solution

  • No, you will have to add the other columns to the GROUP BY clause. I assume by "tech debt" you are referring to the need to type all the columns. Use a smart text editor!