From 5f1f4bd9518a9c10c74de775125ea3549ba38d8c Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Sat, 23 Dec 2023 11:41:17 +0100 Subject: [PATCH v2 2/3] WIP: Generalize handling of nullable pg_attribute columns in DDL DDL code uses tuple descriptors to pass around pg_attribute values during table and index creation. But tuple descriptors don't include the variable-length/nullable columns of pg_attribute, so they have to be handled separately. Right now, the attoptions field is handled in a one-off way with a separate argument passed to InsertPgAttributeTuples(). The other affected fields of pg_attribute are right now not needed at relation creation time. The goal of this patch is to generalize this to allow handling additional variable-length/nullable columns of pg_attribute in a similar manner. For that, create a new struct Form_pg_attribute_extra, which is to be passed around in parallel to the tuple descriptor and optionally supplies the additional columns. Right now, this struct only contains one field for attoptions, so no functionality is actually changed by this. --- src/backend/catalog/heap.c | 12 +++++++++--- src/backend/catalog/index.c | 16 +++++++++++++++- src/include/catalog/heap.h | 2 +- src/include/catalog/pg_attribute.h | 7 +++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 52b4485c4b..40b23fbead 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -697,7 +697,7 @@ void InsertPgAttributeTuples(Relation pg_attribute_rel, TupleDesc tupdesc, Oid new_rel_oid, - const Datum *attoptions, + const FormData_pg_attribute_extra tupdesc_extra[], CatalogIndexState indstate) { TupleTableSlot **slot; @@ -719,6 +719,7 @@ InsertPgAttributeTuples(Relation pg_attribute_rel, while (natts < tupdesc->natts) { Form_pg_attribute attrs = TupleDescAttr(tupdesc, natts); + const FormData_pg_attribute_extra *attrs_extra = tupdesc_extra ? &tupdesc_extra[natts] : NULL; ExecClearTuple(slot[slotCount]); @@ -750,10 +751,15 @@ InsertPgAttributeTuples(Relation pg_attribute_rel, slot[slotCount]->tts_values[Anum_pg_attribute_attislocal - 1] = BoolGetDatum(attrs->attislocal); slot[slotCount]->tts_values[Anum_pg_attribute_attinhcount - 1] = Int16GetDatum(attrs->attinhcount); slot[slotCount]->tts_values[Anum_pg_attribute_attcollation - 1] = ObjectIdGetDatum(attrs->attcollation); - if (attoptions && attoptions[natts] != (Datum) 0) - slot[slotCount]->tts_values[Anum_pg_attribute_attoptions - 1] = attoptions[natts]; + if (attrs_extra) + { + slot[slotCount]->tts_values[Anum_pg_attribute_attoptions - 1] = attrs_extra->attoptions.value; + slot[slotCount]->tts_isnull[Anum_pg_attribute_attoptions - 1] = attrs_extra->attoptions.isnull; + } else + { slot[slotCount]->tts_isnull[Anum_pg_attribute_attoptions - 1] = true; + } /* * The remaining fields are not set for new columns. diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index b2759df311..eaeae568a3 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -517,6 +517,20 @@ AppendAttributeTuples(Relation indexRelation, const Datum *attopts) Relation pg_attribute; CatalogIndexState indstate; TupleDesc indexTupDesc; + FormData_pg_attribute_extra *attrs_extra = NULL; + + if (attopts) + { + attrs_extra = palloc0_array(FormData_pg_attribute_extra, indexRelation->rd_att->natts); + + for (int i = 0; i < indexRelation->rd_att->natts; i++) + { + if (attopts[i]) + attrs_extra[i].attoptions.value = attopts[i]; + else + attrs_extra[i].attoptions.isnull = true; + } + } /* * open the attribute relation and its indexes @@ -530,7 +544,7 @@ AppendAttributeTuples(Relation indexRelation, const Datum *attopts) */ indexTupDesc = RelationGetDescr(indexRelation); - InsertPgAttributeTuples(pg_attribute, indexTupDesc, InvalidOid, attopts, indstate); + InsertPgAttributeTuples(pg_attribute, indexTupDesc, InvalidOid, attrs_extra, indstate); CatalogCloseIndexes(indstate); diff --git a/src/include/catalog/heap.h b/src/include/catalog/heap.h index 51f7b12aa3..dbd65c4d5e 100644 --- a/src/include/catalog/heap.h +++ b/src/include/catalog/heap.h @@ -98,7 +98,7 @@ extern List *heap_truncate_find_FKs(List *relationIds); extern void InsertPgAttributeTuples(Relation pg_attribute_rel, TupleDesc tupdesc, Oid new_rel_oid, - const Datum *attoptions, + const FormData_pg_attribute_extra tupdesc_extra[], CatalogIndexState indstate); extern void InsertPgClassTuple(Relation pg_class_desc, diff --git a/src/include/catalog/pg_attribute.h b/src/include/catalog/pg_attribute.h index 337a2d5bf9..7336d7f2fd 100644 --- a/src/include/catalog/pg_attribute.h +++ b/src/include/catalog/pg_attribute.h @@ -208,6 +208,13 @@ CATALOG(pg_attribute,1249,AttributeRelationId) BKI_BOOTSTRAP BKI_ROWTYPE_OID(75, */ typedef FormData_pg_attribute *Form_pg_attribute; +typedef struct FormData_pg_attribute_extra +{ + NullableDatum attoptions; +} FormData_pg_attribute_extra; + +typedef FormData_pg_attribute_extra *Form_pg_attribute_extra; + DECLARE_UNIQUE_INDEX(pg_attribute_relid_attnam_index, 2658, AttributeRelidNameIndexId, pg_attribute, btree(attrelid oid_ops, attname name_ops)); DECLARE_UNIQUE_INDEX_PKEY(pg_attribute_relid_attnum_index, 2659, AttributeRelidNumIndexId, pg_attribute, btree(attrelid oid_ops, attnum int2_ops)); -- 2.43.0