aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/cache/relcache.c
diff options
context:
space:
mode:
authorÁlvaro Herrera <alvherre@alvh.no-ip.org>2025-04-07 19:19:50 +0200
committerÁlvaro Herrera <alvherre@alvh.no-ip.org>2025-04-07 19:19:50 +0200
commita379061a22a8fdf421e1a457cc6af8503def6252 (patch)
tree8b489ce3c8a5d7f9e6f015e0e67d4d7b45c09a39 /src/backend/utils/cache/relcache.c
parentb52a4a5f285df49399fe6deefa1bf63dc88cd3d1 (diff)
downloadpostgresql-a379061a22a8fdf421e1a457cc6af8503def6252.tar.gz
postgresql-a379061a22a8fdf421e1a457cc6af8503def6252.zip
Allow NOT NULL constraints to be added as NOT VALID
This allows them to be added without scanning the table, and validating them afterwards without holding access exclusive lock on the table after any violating rows have been deleted or fixed. Doing ALTER TABLE ... SET NOT NULL for a column that has an invalid not-null constraint validates that constraint. ALTER TABLE .. VALIDATE CONSTRAINT is also supported. There are various checks on whether an invalid constraint is allowed in a child table when the parent table has a valid constraint; this should match what we do for enforced/not enforced constraints. pg_attribute.attnotnull is now only an indicator for whether a not-null constraint exists for the column; whether it's valid or invalid must be queried in pg_constraint. Applications can continue to query pg_attribute.attnotnull as before, but now it's possible that NULL rows are present in the column even when that's set to true. For backend internal purposes, we cache the nullability status in CompactAttribute->attnullability that each tuple descriptor carries (replacing CompactAttribute.attnotnull, which was a mirror of Form_pg_attribute.attnotnull). During the initial tuple descriptor creation, based on the pg_attribute scan, we set this to UNRESTRICTED if pg_attribute.attnotnull is false, or to UNKNOWN if it's true; then we update the latter to VALID or INVALID depending on the pg_constraint scan. This flag is also copied when tupledescs are copied. Comparing tuple descs for equality must also compare the CompactAttribute.attnullability flag and return false in case of a mismatch. pg_dump deals with these constraints by storing the OIDs of invalid not-null constraints in a separate array, and running a query to obtain their properties. The regular table creation SQL omits them entirely. They are then dealt with in the same way as "separate" CHECK constraints, and dumped after the data has been loaded. Because no additional pg_dump infrastructure was required, we don't bump its version number. I decided not to bump catversion either, because the old catalog state works perfectly in the new world. (Trying to run with new catalog state and the old server version would likely run into issues, however.) System catalogs do not support invalid not-null constraints (because commit 14e87ffa5c54 didn't allow them to have pg_constraint rows anyway.) Author: Rushabh Lathia <rushabh.lathia@gmail.com> Author: Jian He <jian.universality@gmail.com> Reviewed-by: Álvaro Herrera <alvherre@alvh.no-ip.org> Tested-by: Ashutosh Bapat <ashutosh.bapat.oss@gmail.com> Discussion: https://postgr.es/m/CAGPqQf0KitkNack4F5CFkFi-9Dqvp29Ro=EpcWt=4_hs-Rt+bQ@mail.gmail.com
Diffstat (limited to 'src/backend/utils/cache/relcache.c')
-rw-r--r--src/backend/utils/cache/relcache.c73
1 files changed, 66 insertions, 7 deletions
diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c
index 18a14ae186e..2905ae86a20 100644
--- a/src/backend/utils/cache/relcache.c
+++ b/src/backend/utils/cache/relcache.c
@@ -307,7 +307,7 @@ static TupleDesc GetPgClassDescriptor(void);
static TupleDesc GetPgIndexDescriptor(void);
static void AttrDefaultFetch(Relation relation, int ndef);
static int AttrDefaultCmp(const void *a, const void *b);
-static void CheckConstraintFetch(Relation relation);
+static void CheckNNConstraintFetch(Relation relation);
static int CheckConstraintCmp(const void *a, const void *b);
static void InitIndexAmRoutine(Relation relation);
static void IndexSupportInitialize(oidvector *indclass,
@@ -684,6 +684,8 @@ RelationBuildTupleDesc(Relation relation)
attrmiss ||
relation->rd_rel->relchecks > 0)
{
+ bool is_catalog = IsCatalogRelation(relation);
+
relation->rd_att->constr = constr;
if (ndef > 0) /* DEFAULTs */
@@ -693,9 +695,33 @@ RelationBuildTupleDesc(Relation relation)
constr->missing = attrmiss;
- if (relation->rd_rel->relchecks > 0) /* CHECKs */
- CheckConstraintFetch(relation);
- else
+ /* CHECK and NOT NULLs */
+ if (relation->rd_rel->relchecks > 0 ||
+ (!is_catalog && constr->has_not_null))
+ CheckNNConstraintFetch(relation);
+
+ /*
+ * Any not-null constraint that wasn't marked invalid by
+ * CheckNNConstraintFetch must necessarily be valid; make it so in the
+ * CompactAttribute array.
+ */
+ if (!is_catalog)
+ {
+ for (int i = 0; i < relation->rd_rel->relnatts; i++)
+ {
+ CompactAttribute *attr;
+
+ attr = TupleDescCompactAttr(relation->rd_att, i);
+
+ if (attr->attnullability == ATTNULLABLE_UNKNOWN)
+ attr->attnullability = ATTNULLABLE_VALID;
+ else
+ Assert(attr->attnullability == ATTNULLABLE_INVALID ||
+ attr->attnullability == ATTNULLABLE_UNRESTRICTED);
+ }
+ }
+
+ if (relation->rd_rel->relchecks == 0)
constr->num_check = 0;
}
else
@@ -3575,6 +3601,14 @@ RelationBuildLocalRelation(const char *relname,
datt->attnotnull = satt->attnotnull;
has_not_null |= satt->attnotnull;
populate_compact_attribute(rel->rd_att, i);
+
+ if (satt->attnotnull)
+ {
+ CompactAttribute *scatt = TupleDescCompactAttr(tupDesc, i);
+ CompactAttribute *dcatt = TupleDescCompactAttr(rel->rd_att, i);
+
+ dcatt->attnullability = scatt->attnullability;
+ }
}
if (has_not_null)
@@ -4533,13 +4567,14 @@ AttrDefaultCmp(const void *a, const void *b)
}
/*
- * Load any check constraints for the relation.
+ * Load any check constraints for the relation, and update not-null validity
+ * of invalid constraints.
*
* As with defaults, if we don't find the expected number of them, just warn
* here. The executor should throw an error if an INSERT/UPDATE is attempted.
*/
static void
-CheckConstraintFetch(Relation relation)
+CheckNNConstraintFetch(Relation relation)
{
ConstrCheck *check;
int ncheck = relation->rd_rel->relchecks;
@@ -4570,7 +4605,31 @@ CheckConstraintFetch(Relation relation)
Datum val;
bool isnull;
- /* We want check constraints only */
+ /*
+ * If this is a not-null constraint, then only look at it if it's
+ * invalid, and if so, mark the TupleDesc entry as known invalid.
+ * Otherwise move on. We'll mark any remaining columns that are still
+ * in UNKNOWN state as known valid later. This allows us not to have
+ * to extract the attnum from this constraint tuple in the vast
+ * majority of cases.
+ */
+ if (conform->contype == CONSTRAINT_NOTNULL)
+ {
+ if (!conform->convalidated)
+ {
+ AttrNumber attnum;
+
+ attnum = extractNotNullColumn(htup);
+ Assert(relation->rd_att->compact_attrs[attnum - 1].attnullability ==
+ ATTNULLABLE_UNKNOWN);
+ relation->rd_att->compact_attrs[attnum - 1].attnullability =
+ ATTNULLABLE_INVALID;
+ }
+
+ continue;
+ }
+
+ /* For what follows, consider check constraints only */
if (conform->contype != CONSTRAINT_CHECK)
continue;