Skip to content

Commit c75c830

Browse files
committed
Fix broken handling of domains in atthasmissing logic.
If a domain type has a default, adding a column of that type (without any explicit DEFAULT clause) failed to install the domain's default value in existing rows, instead leaving the new column null. This is unexpected, and it used to work correctly before v11. The cause is confusion in the atthasmissing mechanism about which default value to install: we'd only consider installing an explicitly-specified default, and then we'd decide that no table rewrite is needed. To fix, take the responsibility for filling attmissingval out of StoreAttrDefault, and instead put it into ATExecAddColumn's existing logic that derives the correct value to fill the new column with. Also, centralize the logic that determines the need for default-related table rewriting there, instead of spreading it over four or five places. In the back branches, we'll leave the attmissingval-filling code in StoreAttrDefault even though it's now dead, for fear that some extension may be depending on that functionality to exist there. A separate HEAD-only patch will clean up the now-useless code. Reported-by: jian he <jian.universality@gmail.com> Author: jian he <jian.universality@gmail.com> Author: Tom Lane <tgl@sss.pgh.pa.us> Discussion: https://postgr.es/m/CACJufxHFssPvkP1we7WMhPD_1kwgbG52o=kQgL+TnVoX5LOyCQ@mail.gmail.com Backpatch-through: 13
1 parent 76fbb38 commit c75c830

File tree

5 files changed

+232
-34
lines changed

5 files changed

+232
-34
lines changed

src/backend/catalog/heap.c

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,60 @@ RelationClearMissing(Relation rel)
21492149
table_close(attr_rel, RowExclusiveLock);
21502150
}
21512151

2152+
/*
2153+
* StoreAttrMissingVal
2154+
*
2155+
* Set the missing value of a single attribute.
2156+
*/
2157+
void
2158+
StoreAttrMissingVal(Relation rel, AttrNumber attnum, Datum missingval)
2159+
{
2160+
Datum valuesAtt[Natts_pg_attribute] = {0};
2161+
bool nullsAtt[Natts_pg_attribute] = {0};
2162+
bool replacesAtt[Natts_pg_attribute] = {0};
2163+
Relation attrrel;
2164+
Form_pg_attribute attStruct;
2165+
HeapTuple atttup,
2166+
newtup;
2167+
2168+
/* This is only supported for plain tables */
2169+
Assert(rel->rd_rel->relkind == RELKIND_RELATION);
2170+
2171+
/* Fetch the pg_attribute row */
2172+
attrrel = table_open(AttributeRelationId, RowExclusiveLock);
2173+
2174+
atttup = SearchSysCache2(ATTNUM,
2175+
ObjectIdGetDatum(RelationGetRelid(rel)),
2176+
Int16GetDatum(attnum));
2177+
if (!HeapTupleIsValid(atttup)) /* shouldn't happen */
2178+
elog(ERROR, "cache lookup failed for attribute %d of relation %u",
2179+
attnum, RelationGetRelid(rel));
2180+
attStruct = (Form_pg_attribute) GETSTRUCT(atttup);
2181+
2182+
/* Make a one-element array containing the value */
2183+
missingval = PointerGetDatum(construct_array(&missingval,
2184+
1,
2185+
attStruct->atttypid,
2186+
attStruct->attlen,
2187+
attStruct->attbyval,
2188+
attStruct->attalign));
2189+
2190+
/* Update the pg_attribute row */
2191+
valuesAtt[Anum_pg_attribute_atthasmissing - 1] = BoolGetDatum(true);
2192+
replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true;
2193+
2194+
valuesAtt[Anum_pg_attribute_attmissingval - 1] = missingval;
2195+
replacesAtt[Anum_pg_attribute_attmissingval - 1] = true;
2196+
2197+
newtup = heap_modify_tuple(atttup, RelationGetDescr(attrrel),
2198+
valuesAtt, nullsAtt, replacesAtt);
2199+
CatalogTupleUpdate(attrrel, &newtup->t_self, newtup);
2200+
2201+
/* clean up */
2202+
ReleaseSysCache(atttup);
2203+
table_close(attrrel, RowExclusiveLock);
2204+
}
2205+
21522206
/*
21532207
* SetAttrMissing
21542208
*
@@ -2305,6 +2359,12 @@ StoreAttrDefault(Relation rel, AttrNumber attnum,
23052359
valuesAtt[Anum_pg_attribute_atthasdef - 1] = true;
23062360
replacesAtt[Anum_pg_attribute_atthasdef - 1] = true;
23072361

2362+
/*
2363+
* Note: this code is dead so far as core Postgres is concerned,
2364+
* because no caller passes add_column_mode = true anymore. We keep
2365+
* it in back branches on the slight chance that some extension is
2366+
* depending on it.
2367+
*/
23082368
if (rel->rd_rel->relkind == RELKIND_RELATION && add_column_mode &&
23092369
!attgenerated)
23102370
{
@@ -2667,13 +2727,8 @@ AddRelationNewConstraints(Relation rel,
26672727
castNode(Const, expr)->constisnull))
26682728
continue;
26692729

2670-
/* If the DEFAULT is volatile we cannot use a missing value */
2671-
if (colDef->missingMode &&
2672-
contain_volatile_functions_after_planning((Expr *) expr))
2673-
colDef->missingMode = false;
2674-
26752730
defOid = StoreAttrDefault(rel, colDef->attnum, expr, is_internal,
2676-
colDef->missingMode);
2731+
false);
26772732

26782733
cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint));
26792734
cooked->contype = CONSTR_DEFAULT;

src/backend/commands/tablecmds.c

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6814,14 +6814,7 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
68146814
rawEnt = (RawColumnDefault *) palloc(sizeof(RawColumnDefault));
68156815
rawEnt->attnum = attribute.attnum;
68166816
rawEnt->raw_default = copyObject(colDef->raw_default);
6817-
6818-
/*
6819-
* Attempt to skip a complete table rewrite by storing the specified
6820-
* DEFAULT value outside of the heap. This may be disabled inside
6821-
* AddRelationNewConstraints if the optimization cannot be applied.
6822-
*/
6823-
rawEnt->missingMode = (!colDef->generated);
6824-
6817+
rawEnt->missingMode = false; /* XXX vestigial */
68256818
rawEnt->generated = colDef->generated;
68266819

68276820
/*
@@ -6833,13 +6826,6 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
68336826

68346827
/* Make the additional catalog changes visible */
68356828
CommandCounterIncrement();
6836-
6837-
/*
6838-
* Did the request for a missing value work? If not we'll have to do a
6839-
* rewrite
6840-
*/
6841-
if (!rawEnt->missingMode)
6842-
tab->rewrite |= AT_REWRITE_DEFAULT_VAL;
68436829
}
68446830

68456831
/*
@@ -6856,9 +6842,9 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
68566842
* rejects nulls. If there are any domain constraints then we construct
68576843
* an explicit NULL default value that will be passed through
68586844
* CoerceToDomain processing. (This is a tad inefficient, since it causes
6859-
* rewriting the table which we really don't have to do, but the present
6860-
* design of domain processing doesn't offer any simple way of checking
6861-
* the constraints more directly.)
6845+
* rewriting the table which we really wouldn't have to do; but we do it
6846+
* to preserve the historical behavior that such a failure will be raised
6847+
* only if the table currently contains some rows.)
68626848
*
68636849
* Note: we use build_column_default, and not just the cooked default
68646850
* returned by AddRelationNewConstraints, so that the right thing happens
@@ -6877,6 +6863,9 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
68776863
*/
68786864
if (RELKIND_HAS_STORAGE(relkind) && attribute.attnum > 0)
68796865
{
6866+
bool has_domain_constraints;
6867+
bool has_missing = false;
6868+
68806869
/*
68816870
* For an identity column, we can't use build_column_default(),
68826871
* because the sequence ownership isn't set yet. So do it manually.
@@ -6889,14 +6878,13 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
68896878
nve->typeId = typeOid;
68906879

68916880
defval = (Expr *) nve;
6892-
6893-
/* must do a rewrite for identity columns */
6894-
tab->rewrite |= AT_REWRITE_DEFAULT_VAL;
68956881
}
68966882
else
68976883
defval = (Expr *) build_column_default(rel, attribute.attnum);
68986884

6899-
if (!defval && DomainHasConstraints(typeOid))
6885+
/* Build CoerceToDomain(NULL) expression if needed */
6886+
has_domain_constraints = DomainHasConstraints(typeOid);
6887+
if (!defval && has_domain_constraints)
69006888
{
69016889
Oid baseTypeId;
69026890
int32 baseTypeMod;
@@ -6922,18 +6910,61 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel,
69226910
{
69236911
NewColumnValue *newval;
69246912

6913+
/* Prepare defval for execution, either here or in Phase 3 */
6914+
defval = expression_planner(defval);
6915+
6916+
/* Add the new default to the newvals list */
69256917
newval = (NewColumnValue *) palloc0(sizeof(NewColumnValue));
69266918
newval->attnum = attribute.attnum;
6927-
newval->expr = expression_planner(defval);
6919+
newval->expr = defval;
69286920
newval->is_generated = (colDef->generated != '\0');
69296921

69306922
tab->newvals = lappend(tab->newvals, newval);
6931-
}
69326923

6933-
if (DomainHasConstraints(typeOid))
6934-
tab->rewrite |= AT_REWRITE_DEFAULT_VAL;
6924+
/*
6925+
* Attempt to skip a complete table rewrite by storing the
6926+
* specified DEFAULT value outside of the heap. This is only
6927+
* allowed for plain relations and non-generated columns, and the
6928+
* default expression can't be volatile (stable is OK). Note that
6929+
* contain_volatile_functions deems CoerceToDomain immutable, but
6930+
* here we consider that coercion to a domain with constraints is
6931+
* volatile; else it might fail even when the table is empty.
6932+
*/
6933+
if (rel->rd_rel->relkind == RELKIND_RELATION &&
6934+
!colDef->generated &&
6935+
!has_domain_constraints &&
6936+
!contain_volatile_functions((Node *) defval))
6937+
{
6938+
EState *estate;
6939+
ExprState *exprState;
6940+
Datum missingval;
6941+
bool missingIsNull;
6942+
6943+
/* Evaluate the default expression */
6944+
estate = CreateExecutorState();
6945+
exprState = ExecPrepareExpr(defval, estate);
6946+
missingval = ExecEvalExpr(exprState,
6947+
GetPerTupleExprContext(estate),
6948+
&missingIsNull);
6949+
/* If it turns out NULL, nothing to do; else store it */
6950+
if (!missingIsNull)
6951+
{
6952+
StoreAttrMissingVal(rel, attribute.attnum, missingval);
6953+
has_missing = true;
6954+
}
6955+
FreeExecutorState(estate);
6956+
}
6957+
else
6958+
{
6959+
/*
6960+
* Failed to use missing mode. We have to do a table rewrite
6961+
* to install the value.
6962+
*/
6963+
tab->rewrite |= AT_REWRITE_DEFAULT_VAL;
6964+
}
6965+
}
69356966

6936-
if (!TupleDescAttr(rel->rd_att, attribute.attnum - 1)->atthasmissing)
6967+
if (!has_missing)
69376968
{
69386969
/*
69396970
* If the new column is NOT NULL, and there is no missing value,

src/include/catalog/heap.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ typedef struct RawColumnDefault
2828
{
2929
AttrNumber attnum; /* attribute to attach default to */
3030
Node *raw_default; /* default value (untransformed parse tree) */
31-
bool missingMode; /* true if part of add column processing */
31+
bool missingMode; /* obsolete, no longer used */
3232
char generated; /* attgenerated setting */
3333
} RawColumnDefault;
3434

@@ -114,6 +114,9 @@ extern List *AddRelationNewConstraints(Relation rel,
114114
const char *queryString);
115115

116116
extern void RelationClearMissing(Relation rel);
117+
118+
extern void StoreAttrMissingVal(Relation rel, AttrNumber attnum,
119+
Datum missingval);
117120
extern void SetAttrMissing(Oid relid, char *attname, char *value);
118121

119122
extern Oid StoreAttrDefault(Relation rel, AttrNumber attnum,

src/test/regress/expected/fast_default.out

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,71 @@ SELECT comp();
245245
(1 row)
246246

247247
DROP TABLE T;
248+
-- Test domains with default value for table rewrite.
249+
CREATE DOMAIN domain1 AS int DEFAULT 11; -- constant
250+
CREATE DOMAIN domain2 AS int DEFAULT 10 + (random() * 10)::int; -- volatile
251+
CREATE DOMAIN domain3 AS text DEFAULT foo(4); -- stable
252+
CREATE DOMAIN domain4 AS text[]
253+
DEFAULT ('{"This", "is", "' || foo(4) || '","the", "real", "world"}')::TEXT[];
254+
CREATE TABLE t2 (a domain1);
255+
INSERT INTO t2 VALUES (1),(2);
256+
-- no table rewrite
257+
ALTER TABLE t2 ADD COLUMN b domain1 default 3;
258+
SELECT attnum, attname, atthasmissing, atthasdef, attmissingval
259+
FROM pg_attribute
260+
WHERE attnum > 0 AND attrelid = 't2'::regclass
261+
ORDER BY attnum;
262+
attnum | attname | atthasmissing | atthasdef | attmissingval
263+
--------+---------+---------------+-----------+---------------
264+
1 | a | f | f |
265+
2 | b | t | t | {3}
266+
(2 rows)
267+
268+
-- table rewrite should happen
269+
ALTER TABLE t2 ADD COLUMN c domain3 default left(random()::text,3);
270+
NOTICE: rewriting table t2 for reason 2
271+
-- no table rewrite
272+
ALTER TABLE t2 ADD COLUMN d domain4;
273+
SELECT attnum, attname, atthasmissing, atthasdef, attmissingval
274+
FROM pg_attribute
275+
WHERE attnum > 0 AND attrelid = 't2'::regclass
276+
ORDER BY attnum;
277+
attnum | attname | atthasmissing | atthasdef | attmissingval
278+
--------+---------+---------------+-----------+-----------------------------------
279+
1 | a | f | f |
280+
2 | b | f | t |
281+
3 | c | f | t |
282+
4 | d | t | f | {"{This,is,abcd,the,real,world}"}
283+
(4 rows)
284+
285+
-- table rewrite should happen
286+
ALTER TABLE t2 ADD COLUMN e domain2;
287+
NOTICE: rewriting table t2 for reason 2
288+
SELECT attnum, attname, atthasmissing, atthasdef, attmissingval
289+
FROM pg_attribute
290+
WHERE attnum > 0 AND attrelid = 't2'::regclass
291+
ORDER BY attnum;
292+
attnum | attname | atthasmissing | atthasdef | attmissingval
293+
--------+---------+---------------+-----------+---------------
294+
1 | a | f | f |
295+
2 | b | f | t |
296+
3 | c | f | t |
297+
4 | d | f | f |
298+
5 | e | f | f |
299+
(5 rows)
300+
301+
SELECT a, b, length(c) = 3 as c_ok, d, e >= 10 as e_ok FROM t2;
302+
a | b | c_ok | d | e_ok
303+
---+---+------+-------------------------------+------
304+
1 | 3 | t | {This,is,abcd,the,real,world} | t
305+
2 | 3 | t | {This,is,abcd,the,real,world} | t
306+
(2 rows)
307+
308+
DROP TABLE t2;
309+
DROP DOMAIN domain1;
310+
DROP DOMAIN domain2;
311+
DROP DOMAIN domain3;
312+
DROP DOMAIN domain4;
248313
DROP FUNCTION foo(INT);
249314
-- Fall back to full rewrite for volatile expressions
250315
CREATE TABLE T(pk INT NOT NULL PRIMARY KEY);

src/test/regress/sql/fast_default.sql

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,50 @@ SELECT comp();
237237

238238
DROP TABLE T;
239239

240+
-- Test domains with default value for table rewrite.
241+
CREATE DOMAIN domain1 AS int DEFAULT 11; -- constant
242+
CREATE DOMAIN domain2 AS int DEFAULT 10 + (random() * 10)::int; -- volatile
243+
CREATE DOMAIN domain3 AS text DEFAULT foo(4); -- stable
244+
CREATE DOMAIN domain4 AS text[]
245+
DEFAULT ('{"This", "is", "' || foo(4) || '","the", "real", "world"}')::TEXT[];
246+
247+
CREATE TABLE t2 (a domain1);
248+
INSERT INTO t2 VALUES (1),(2);
249+
250+
-- no table rewrite
251+
ALTER TABLE t2 ADD COLUMN b domain1 default 3;
252+
253+
SELECT attnum, attname, atthasmissing, atthasdef, attmissingval
254+
FROM pg_attribute
255+
WHERE attnum > 0 AND attrelid = 't2'::regclass
256+
ORDER BY attnum;
257+
258+
-- table rewrite should happen
259+
ALTER TABLE t2 ADD COLUMN c domain3 default left(random()::text,3);
260+
261+
-- no table rewrite
262+
ALTER TABLE t2 ADD COLUMN d domain4;
263+
264+
SELECT attnum, attname, atthasmissing, atthasdef, attmissingval
265+
FROM pg_attribute
266+
WHERE attnum > 0 AND attrelid = 't2'::regclass
267+
ORDER BY attnum;
268+
269+
-- table rewrite should happen
270+
ALTER TABLE t2 ADD COLUMN e domain2;
271+
272+
SELECT attnum, attname, atthasmissing, atthasdef, attmissingval
273+
FROM pg_attribute
274+
WHERE attnum > 0 AND attrelid = 't2'::regclass
275+
ORDER BY attnum;
276+
277+
SELECT a, b, length(c) = 3 as c_ok, d, e >= 10 as e_ok FROM t2;
278+
279+
DROP TABLE t2;
280+
DROP DOMAIN domain1;
281+
DROP DOMAIN domain2;
282+
DROP DOMAIN domain3;
283+
DROP DOMAIN domain4;
240284
DROP FUNCTION foo(INT);
241285

242286
-- Fall back to full rewrite for volatile expressions

0 commit comments

Comments
 (0)