Skip to content

Commit 3e2e0d5

Browse files
committed
Set all variable-length fields of pg_attribute to null on column drop
When a column is dropped, the fields attacl, attoptions, and attfdwoptions were kept unchanged. This is probably harmless, but it seems wasteful, and leaves potentially dangling data lying around (for example, attacl could contain references to users that are later also dropped). Change this to set those fields to null when a column is marked as dropped. Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org> Discussion: https://www.postgresql.org/message-id/flat/249d819d-1763-4580-8110-0bf91a0f08b7@eisentraut.org
1 parent e2b73f4 commit 3e2e0d5

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/backend/catalog/heap.c

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,6 +1647,9 @@ RemoveAttributeById(Oid relid, AttrNumber attnum)
16471647
HeapTuple tuple;
16481648
Form_pg_attribute attStruct;
16491649
char newattname[NAMEDATALEN];
1650+
Datum valuesAtt[Natts_pg_attribute] = {0};
1651+
bool nullsAtt[Natts_pg_attribute] = {0};
1652+
bool replacesAtt[Natts_pg_attribute] = {0};
16501653

16511654
/*
16521655
* Grab an exclusive lock on the target table, which we will NOT release
@@ -1695,24 +1698,24 @@ RemoveAttributeById(Oid relid, AttrNumber attnum)
16951698
"........pg.dropped.%d........", attnum);
16961699
namestrcpy(&(attStruct->attname), newattname);
16971700

1698-
/* clear the missing value if any */
1699-
if (attStruct->atthasmissing)
1700-
{
1701-
Datum valuesAtt[Natts_pg_attribute] = {0};
1702-
bool nullsAtt[Natts_pg_attribute] = {0};
1703-
bool replacesAtt[Natts_pg_attribute] = {0};
1704-
1705-
/* update the tuple - set atthasmissing and attmissingval */
1706-
valuesAtt[Anum_pg_attribute_atthasmissing - 1] =
1707-
BoolGetDatum(false);
1708-
replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true;
1709-
valuesAtt[Anum_pg_attribute_attmissingval - 1] = (Datum) 0;
1710-
nullsAtt[Anum_pg_attribute_attmissingval - 1] = true;
1711-
replacesAtt[Anum_pg_attribute_attmissingval - 1] = true;
1712-
1713-
tuple = heap_modify_tuple(tuple, RelationGetDescr(attr_rel),
1714-
valuesAtt, nullsAtt, replacesAtt);
1715-
}
1701+
/* Clear the missing value */
1702+
attStruct->atthasmissing = false;
1703+
nullsAtt[Anum_pg_attribute_attmissingval - 1] = true;
1704+
replacesAtt[Anum_pg_attribute_attmissingval - 1] = true;
1705+
1706+
/*
1707+
* Clear the other variable-length fields. This saves some space in
1708+
* pg_attribute and removes no longer useful information.
1709+
*/
1710+
nullsAtt[Anum_pg_attribute_attacl - 1] = true;
1711+
replacesAtt[Anum_pg_attribute_attacl - 1] = true;
1712+
nullsAtt[Anum_pg_attribute_attoptions - 1] = true;
1713+
replacesAtt[Anum_pg_attribute_attoptions - 1] = true;
1714+
nullsAtt[Anum_pg_attribute_attfdwoptions - 1] = true;
1715+
replacesAtt[Anum_pg_attribute_attfdwoptions - 1] = true;
1716+
1717+
tuple = heap_modify_tuple(tuple, RelationGetDescr(attr_rel),
1718+
valuesAtt, nullsAtt, replacesAtt);
17161719

17171720
CatalogTupleUpdate(attr_rel, &tuple->t_self, tuple);
17181721

0 commit comments

Comments
 (0)