diff options
Diffstat (limited to 'src/backend/catalog/heap.c')
-rw-r--r-- | src/backend/catalog/heap.c | 62 |
1 files changed, 56 insertions, 6 deletions
diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 4d0e18fb145..2d66639a209 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -69,6 +69,7 @@ #include "pgstat.h" #include "storage/lmgr.h" #include "storage/predicate.h" +#include "utils/array.h" #include "utils/builtins.h" #include "utils/fmgroids.h" #include "utils/inval.h" @@ -2004,6 +2005,60 @@ RelationClearMissing(Relation rel) } /* + * StoreAttrMissingVal + * + * Set the missing value of a single attribute. + */ +void +StoreAttrMissingVal(Relation rel, AttrNumber attnum, Datum missingval) +{ + Datum valuesAtt[Natts_pg_attribute] = {0}; + bool nullsAtt[Natts_pg_attribute] = {0}; + bool replacesAtt[Natts_pg_attribute] = {0}; + Relation attrrel; + Form_pg_attribute attStruct; + HeapTuple atttup, + newtup; + + /* This is only supported for plain tables */ + Assert(rel->rd_rel->relkind == RELKIND_RELATION); + + /* Fetch the pg_attribute row */ + attrrel = table_open(AttributeRelationId, RowExclusiveLock); + + atttup = SearchSysCache2(ATTNUM, + ObjectIdGetDatum(RelationGetRelid(rel)), + Int16GetDatum(attnum)); + if (!HeapTupleIsValid(atttup)) /* shouldn't happen */ + elog(ERROR, "cache lookup failed for attribute %d of relation %u", + attnum, RelationGetRelid(rel)); + attStruct = (Form_pg_attribute) GETSTRUCT(atttup); + + /* Make a one-element array containing the value */ + missingval = PointerGetDatum(construct_array(&missingval, + 1, + attStruct->atttypid, + attStruct->attlen, + attStruct->attbyval, + attStruct->attalign)); + + /* Update the pg_attribute row */ + valuesAtt[Anum_pg_attribute_atthasmissing - 1] = BoolGetDatum(true); + replacesAtt[Anum_pg_attribute_atthasmissing - 1] = true; + + valuesAtt[Anum_pg_attribute_attmissingval - 1] = missingval; + replacesAtt[Anum_pg_attribute_attmissingval - 1] = true; + + newtup = heap_modify_tuple(atttup, RelationGetDescr(attrrel), + valuesAtt, nullsAtt, replacesAtt); + CatalogTupleUpdate(attrrel, &newtup->t_self, newtup); + + /* clean up */ + ReleaseSysCache(atttup); + table_close(attrrel, RowExclusiveLock); +} + +/* * SetAttrMissing * * Set the missing value of a single attribute. This should only be used by @@ -2330,13 +2385,8 @@ AddRelationNewConstraints(Relation rel, castNode(Const, expr)->constisnull)) continue; - /* If the DEFAULT is volatile we cannot use a missing value */ - if (colDef->missingMode && - contain_volatile_functions_after_planning((Expr *) expr)) - colDef->missingMode = false; - defOid = StoreAttrDefault(rel, colDef->attnum, expr, is_internal, - colDef->missingMode); + false); cooked = (CookedConstraint *) palloc(sizeof(CookedConstraint)); cooked->contype = CONSTR_DEFAULT; |