aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/tablecmds.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2017-01-04 18:00:11 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2017-01-04 18:00:11 -0500
commitf64554b99a00ed0fe4097811dfa94265581c27ae (patch)
treea0f4eab2f47a77d8e4c6e17cbc9e113f1953e51a /src/backend/commands/tablecmds.c
parent6e5de703b6c791d355936a61abb52b3b1fc6e184 (diff)
downloadpostgresql-f64554b99a00ed0fe4097811dfa94265581c27ae.tar.gz
postgresql-f64554b99a00ed0fe4097811dfa94265581c27ae.zip
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
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r--src/backend/commands/tablecmds.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 2f5bde60539..3c0bd7b104f 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -10329,6 +10329,39 @@ MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel)
}
}
+ /*
+ * If the parent has an OID column, so must the child, and we'd better
+ * update the child's attinhcount and attislocal the same as for normal
+ * columns. We needn't check data type or not-nullness though.
+ */
+ if (tupleDesc->tdhasoid)
+ {
+ /*
+ * Here we match by column number not name; the match *must* be the
+ * system column, not some random column named "oid".
+ */
+ tuple = SearchSysCacheCopy2(ATTNUM,
+ ObjectIdGetDatum(RelationGetRelid(child_rel)),
+ Int16GetDatum(ObjectIdAttributeNumber));
+ if (HeapTupleIsValid(tuple))
+ {
+ Form_pg_attribute childatt = (Form_pg_attribute) GETSTRUCT(tuple);
+
+ /* See comments above; these changes should be the same */
+ childatt->attinhcount++;
+ simple_heap_update(attrrel, &tuple->t_self, tuple);
+ CatalogUpdateIndexes(attrrel, tuple);
+ heap_freetuple(tuple);
+ }
+ else
+ {
+ ereport(ERROR,
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
+ errmsg("child table is missing column \"%s\"",
+ "oid")));
+ }
+ }
+
heap_close(attrrel, RowExclusiveLock);
}