Re: bug when apply fast default mechanism for adding new column over domain with default value
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: jian he <jian.universality@gmail.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2025-03-03T16:18:46Z
Lists: pgsql-hackers
jian he <jian.universality@gmail.com> writes: > within ATExecAddColumn, we can > if (!missingIsNull) > StoreAttrMissingVal(rel, attribute->attnum, missingval, missingIsNull); > to save some cycles? Probably not really worth it: surely that's going to be a very infrequent edge case. We already eliminated cases with a simple null-Constant default, so we'd only get here with a more complex expression that evaluates to null. > + /* ... and store it. If it's null, nothing is stored. */ > + StoreAttrMissingVal(rel, attribute->attnum, > + missingval, missingIsNull); > + has_missing = !missingIsNull; > + FreeExecutorState(estate); > do we need pfree missingval? I don't see the point. It's not like this code leaks nothing else, and we shouldn't be running in a long-lived memory context. > maybe here add comments mentioning that the identity column needs table rewrite. > since we removed ``tab->rewrite |= AT_REWRITE_DEFAULT_VAL;`` > in the if (colDef->identity) branch. We don't need that because it's not a special case anymore: the NextValueExpr will correctly be seen as a volatile default. The existing code needs that hack because it creates the NextValueExpr after the point at which expression volatility is checked, but that's just poor design. > looking at DefineRelation comments: > * We can set the atthasdef flags now in the tuple descriptor; this just > * saves StoreAttrDefault from having to do an immediate update of the > * pg_attribute rows. > this seems not right? > DefineRelation->heap_create_with_catalog->heap_create->RelationBuildLocalRelation->CreateTupleDescCopy > don't copy atthasdef. > RelationBuildLocalRelation later didn't touch atthasdef. > populate_compact_attribute didn't touch atthasdef. > so StoreAttrDefault has to update that pg_attribute row. You are right: that code has no effect, and if it did have an effect it would be wrong in the case where somebody writes "DEFAULT NULL" explicitly. We'd end with atthasdef set and nothing in pg_attrdef, which would upset various places later. So we should remove that comment and also the adjustments of atthasdef. That seems like an independent patch though. regards, tom lane
Commits
-
Need to do CommandCounterIncrement after StoreAttrMissingVal.
- dd34cbfce296 13.21 landed
- d31d39cfe4f5 14.18 landed
- bd178960c69b 18.0 landed
- 2d6cfb0cddd3 15.13 landed
- 0941aadcd55b 17.5 landed
- 053222a97b13 16.9 landed
-
Simplify some logic around setting pg_attribute.atthasdef.
- 35c8dd9e1176 18.0 landed
-
Remove now-dead code in StoreAttrDefault().
- 4528768d98f8 18.0 landed
-
Fix broken handling of domains in atthasmissing logic.
- 95f650674d2c 18.0 landed
- edc3bccd0dc7 16.9 landed
- d6dd2a02bae0 17.5 landed
- c75c830e2392 14.18 landed
- aac07b56256e 13.21 landed
- 1d180931cc3b 15.13 landed