Skip to content

Commit 5f89a98

Browse files
committed
Handle OID column inheritance correctly in ALTER TABLE ... INHERIT.
Inheritance operations must treat the OID column, if any, much like regular user columns. But MergeAttributesIntoExisting() neglected to do that, leading to weird results after a table with OIDs is associated to a parent with OIDs via ALTER TABLE ... INHERIT. Report and patch by Amit Langote, reviewed by Ashutosh Bapat, some adjustments by me. It's been broken all along, so back-patch to all supported branches. Discussion: https://postgr.es/m/cb13cfe7-a48c-5720-c383-bb843ab28298@lab.ntt.co.jp
1 parent e7c586e commit 5f89a98

File tree

3 files changed

+108
-0
lines changed

3 files changed

+108
-0
lines changed

src/backend/commands/tablecmds.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9356,6 +9356,39 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
93569356
}
93579357
}
93589358

9359+
/*
9360+
* If the parent has an OID column, so must the child, and we'd better
9361+
* update the child's attinhcount and attislocal the same as for normal
9362+
* columns. We needn't check data type or not-nullness though.
9363+
*/
9364+
if (tupleDesc->tdhasoid)
9365+
{
9366+
/*
9367+
* Here we match by column number not name; the match *must* be the
9368+
* system column, not some random column named "oid".
9369+
*/
9370+
tuple = SearchSysCacheCopy2(ATTNUM,
9371+
ObjectIdGetDatum(RelationGetRelid(child_rel)),
9372+
Int16GetDatum(ObjectIdAttributeNumber));
9373+
if (HeapTupleIsValid(tuple))
9374+
{
9375+
Form_pg_attribute childatt = (Form_pg_attribute) GETSTRUCT(tuple);
9376+
9377+
/* See comments above; these changes should be the same */
9378+
childatt->attinhcount++;
9379+
simple_heap_update(attrrel, &tuple->t_self, tuple);
9380+
CatalogUpdateIndexes(attrrel, tuple);
9381+
heap_freetuple(tuple);
9382+
}
9383+
else
9384+
{
9385+
ereport(ERROR,
9386+
(errcode(ERRCODE_DATATYPE_MISMATCH),
9387+
errmsg("child table is missing column \"%s\"",
9388+
"oid")));
9389+
}
9390+
}
9391+
93599392
heap_close(attrrel, RowExclusiveLock);
93609393
}
93619394

src/test/regress/expected/inherit.out

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,55 @@ select * from d;
612612
32 | one | two | three
613613
(1 row)
614614

615+
-- check that oid column is handled properly during alter table inherit
616+
create table oid_parent (a int) with oids;
617+
create table oid_child () inherits (oid_parent);
618+
select attinhcount, attislocal from pg_attribute
619+
where attrelid = 'oid_child'::regclass and attname = 'oid';
620+
attinhcount | attislocal
621+
-------------+------------
622+
1 | f
623+
(1 row)
624+
625+
drop table oid_child;
626+
create table oid_child (a int) without oids;
627+
alter table oid_child inherit oid_parent; -- fail
628+
ERROR: table "oid_child" without OIDs cannot inherit from table "oid_parent" with OIDs
629+
alter table oid_child set with oids;
630+
select attinhcount, attislocal from pg_attribute
631+
where attrelid = 'oid_child'::regclass and attname = 'oid';
632+
attinhcount | attislocal
633+
-------------+------------
634+
0 | t
635+
(1 row)
636+
637+
alter table oid_child inherit oid_parent;
638+
select attinhcount, attislocal from pg_attribute
639+
where attrelid = 'oid_child'::regclass and attname = 'oid';
640+
attinhcount | attislocal
641+
-------------+------------
642+
1 | t
643+
(1 row)
644+
645+
alter table oid_child set without oids; -- fail
646+
ERROR: cannot drop inherited column "oid"
647+
alter table oid_parent set without oids;
648+
select attinhcount, attislocal from pg_attribute
649+
where attrelid = 'oid_child'::regclass and attname = 'oid';
650+
attinhcount | attislocal
651+
-------------+------------
652+
0 | t
653+
(1 row)
654+
655+
alter table oid_child set without oids;
656+
select attinhcount, attislocal from pg_attribute
657+
where attrelid = 'oid_child'::regclass and attname = 'oid';
658+
attinhcount | attislocal
659+
-------------+------------
660+
(0 rows)
661+
662+
drop table oid_parent cascade;
663+
NOTICE: drop cascades to table oid_child
615664
-- Test non-inheritable parent constraints
616665
create table p1(ff1 int);
617666
alter table p1 add constraint p1chk check (ff1 > 0) no inherit;

src/test/regress/sql/inherit.sql

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,32 @@ insert into d values('test','one','two','three');
145145
alter table a alter column aa type integer using bit_length(aa);
146146
select * from d;
147147

148+
-- check that oid column is handled properly during alter table inherit
149+
create table oid_parent (a int) with oids;
150+
151+
create table oid_child () inherits (oid_parent);
152+
select attinhcount, attislocal from pg_attribute
153+
where attrelid = 'oid_child'::regclass and attname = 'oid';
154+
drop table oid_child;
155+
156+
create table oid_child (a int) without oids;
157+
alter table oid_child inherit oid_parent; -- fail
158+
alter table oid_child set with oids;
159+
select attinhcount, attislocal from pg_attribute
160+
where attrelid = 'oid_child'::regclass and attname = 'oid';
161+
alter table oid_child inherit oid_parent;
162+
select attinhcount, attislocal from pg_attribute
163+
where attrelid = 'oid_child'::regclass and attname = 'oid';
164+
alter table oid_child set without oids; -- fail
165+
alter table oid_parent set without oids;
166+
select attinhcount, attislocal from pg_attribute
167+
where attrelid = 'oid_child'::regclass and attname = 'oid';
168+
alter table oid_child set without oids;
169+
select attinhcount, attislocal from pg_attribute
170+
where attrelid = 'oid_child'::regclass and attname = 'oid';
171+
172+
drop table oid_parent cascade;
173+
148174
-- Test non-inheritable parent constraints
149175
create table p1(ff1 int);
150176
alter table p1 add constraint p1chk check (ff1 > 0) no inherit;

0 commit comments

Comments
 (0)