Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf64554b

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

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
@@ -10329,6 +10329,39 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
1032910329
}
1033010330
}
1033110331

10332+
/*
10333+
* If the parent has an OID column, so must the child, and we'd better
10334+
* update the child's attinhcount and attislocal the same as for normal
10335+
* columns. We needn't check data type or not-nullness though.
10336+
*/
10337+
if (tupleDesc->tdhasoid)
10338+
{
10339+
/*
10340+
* Here we match by column number not name; the match *must* be the
10341+
* system column, not some random column named "oid".
10342+
*/
10343+
tuple=SearchSysCacheCopy2(ATTNUM,
10344+
ObjectIdGetDatum(RelationGetRelid(child_rel)),
10345+
Int16GetDatum(ObjectIdAttributeNumber));
10346+
if (HeapTupleIsValid(tuple))
10347+
{
10348+
Form_pg_attributechildatt= (Form_pg_attribute)GETSTRUCT(tuple);
10349+
10350+
/* See comments above; these changes should be the same */
10351+
childatt->attinhcount++;
10352+
simple_heap_update(attrrel,&tuple->t_self,tuple);
10353+
CatalogUpdateIndexes(attrrel,tuple);
10354+
heap_freetuple(tuple);
10355+
}
10356+
else
10357+
{
10358+
ereport(ERROR,
10359+
(errcode(ERRCODE_DATATYPE_MISMATCH),
10360+
errmsg("child table is missing column \"%s\"",
10361+
"oid")));
10362+
}
10363+
}
10364+
1033210365
heap_close(attrrel,RowExclusiveLock);
1033310366
}
1033410367

‎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
altertable a alter column aa typeinteger using bit_length(aa);
146146
select*from d;
147147

148+
-- check that oid column is handled properly during alter table inherit
149+
createtableoid_parent (aint) with oids;
150+
151+
createtableoid_child () inherits (oid_parent);
152+
select attinhcount, attislocalfrom pg_attribute
153+
where attrelid='oid_child'::regclassand attname='oid';
154+
droptable oid_child;
155+
156+
createtableoid_child (aint) without oids;
157+
altertable oid_child inherit oid_parent;-- fail
158+
altertable oid_childset with oids;
159+
select attinhcount, attislocalfrom pg_attribute
160+
where attrelid='oid_child'::regclassand attname='oid';
161+
altertable oid_child inherit oid_parent;
162+
select attinhcount, attislocalfrom pg_attribute
163+
where attrelid='oid_child'::regclassand attname='oid';
164+
altertable oid_childset without oids;-- fail
165+
altertable oid_parentset without oids;
166+
select attinhcount, attislocalfrom pg_attribute
167+
where attrelid='oid_child'::regclassand attname='oid';
168+
altertable oid_childset without oids;
169+
select attinhcount, attislocalfrom pg_attribute
170+
where attrelid='oid_child'::regclassand attname='oid';
171+
172+
droptable oid_parent cascade;
173+
148174
-- Test non-inheritable parent constraints
149175
createtablep1(ff1int);
150176
altertable p1 addconstraint p1chkcheck (ff1>0) no inherit;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp