Re: Restore relhaspkey in PostgreSQL Version 11 Beta - Mailing list pgsql-general

From Tom Lane
Subject Re: Restore relhaspkey in PostgreSQL Version 11 Beta
Date
Msg-id 27216.1532961061@sss.pgh.pa.us
Whole thread Raw
In response to Restore relhaspkey in PostgreSQL Version 11 Beta  (Melvin Davidson <melvin6925@gmail.com>)
Responses Re: Restore relhaspkey in PostgreSQL Version 11 Beta  (Melvin Davidson <melvin6925@gmail.com>)
List pgsql-general
Melvin Davidson <melvin6925@gmail.com> writes:
> In the release notes for Version 11 Beta, under changes, I see these scary
> remarks:
> Remove relhaspkey column from system table pg_class (Peter Eisentraut)
> Applications needing to check for a primary key should consult pg_index.
> 
> That absolutely breaks my code (and I'm guessing others), as I have a cron
> job that checks for tables that were created with no pkey.

Well, I'd say your code was broken anyway, because it has never been the
case that relhaspkey meant that the table *currently* has a primary key.
We got rid of it on the grounds that its semantics were too squishy to
be useful.

What you want is something like

select relname from pg_class c where relkind = 'r' and
  not exists (select 1 from pg_index where indrelid = c.oid and indisprimary);

which will give the right answer in all PG versions.

            regards, tom lane


pgsql-general by date:

Previous
From: Melvin Davidson
Date:
Subject: Restore relhaspkey in PostgreSQL Version 11 Beta
Next
From: hmidi slim
Date:
Subject: Design of a database table