Re: Restore relhaspkey in PostgreSQL Version 11 Beta

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Melvin Davidson <melvin6925(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org >> PG-General Mailing List" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Restore relhaspkey in PostgreSQL Version 11 Beta
Date: 2018-07-30 14:31:01
Message-ID: 27216.1532961061@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Melvin Davidson <melvin6925(at)gmail(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hmidi slim 2018-07-30 14:37:00 Design of a database table
Previous Message Melvin Davidson 2018-07-30 14:20:20 Restore relhaspkey in PostgreSQL Version 11 Beta