Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T0DDH-0007t6-HX for pgsql-bugs@postgresql.org; Sat, 11 Aug 2012 15:02:51 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1T0DDF-0004Nd-AO for pgsql-bugs@postgresql.org; Sat, 11 Aug 2012 15:02:51 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id q7BF29UZ012535; Sat, 11 Aug 2012 11:02:09 -0400 (EDT) From: Tom Lane To: depesz@depesz.com cc: pgsql-bugs@postgresql.org Subject: Re: Bug when changing datatype of primary key column In-reply-to: <20120811105331.GA5981@depesz.com> References: <20120811105331.GA5981@depesz.com> Comments: In-reply-to hubert depesz lubaczewski message dated "Sat, 11 Aug 2012 12:53:32 +0200" Date: Sat, 11 Aug 2012 11:02:09 -0400 Message-ID: <12533.1344697329@sss.pgh.pa.us> X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201208/58 X-Sequence-Number: 33782 hubert depesz lubaczewski writes: > $ create table z (i int4); > CREATE TABLE > $ create unique index q on z (i); > CREATE INDEX > $ alter table z add primary key using index q; > ALTER TABLE > $ alter table z alter column i type int8; > ERROR: could not open relation with OID 16503 > looks like some missing dependancy. Mph. Looks more like too many dependencies :-( If you just create a pkey straight off, the dependencies look like this: regression=# create domain ref as int; CREATE DOMAIN regression=# create table z (i int primary key); CREATE TABLE regression=# select pg_describe_object(classid,objid,objsubid) as obj, pg_describe_object(refclassid,refobjid,refobjsubid) as ref, deptype from pg_depend where objid > 'ref'::regtype or refobjid > 'ref'::regtype; obj | ref | deptype ------------------------------+------------------------------+--------- type z | table z | i type z[] | type z | i table z | schema public | n constraint z_pkey on table z | table z column i | a index z_pkey | constraint z_pkey on table z | i (5 rows) But if you make the index separately and then use "add primary key using index": regression=# drop table z; DROP TABLE regression=# create table z (i int4); CREATE TABLE regression=# create unique index q on z (i); CREATE INDEX regression=# select pg_describe_object(classid,objid,objsubid) as obj, pg_describe_object(refclassid,refobjid,refobjsubid) as ref, deptype from pg_depend where objid > 'ref'::regtype or refobjid > 'ref'::regtype; obj | ref | deptype ----------+------------------+--------- type z | table z | i type z[] | type z | i table z | schema public | n index q | table z column i | a (4 rows) regression=# alter table z add primary key using index q; ALTER TABLE regression=# select pg_describe_object(classid,objid,objsubid) as obj, pg_describe_object(refclassid,refobjid,refobjsubid) as ref, deptype from pg_depend where objid > 'ref'::regtype or refobjid > 'ref'::regtype; obj | ref | deptype -------------------------+-------------------------+--------- type z | table z | i type z[] | type z | i table z | schema public | n index q | table z column i | a constraint q on table z | table z column i | a index q | constraint q on table z | i (6 rows) So that ALTER is forgetting to remove the index's original direct dependency on the table column(s). I suppose ideally that wouldn't matter, but in the real world it will likely confuse many things, not just ALTER COLUMN TYPE. regards, tom lane