Primary Key Constraint on inheritance table not getting route to child tables

Lists: pgsql-hackers
From: Rushabh Lathia <rushabh(dot)lathia(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Cc: Rushabh Lathia <rushabh(dot)lathia(at)enterprisedb(dot)com>
Subject: Primary Key Constraint on inheritance table not getting route to child tables
Date: 2012-08-20 06:50:52
Message-ID: CAGPqQf2Mx-B0kwejftWckzCEpAfzfkCNhvd+qp3nZ0hbwEMFdA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hi,

ALTER TABLE ADD Constraints PRIMARY KEY on inheritance table not getting
route to child table.

But when we do ALTER TABLE DROP Constraint on the same, it complains about
constraint does not
exists on child table.

Consider the following example

psql=# CREATE TABLE measurement (
psql(# city_id int not null,
psql(# logdate date not null,
psql(# peaktemp int,
psql(# unitsales int
psql(# );
CREATE TABLE
psql=# CREATE TABLE measurement_y2006m02 (
psql(# CHECK ( logdate >= DATE '2006-02-01' AND logdate < DATE
'2006-03-01' )
psql(# ) INHERITS (measurement);
CREATE TABLE
psql=# CREATE TABLE measurement_y2006m03 (
psql(# CHECK ( logdate >= DATE '2006-03-01' AND logdate < DATE
'2006-04-01' )
psql(# ) INHERITS (measurement);
CREATE TABLE
psql=#
psql=#
psql=# ALTER TABLE measurement
ADD CONSTRAINT con1 PRIMARY KEY (city_id);
ALTER TABLE
psql=#
psql=#

-- Don't have primary key on child table
psql=# desc measurement_y2006m02
Table "public.measurement_y2006m02"
Column | Type | Modifiers
-----------+-----------------------------+-----------
city_id | integer | not null
logdate | timestamp without time zone | not null
peaktemp | integer |
unitsales | integer |
Check constraints:
"measurement_y2006m02_logdate_check" CHECK (logdate >= '01-FEB-06
00:00:00'::timestamp without time zone AND logdate < '01-MAR-06
00:00:00'::timestamp without time zone)
Inherits: measurement

-- Primary key on parent table
psql=# desc measurement
Table "public.measurement"
Column | Type | Modifiers
-----------+-----------------------------+-----------
city_id | integer | not null
logdate | timestamp without time zone | not null
peaktemp | integer |
unitsales | integer |
Indexes:
"con1" PRIMARY KEY, btree (city_id)
Number of child tables: 2 (Use \d+ to list them.)

*psql=# ALTER TABLE measurement*
*DROP CONSTRAINT con1;*
*ERROR: constraint "con1" of relation "measurement_y2006m02" does not exist
*

I am not sure whether PRIMARY KEY not getting route is a
expected behavior or not, but if its expected behavior
then obviously DROP CONSTRAINT should not complain about constraint doesn't
exists on child table.

Inputs/Comments ?

Thanks,
Rushabh Lathia
www.EnterpriseDB.com


From: Alvaro Herrera <alvherre(at)2ndquadrant(dot)com>
To: Rushabh Lathia <rushabh(dot)lathia(at)gmail(dot)com>
Cc: Pg Hackers <pgsql-hackers(at)postgresql(dot)org>, Rushabh Lathia <rushabh(dot)lathia(at)enterprisedb(dot)com>
Subject: Re: Primary Key Constraint on inheritance table not getting route to child tables
Date: 2012-08-20 15:39:06
Message-ID: 1345472788-sup-2672@alvh.no-ip.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Excerpts from Rushabh Lathia's message of lun ago 20 02:50:52 -0400 2012:
> Hi,
>
> ALTER TABLE ADD Constraints PRIMARY KEY on inheritance table not getting
> route to child table.
>
> But when we do ALTER TABLE DROP Constraint on the same, it complains about
> constraint does not
> exists on child table.

This is a known 9.2 bug, fixed a month in this commit:

Author: Alvaro Herrera <alvherre(at)alvh(dot)no-ip(dot)org>
Branch: master [f5bcd398a] 2012-07-20 14:08:07 -0400
Branch: REL9_2_STABLE [d721f208a] 2012-07-20 14:07:09 -0400

connoinherit may be true only for CHECK constraints

The code was setting it true for other constraints, which is
bogus. Doing so caused bogus catalog entries for such constraints, and
in particular caused an error to be raised when trying to drop a
constraint of types other than CHECK from a table that has children,
such as reported in bug #6712.

In 9.2, additionally ignore connoinherit=true for other constraint
types, to avoid having to force initdb; existing databases might already
contain bogus catalog entries.

Includes a catversion bump (in HEAD only).

Bug report from Miroslav Šulc
Analysis from Amit Kapila and Noah Misch; Amit also contributed the patch.

I cannot reproduce it in 9.2 HEAD or master HEAD. I assume you were
testing with something older than the above commit; the 9.1 branch does
not contain the bug.

--
Álvaro Herrera http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Rushabh Lathia <rushabh(dot)lathia(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org, Rushabh Lathia <rushabh(dot)lathia(at)enterprisedb(dot)com>
Subject: Re: Primary Key Constraint on inheritance table not getting route to child tables
Date: 2012-08-20 15:58:07
Message-ID: 1457.1345478287@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Rushabh Lathia <rushabh(dot)lathia(at)gmail(dot)com> writes:
> ALTER TABLE ADD Constraints PRIMARY KEY on inheritance table not getting
> route to child table.

Right.

> But when we do ALTER TABLE DROP Constraint on the same, it complains about
> constraint does not exists on child table.

Works for me in HEAD. What version are you testing? This seems related
to some recent bug fixes ...

regards, tom lane


From: Rushabh Lathia <rushabh(dot)lathia(at)enterprisedb(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Rushabh Lathia <rushabh(dot)lathia(at)gmail(dot)com>, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Primary Key Constraint on inheritance table not getting route to child tables
Date: 2012-08-20 16:46:44
Message-ID: CAMBO3tWhV7azeyzg-gp9AY917n5YcjYjR-EY25byySOCZ2Qf4A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

On Mon, Aug 20, 2012 at 9:28 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> Rushabh Lathia <rushabh(dot)lathia(at)gmail(dot)com> writes:
> > ALTER TABLE ADD Constraints PRIMARY KEY on inheritance table not getting
> > route to child table.
>
> Right.
>
> > But when we do ALTER TABLE DROP Constraint on the same, it complains
> about
> > constraint does not exists on child table.
>
> Works for me in HEAD. What version are you testing? This seems related
> to some recent bug fixes ...
>

Oh ok.

Sorry for wrong noise, I was checking this on old version.

Thanks,

> regards, tom lane
>

--
--

Rushabh Lathia
Technical Architect
EnterpriseDB Corporation
The Enterprise Postgres Company

Phone: +91-20-30589494

Website: http://www.enterprisedb.com
EnterpriseDB Blog: http://blogs.enterprisedb.com/
Follow us on Twitter: http://www.twitter.com/enterprisedb