Alvaro Herrera <alvhe...@alvh.no-ip.org> writes:
>> Perhaps one more task for me is to figure out a way to get a list of all
>> the constraints that are broken because of this ... let me see if I can
>> figure that out.

> It's gotta be something like this,

> SELECT conrelid::regclass AS "constrained table",
>        conname as constraint, confrelid::regclass AS "references"
> FROM pg_constraint
> WHERE contype = 'f' and conparentid = 0 AND
>    (SELECT count(*) FROM pg_constraint p2 WHERE conparentid = 
> pg_constraint.oid) <>
>    (SELECT count(*)
>       FROM pg_inherits
>      WHERE inhparent = pg_constraint.conrelid OR inhparent = 
> pg_constraint.confrelid);

Hmm ... interestingly, if I run this in HEAD's regression database,
I get

 constrained table |  constraint   | references  
-------------------+---------------+-------------
 clstr_tst         | clstr_tst_con | clstr_tst_s
(1 row)

Digging a bit deeper, the sub-select for conparentid finds no rows,
but the sub-select on pg_inherits finds

regression=# SELECT inhrelid::regclass, inhparent::regclass, 
inhseqno,inhdetachpending from pg_inherits WHERE inhparent = 
'clstr_tst'::regclass or inhparent = 'clstr_tst_s'::regclass;
   inhrelid    | inhparent | inhseqno | inhdetachpending 
---------------+-----------+----------+------------------
 clstr_tst_inh | clstr_tst |        1 | f
(1 row)

So it looks like this query needs a guard to make it ignore
constraints on traditional-inheritance tables.

                        regards, tom lane


Reply via email to