From: Tom Lane <tgl@sss.pgh.pa.us>
To: Roy Brokvam <roy.brokvam@gmail.com>
Cc: pgsql-bugs@postgresql.org
Subject: Re: Meaning of pg_constraint.conindid for foreign keys
Date: Thu, 27 Apr 2017 09:54:47 -0400
Message-ID: <26798.1493301287@sss.pgh.pa.us> (raw)
In-Reply-To: <CADhin7Xj7FFp3N+d_J=4ya8iqo+hnoyX23vYEA35guMDDuGzbg@mail.gmail.com>
References: <CADhin7Xj7FFp3N+d_J=4ya8iqo+hnoyX23vYEA35guMDDuGzbg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Roy Brokvam <roy.brokvam@gmail.com> writes:
> To my surprise, the query did not return any rows, even though I knew there
> existed indexless foreign keys referencing my table. After investigating
> further, saw that conindid contained the oid of the referenced table's
> primary key, not the oid of the index "implementing" the foreign key.
Not what I see here:
regression=# create table foo1(f1 int primary key);
CREATE TABLE
regression=# create table foo2(f1 int);
CREATE TABLE
regression=# create unique index on foo2(f1);
CREATE INDEX
regression=# create table bar1 (f1 int references foo1(f1));
CREATE TABLE
regression=# create table bar2 (f1 int references foo2(f1));
CREATE TABLE
regression=# select oid,conname,contype,conrelid::regclass,conindid,conindid::regclass,confrelid::regclass from pg_constraint order by oid desc limit 3;
oid | conname | contype | conrelid | conindid | conindid | confrelid
--------+--------------+---------+----------+----------+-------------+-----------
232357 | bar2_f1_fkey | f | bar2 | 232353 | foo2_f1_idx | foo2
232345 | bar1_f1_fkey | f | bar1 | 232337 | foo1_pkey | foo1
232338 | foo1_pkey | p | foo1 | 232337 | foo1_pkey | -
(3 rows)
foo1's pkey constraint has OID 232338, its underlying index has
OID 232337, and the latter is what's in conindid for bar1's
foreign key constraint. It has to be that way because of the
case illustrated by foo2/bar2, where an FK depends on an index
that doesn't have a formal constraint associated with it.
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, roy.brokvam@gmail.com
Subject: Re: Meaning of pg_constraint.conindid for foreign keys
In-Reply-To: <26798.1493301287@sss.pgh.pa.us>
References: <CADhin7Xj7FFp3N+d_J=4ya8iqo+hnoyX23vYEA35guMDDuGzbg@mail.gmail.com> <26798.1493301287@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox