From 58af867b3e3990e787a33c5d5023753e623dffe0 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Tue, 27 Jun 2023 14:43:55 +0200 Subject: [PATCH 03/17] Remove ancient special case code for dropping oid columns The special handling of negative attribute numbers in RemoveAttributeById() was introduced to support SET WITHOUT OIDS (commit 24614a9880). But that feature doesn't exist anymore, so we can revert to the previous, simpler version. --- src/backend/catalog/heap.c | 99 +++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 56 deletions(-) diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 9196dcd39f..4c30c7d461 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -1666,68 +1666,56 @@ RemoveAttributeById(Oid relid, AttrNumber attnum) attnum, relid); attStruct = (Form_pg_attribute) GETSTRUCT(tuple); - if (attnum < 0) - { - /* System attribute (probably OID) ... just delete the row */ - - CatalogTupleDelete(attr_rel, &tuple->t_self); - } - else - { - /* Dropping user attributes is lots harder */ + /* Mark the attribute as dropped */ + attStruct->attisdropped = true; - /* Mark the attribute as dropped */ - attStruct->attisdropped = true; - - /* - * Set the type OID to invalid. A dropped attribute's type link - * cannot be relied on (once the attribute is dropped, the type might - * be too). Fortunately we do not need the type row --- the only - * really essential information is the type's typlen and typalign, - * which are preserved in the attribute's attlen and attalign. We set - * atttypid to zero here as a means of catching code that incorrectly - * expects it to be valid. - */ - attStruct->atttypid = InvalidOid; - - /* Remove any NOT NULL constraint the column may have */ - attStruct->attnotnull = false; + /* + * Set the type OID to invalid. A dropped attribute's type link cannot be + * relied on (once the attribute is dropped, the type might be too). + * Fortunately we do not need the type row --- the only really essential + * information is the type's typlen and typalign, which are preserved in + * the attribute's attlen and attalign. We set atttypid to zero here as a + * means of catching code that incorrectly expects it to be valid. + */ + attStruct->atttypid = InvalidOid; - /* We don't want to keep stats for it anymore */ - attStruct->attstattarget = 0; + /* Remove any NOT NULL constraint the column may have */ + attStruct->attnotnull = false; - /* Unset this so no one tries to look up the generation expression */ - attStruct->attgenerated = '\0'; + /* We don't want to keep stats for it anymore */ + attStruct->attstattarget = 0; - /* - * Change the column name to something that isn't likely to conflict - */ - snprintf(newattname, sizeof(newattname), - "........pg.dropped.%d........", attnum); - namestrcpy(&(attStruct->attname), newattname); + /* Unset this so no one tries to look up the generation expression */ + attStruct->attgenerated = '\0'; - /* clear the missing value if any */ - if (attStruct->atthasmissing) - { - Datum valuesAtt[Natts_pg_attribute] = {0}; - bool nullsAtt[Natts_pg_attribute] = {0}; - bool replacesAtt[Natts_pg_attribute] = {0}; - - /* update the tuple - set atthasmissing and attmissingval */ - valuesAtt[Anum_pg_attribute_atthasmissing - 1] = - BoolGetDatum(false); - replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true; - valuesAtt[Anum_pg_attribute_attmissingval - 1] = (Datum) 0; - nullsAtt[Anum_pg_attribute_attmissingval - 1] = true; - replacesAtt[Anum_pg_attribute_attmissingval - 1] = true; - - tuple = heap_modify_tuple(tuple, RelationGetDescr(attr_rel), - valuesAtt, nullsAtt, replacesAtt); - } + /* + * Change the column name to something that isn't likely to conflict + */ + snprintf(newattname, sizeof(newattname), + "........pg.dropped.%d........", attnum); + namestrcpy(&(attStruct->attname), newattname); - CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple); + /* clear the missing value if any */ + if (attStruct->atthasmissing) + { + Datum valuesAtt[Natts_pg_attribute] = {0}; + bool nullsAtt[Natts_pg_attribute] = {0}; + bool replacesAtt[Natts_pg_attribute] = {0}; + + /* update the tuple - set atthasmissing and attmissingval */ + valuesAtt[Anum_pg_attribute_atthasmissing - 1] = + BoolGetDatum(false); + replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true; + valuesAtt[Anum_pg_attribute_attmissingval - 1] = (Datum) 0; + nullsAtt[Anum_pg_attribute_attmissingval - 1] = true; + replacesAtt[Anum_pg_attribute_attmissingval - 1] = true; + + tuple = heap_modify_tuple(tuple, RelationGetDescr(attr_rel), + valuesAtt, nullsAtt, replacesAtt); } + CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple); + /* * Because updating the pg_attribute row will trigger a relcache flush for * the target relation, we need not do anything else to notify other @@ -1736,8 +1724,7 @@ RemoveAttributeById(Oid relid, AttrNumber attnum) table_close(attr_rel, RowExclusiveLock); - if (attnum > 0) - RemoveStatistics(relid, attnum); + RemoveStatistics(relid, attnum); relation_close(rel, NoLock); } -- 2.41.0