From 61f08c016322ce44b2d35ff5c569d495371a3322 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Fri, 19 Jan 2018 10:15:08 -0300 Subject: [PATCH] Fix StoreCatalogInheritance1 to use 32bit inhseqno MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit For no apparent reason, this function was using a 16bit-wide inhseqno value, rather than the correct 32 bit width which is what is stored in the pg_inherits catalog. This becomes evident if you try to create a table with more than 65535 parents, because this error appears: ERROR: duplicate key value violates unique constraint «pg_inherits_relid_seqno_index» DETAIL: Key (inhrelid, inhseqno)=(329371, 0) already exists. Needless to say, having so many parents is an uncommon situations, which explains why this error has never been reported despite being having been introduced with the Postgres95 1.01 sources in commit d31084e9d111: https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/commands/creatinh.c;hb=d31084e9d111#l349 Backpatch all the way back. David Rowley noticed this while reviewing a patch of mine. Discussion: https://postgr.es/m/CAKJS1f8Dn7swSEhOWwzZzssW7747YB=2Hi+T7uGud40dur69-g@mail.gmail.com --- src/backend/commands/tablecmds.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 18c9f21617..d22a9d1e1f 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -301,7 +301,7 @@ static void MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel static void StoreCatalogInheritance(Oid relationId, List *supers, bool child_is_partition); static void StoreCatalogInheritance1(Oid relationId, Oid parentOid, - int16 seqNumber, Relation inhRelation, + int32 seqNumber, Relation inhRelation, bool child_is_partition); static int findAttrByName(const char *attributeName, List *schema); static void AlterIndexNamespaces(Relation classRel, Relation rel, @@ -2300,7 +2300,7 @@ StoreCatalogInheritance(Oid relationId, List *supers, bool child_is_partition) { Relation relation; - int16 seqNumber; + int32 seqNumber; ListCell *entry; /* @@ -2341,7 +2341,7 @@ StoreCatalogInheritance(Oid relationId, List *supers, */ static void StoreCatalogInheritance1(Oid relationId, Oid parentOid, - int16 seqNumber, Relation inhRelation, + int32 seqNumber, Relation inhRelation, bool child_is_partition) { TupleDesc desc = RelationGetDescr(inhRelation); @@ -2356,7 +2356,7 @@ StoreCatalogInheritance1(Oid relationId, Oid parentOid, */ values[Anum_pg_inherits_inhrelid - 1] = ObjectIdGetDatum(relationId); values[Anum_pg_inherits_inhparent - 1] = ObjectIdGetDatum(parentOid); - values[Anum_pg_inherits_inhseqno - 1] = Int16GetDatum(seqNumber); + values[Anum_pg_inherits_inhseqno - 1] = Int32GetDatum(seqNumber); memset(nulls, 0, sizeof(nulls)); -- 2.40.0