1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-24 00:23:06 +03:00

Fix assorted bugs related to identity column in partitioned tables

When changing the data type of a column of a partitioned table, craft
the ALTER SEQUENCE command only once.  Partitions do not have identity
sequences of their own and thus do not need a ALTER SEQUENCE command
for each partition.

Fix getIdentitySequence() to fetch the identity sequence associated
with the top-level partitioned table when a Relation of a partition is
passed to it.  While doing so, translate the attribute number of the
partition into the attribute number of the partitioned table.

Author: Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>
Reported-by: Alexander Lakhin <exclusion@gmail.com>
Reviewed-by: Dmitry Dolgov <9erthalion6@gmail.com>
Discussion: https://www.postgresql.org/message-id/3b8a9dc1-bbc7-0ef5-6863-c432afac7d59@gmail.com
This commit is contained in:
Peter Eisentraut
2024-05-07 22:42:32 +02:00
parent 832c4f657f
commit 509199587d
7 changed files with 100 additions and 47 deletions

View File

@@ -23,10 +23,12 @@
#include "catalog/pg_constraint.h"
#include "catalog/pg_depend.h"
#include "catalog/pg_extension.h"
#include "catalog/partition.h"
#include "commands/extension.h"
#include "miscadmin.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/syscache.h"
#include "utils/rel.h"
@@ -941,10 +943,35 @@ getOwnedSequences(Oid relid)
* Get owned identity sequence, error if not exactly one.
*/
Oid
getIdentitySequence(Oid relid, AttrNumber attnum, bool missing_ok)
getIdentitySequence(Relation rel, AttrNumber attnum, bool missing_ok)
{
List *seqlist = getOwnedSequences_internal(relid, attnum, DEPENDENCY_INTERNAL);
Oid relid;
List *seqlist;
/*
* The identity sequence is associated with the topmost partitioned table,
* which might have column order different than the given partition.
*/
if (RelationGetForm(rel)->relispartition)
{
List *ancestors =
get_partition_ancestors(RelationGetRelid(rel));
HeapTuple ctup = SearchSysCacheAttNum(RelationGetRelid(rel), attnum);
const char *attname = NameStr(((Form_pg_attribute) GETSTRUCT(ctup))->attname);
HeapTuple ptup;
relid = llast_oid(ancestors);
ptup = SearchSysCacheAttName(relid, attname);
attnum = ((Form_pg_attribute) GETSTRUCT(ptup))->attnum;
ReleaseSysCache(ctup);
ReleaseSysCache(ptup);
list_free(ancestors);
}
else
relid = RelationGetRelid(rel);
seqlist = getOwnedSequences_internal(relid, attnum, DEPENDENCY_INTERNAL);
if (list_length(seqlist) > 1)
elog(ERROR, "more than one owned sequence found");
else if (seqlist == NIL)