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

Commitd86f400

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 parent44f7afb commitd86f400

File tree

3 files changed

+117
-2
lines changed

3 files changed

+117
-2
lines changed

‎src/backend/commands/tablecmds.c

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,8 +1605,8 @@ MergeAttributes(List *schema, List *supers, char relpersistence,
16051605
* execute if the user attempts to create a table with hundreds of
16061606
* thousands of columns.
16071607
*
1608-
* Note that we also need to check that we do not exceed this figure
1609-
*afterincluding columns from inherited relations.
1608+
* Note that we also need to check that we do not exceed this figure after
1609+
* including columns from inherited relations.
16101610
*/
16111611
if (list_length(schema)>MaxHeapAttributeNumber)
16121612
ereport(ERROR,
@@ -10902,6 +10902,46 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
1090210902
}
1090310903
}
1090410904

10905+
/*
10906+
* If the parent has an OID column, so must the child, and we'd better
10907+
* update the child's attinhcount and attislocal the same as for normal
10908+
* columns. We needn't check data type or not-nullness though.
10909+
*/
10910+
if (tupleDesc->tdhasoid)
10911+
{
10912+
/*
10913+
* Here we match by column number not name; the match *must* be the
10914+
* system column, not some random column named "oid".
10915+
*/
10916+
tuple=SearchSysCacheCopy2(ATTNUM,
10917+
ObjectIdGetDatum(RelationGetRelid(child_rel)),
10918+
Int16GetDatum(ObjectIdAttributeNumber));
10919+
if (HeapTupleIsValid(tuple))
10920+
{
10921+
Form_pg_attributechildatt= (Form_pg_attribute)GETSTRUCT(tuple);
10922+
10923+
/* See comments above; these changes should be the same */
10924+
childatt->attinhcount++;
10925+
10926+
if (child_is_partition)
10927+
{
10928+
Assert(childatt->attinhcount==1);
10929+
childatt->attislocal= false;
10930+
}
10931+
10932+
simple_heap_update(attrrel,&tuple->t_self,tuple);
10933+
CatalogUpdateIndexes(attrrel,tuple);
10934+
heap_freetuple(tuple);
10935+
}
10936+
else
10937+
{
10938+
ereport(ERROR,
10939+
(errcode(ERRCODE_DATATYPE_MISMATCH),
10940+
errmsg("child table is missing column \"%s\"",
10941+
"oid")));
10942+
}
10943+
}
10944+
1090510945
heap_close(attrrel,RowExclusiveLock);
1090610946
}
1090710947

‎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