pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Baji Shaik <baji.pgdev@gmail.com>
To: jian he <jian.universality@gmail.com>
Cc: Álvaro Herrera <alvherre@kurilemu.de>
Cc: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: Re: using index to speedup add not null constraints to a table
Date: Tue, 17 Nov 2026 12:28:58 -0600
Message-ID: <CA+fm-RPCcMxpHVJrUZWKwMPtX_D2eq=2ArAqU7U1GsJrGyiCDg@mail.gmail.com> (raw)
In-Reply-To: <CACJufxEkSyhOotK0b3hj_D3c9HO=dxGoGxL3Cx4j6enG_t_P6Q@mail.gmail.com>
References: <CACJufxE-+VSkxMvBRbpM45iex=7ws4-km4MvmOBPXtTJ7jjdQA@mail.gmail.com>
	<202604070954.qesyoeqbnvm6@alvherre.pgsql>
	<CACJufxGr=t0N1C87ZgKD4pEQ42mFjS+jEVW=s=D_Mu8i0dnzLA@mail.gmail.com>
	<CACJufxFaNH_LVHi8cd+AkKS4Jx7ihbyBEN9tyCxSW=MZPVTS6A@mail.gmail.com>
	<CACJufxEkSyhOotK0b3hj_D3c9HO=dxGoGxL3Cx4j6enG_t_P6Q@mail.gmail.com>

Hello Jian,

On Tue, Sep 29, 2026 at 7:02 PM jian he <jian.universality@gmail.com> wrote:

> Hi.
> Please check the attached v16.
>

I rebased v16 against current master and ran it through some
correctness tests, including the indcheckxmin/HOT scenario your own
test covers. Looks good to me.

Alvaro's point about checking for generated columns before computing
notnull_attrs still looks open; the other point (verifying every
column has a usable index before scanning any) looks addressed now in
index_check_notnull().

One small thing I noticed in index_check_notnull():

+ attnum = (indexStruct->indkey.values[0]);
+
+ if (list_member_int(notnull_attnums, attnum) &&
+ !list_member_int(attnums, attnum))
+ {
+ attnums = lappend_int(attnums, attnum);
+ idxs = lappend_oid(idxs, indexoid);
+ }

This takes the first qualifying index per column in
RelationGetIndexList() order, with no preference for e.g. the primary
key or the narrowest index. Was that a deliberate simplification, or
worth tightening later?

Separately, I've been working on a related patch: ALTER TABLE ...
VALIDATE CONSTRAINT also always does a full table scan to check a
NOT VALID CHECK constraint, even when an existing btree index on the
column already proves it. For a simple "column OP constant" CHECK on a
NOT NULL column that's the leading column of a btree index, it scans
the index for the negated condition instead, falling back to the
existing full scan otherwise. Same underlying idea as this patch (use
an existing index to skip a validation scan), just for CHECK instead
of NOT NULL, so it seemed worth raising here rather than as a separate
thread. Attached are v16 (unchanged) and v1 of mine as 0002.

Thoughts?

Thanks,
Baji Shaik

Attachments: download all (2 files, .tar.gz)

v16-0001-using-indexscan-to-speedup-add-not-null-constraints.patch (application/octet-stream, 20.8K) download From d973939a318ed0d5346c5e11e8fca49f8a1a5e1d Mon Sep 17 00:00:00 2001 From: jian he <jian.universality@gmail.com> Date: Tue, 29 Sep 2026 15:21:31 +0800 Subject: [PATCH v16 1/1] using indexscan to speedup add not null constraints This patch tries to use index_beginscan() / table_index_getnext_slot() / index_endscan() mentioned in [1] to speedup adding not-null constraints to the existing table. The main logic happens in phase 3 ATRewriteTable->index_check_notnull 1. collect all not-null constraints. 2. For each not-null constraint, check whether there is a corresponding index available for validation. If not, then cannot use indexscan mechanism to verify not-null constraints. 3. If any of the following conditions are true * table scan or rewrite is required, * table wasn't locked with `AccessExclusiveLock` * the NOT NULL constraint applies to a virtual generated column then we cannot use indexscan to fast vertify column is not null or not. If all conditions are satisfied, attempt to use indexscan to verify whether the column contains any NULL values. concurrency concern: Under AccessExclusiveLock no other transaction can be changing the table. A fresh MVCC snapshot, GetLatestSnapshot(), therefore sees exactly the rows the full table scan would check. Nothing can add or change a row before we commit, so if the index scan finds no NULL, the table scan wouldn't find one either. As with the table scan, rows already deleted but still visible to older snapshots aren't checked. references: [1] https://postgr.es/m/CA%2BTgmoa5NKz8iGW_9v7wz%3D-%2BzQFu%3DE4SZoaTaU1znLaEXRYp-Q%40mail.gmail.com [2] https://postgr.es/m/900056D1-32DF-4927-8251-3E0C0DC407FD%40anarazel.de discussion: https://postgr.es/m/CACJufxFiW=4k1is=F1J=r-Cx1RuByXQPUrWB331U47rSnGz+hw@mail.gmail.com commitfest entry: https://commitfest.postgresql.org/patch/5444 --- src/backend/commands/tablecmds.c | 277 +++++++++++++++++- src/test/modules/test_misc/meson.build | 1 + .../t/016_indexscan_validate_notnull.pl | 145 +++++++++ 3 files changed, 413 insertions(+), 10 deletions(-) create mode 100644 src/test/modules/test_misc/t/016_indexscan_validate_notnull.pl diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 0274d892f2e..c2a4d402597 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -212,16 +212,19 @@ typedef struct AlteredTableInfo List *changedStatisticsOwners; /* owners of same */ } AlteredTableInfo; -/* Struct describing one new constraint to check in Phase 3 scan */ -/* Note: new not-null constraints are handled elsewhere */ +/* + * Struct describing one new constraint to check in Phase 3 scan. Note: new + * not-null constraints were also added to Phase 3. + */ typedef struct NewConstraint { char *name; /* Constraint name, or NULL if none */ - ConstrType contype; /* CHECK or FOREIGN */ + ConstrType contype; /* CHECK or FOREIGN or NOT NULL */ Oid refrelid; /* PK rel, if FOREIGN */ Oid refindid; /* OID of PK's index, if FOREIGN */ bool conwithperiod; /* Whether the new FOREIGN KEY uses PERIOD */ Oid conid; /* OID of pg_constraint entry, if FOREIGN */ + int attnum; /* NOT NULL constraint attribute number */ Node *qual; /* Check expr or CONSTR_FOREIGN Constraint */ ExprState *qualstate; /* Execution state for CHECK expr */ } NewConstraint; @@ -757,6 +760,25 @@ static List *GetParentedForeignKeyRefs(Relation partition); static void ATDetachCheckNoForeignKeyRefs(Relation partition); static char GetAttributeCompression(Oid atttypid, const char *compression); static char GetAttributeStorage(Oid atttypid, const char *storagemode); +static bool index_check_notnull(Relation relation, List *notnull_attnums); + +/* + * Ask phase 3 to verify that column attnum contains no NULLs. + * + * All requests for NOT NULL verification must come through here: + * ATRewriteTable may skip the table scan after checking, via an index, + * only the columns queued in tab->constraints. + */ +static void +queue_notnull_validation(AlteredTableInfo *tab, AttrNumber attnum) +{ + NewConstraint *newcon = palloc0_object(NewConstraint); + + newcon->contype = CONSTR_NOTNULL; + newcon->attnum = attnum; + tab->constraints = lappend(tab->constraints, newcon); + tab->verify_new_notnull = true; +} /* ---------------------------------------------------------------- @@ -6252,6 +6274,9 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap) needscan = true; con->qualstate = ExecPrepareExpr((Expr *) expand_generated_columns_in_expr(con->qual, oldrel, 1), estate); break; + case CONSTR_NOTNULL: + /* Nothing to do here */ + break; case CONSTR_FOREIGN: /* Nothing to do here */ break; @@ -6279,6 +6304,77 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap) notnull_attrs = notnull_virtual_attrs = NIL; if (newrel || tab->verify_new_notnull) { + bool verified_by_index = false; + + /* + * The conditions using indexscan mechanism fast verifying not-null + * constraints are quite strict. All of the following conditions must + * be met. + * + * 1. AlteredTableInfo->verify_new_notnull is true. + * + * 2. No table scan (e.g., for CHECK constraint verification) or table + * rewrite is expected later, if one is, using indexscan would wastes + * cycles. + * + * 3. Indexes cannot be created on virtual generated columns, so fast + * checking not-null constraints is not applicable to them. + * + * 4. The relation must be a plain table. + * + * 5. under AccessExclusiveLock no other transaction is in progress, + * so the index scan sees exactly the rows the table scan would. + */ + if (!needscan && + newrel == NULL && + oldrel->rd_rel->relkind == RELKIND_RELATION && + CheckRelationLockedByMe(oldrel, AccessExclusiveLock, false)) + { + List *notnull_attnums = NIL; + + Assert(!tab->rewrite); + + foreach(l, tab->constraints) + { + Form_pg_attribute attr; + NewConstraint *con = lfirst(l); + + if (con->contype != CONSTR_NOTNULL) + continue; + + attr = TupleDescAttr(newTupDesc, con->attnum - 1); + + if (attr->attisdropped) + continue; + + Assert(attr->attnotnull); + Assert(attr->attnum == con->attnum); + + if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL) + { + needscan = true; + break; + } + + notnull_attnums = list_append_unique_int(notnull_attnums, + attr->attnum); + } + + if (!needscan && notnull_attnums != NIL) + { + if (!index_check_notnull(oldrel, notnull_attnums)) + needscan = true; + else + { + verified_by_index = true; + + ereport(DEBUG1, + (errmsg_internal("all new not-null constraints on relation \"%s\" have been validated by using index scan", + RelationGetRelationName(oldrel)))); + } + } + } + /* * If we are rebuilding the tuples OR if we added any new but not * verified not-null constraints, check all *valid* not-null @@ -6289,7 +6385,7 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap) * not-null constraints over virtual generated columns; instead, they * are collected in notnull_virtual_attrs for verification elsewhere. */ - for (i = 0; i < newTupDesc->natts; i++) + for (i = 0; !verified_by_index && i < newTupDesc->natts; i++) { CompactAttribute *attr = TupleDescCompactAttr(newTupDesc, i); @@ -6307,6 +6403,12 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap) } if (notnull_attrs || notnull_virtual_attrs) needscan = true; + + if (verified_by_index) + { + Assert(needscan == false); + Assert(newrel == NULL); + } } if (newrel || needscan) @@ -7648,13 +7750,13 @@ ATExecAddColumn(List **wqueue, AlteredTableInfo *tab, Relation rel, } } - if (!has_missing) + if (!has_missing && colDef->is_not_null) { /* * If the new column is NOT NULL, and there is no missing value, * tell Phase 3 it needs to check for NULLs. */ - tab->verify_new_notnull |= colDef->is_not_null; + queue_notnull_validation(tab, attribute->attnum); } } @@ -7960,7 +8062,8 @@ set_attnotnull(List **wqueue, Relation rel, AttrNumber attnum, AlteredTableInfo *tab; tab = ATGetQueueEntry(wqueue, rel); - tab->verify_new_notnull = true; + + queue_notnull_validation(tab, attnum); } CommandCounterIncrement(); @@ -8719,7 +8822,7 @@ ATExecSetExpression(AlteredTableInfo *tab, Relation rel, const char *colName, colName, RelationGetRelationName(rel)))); if (attgenerated == ATTRIBUTE_GENERATED_VIRTUAL && attTup->attnotnull) - tab->verify_new_notnull = true; + queue_notnull_validation(tab, attnum); /* * We need to prevent this because a change of expression could affect a @@ -10030,13 +10133,15 @@ ATAddCheckNNConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel, { CookedConstraint *ccon = (CookedConstraint *) lfirst(lcon); - if (!ccon->skip_validation && ccon->contype != CONSTR_NOTNULL) + if (!ccon->skip_validation) { NewConstraint *newcon; newcon = palloc0_object(NewConstraint); newcon->name = ccon->name; newcon->contype = ccon->contype; + if (ccon->contype == CONSTR_NOTNULL) + newcon->attnum = ccon->attnum; newcon->qual = ccon->expr; tab->constraints = lappend(tab->constraints, newcon); @@ -13769,7 +13874,14 @@ QueueNNConstraintValidation(List **wqueue, Relation conrel, Relation rel, set_attnotnull(NULL, rel, attnum, false); tab = ATGetQueueEntry(wqueue, rel); - tab->verify_new_notnull = true; + + /* + * Queue validation for phase 3. Like ALTER TABLE SET NOT NULL, also add + * a NewConstraint to AlteredTableInfo->constraints: when ATRewriteTable + * verifies new not-null constraints with an index scan, it checks the + * columns queued there. + */ + queue_notnull_validation(tab, attnum); /* * Invalidate relcache so that others see the new validated constraint. @@ -22822,3 +22934,148 @@ GetAttributeStorage(Oid atttypid, const char *storagemode) return cstorage; } + + /* + * Try to verify newly added NOT NULL constraints using index scans. + * + * notnull_attnums: lists the columns of the newly added NOT NULL + * constraints. Each must be the leading column of a usable btree index, + * which we then scan for NULL entries. + * + * Returns true means the new NOT NULL constraints are fully verified and no + * extra table scans are necessary. + */ +static bool +index_check_notnull(Relation relation, List *notnull_attnums) +{ + List *idxs = NIL; + List *attnums = NIL; + bool found_null = false; + ListCell *lc, + *lc2; + Snapshot snapshot; + List *indexes; + Relation indexRel; + + Assert(notnull_attnums != NIL); + + indexes = RelationGetIndexList(relation); + foreach_oid(indexoid, indexes) + { + AttrNumber attnum; + Form_pg_index indexStruct; + + indexRel = index_open(indexoid, AccessShareLock); + indexStruct = indexRel->rd_index; + + /* + * We only use non-deferrable, valid, and live b-tree indexes to + * verify NOT NULL constraints. + */ + if (!indexStruct->indimmediate || + !indexStruct->indisvalid || + !indexStruct->indislive || + indexRel->rd_rel->relam != BTREE_AM_OID) + { + index_close(indexRel, NoLock); + continue; + } + + /* cannot use expression index or partial index too */ + if (!heap_attisnull(indexRel->rd_indextuple, Anum_pg_index_indexprs, NULL) || + !heap_attisnull(indexRel->rd_indextuple, Anum_pg_index_indpred, NULL)) + { + index_close(indexRel, NoLock); + continue; + } + + /* + * If the index is valid but cannot be used yet, ignore it. See + * src/backend/access/heap/README.HOT for discussion. + */ + if (indexStruct->indcheckxmin && + !TransactionIdPrecedes(HeapTupleHeaderGetXmin(indexRel->rd_indextuple->t_data), + TransactionXmin)) + { + index_close(indexRel, NoLock); + continue; + } + + attnum = (indexStruct->indkey.values[0]); + + if (list_member_int(notnull_attnums, attnum) && + !list_member_int(attnums, attnum)) + { + attnums = lappend_int(attnums, attnum); + idxs = lappend_oid(idxs, indexoid); + } + index_close(indexRel, NoLock); + } + + /* + * Verify NOT NULL constraints using a suitable index, falling back to a + * full table scan if a suitable index is not present. + */ + if (list_length(notnull_attnums) != list_length(attnums)) + return false; + + foreach_int(attno, notnull_attnums) + { + if (!list_member_int(attnums, attno)) + return false; + } + + snapshot = RegisterSnapshot(GetLatestSnapshot()); + + forboth(lc, attnums, lc2, idxs) + { + IndexScanDesc indexScan; + ScanKeyData scankeys[1]; + AttrNumber attno = lfirst_int(lc); + Oid indexoid = lfirst_oid(lc2); + TupleTableSlot *existing_slot; + + indexRel = index_open(indexoid, NoLock); + + Assert(indexRel->rd_index->indkey.values[0] == attno); + + existing_slot = table_slot_create(relation, NULL); + + /* set up an IS NULL scan key so that only NULLs are returned */ + ScanKeyEntryInitialize(&scankeys[0], + SK_ISNULL | SK_SEARCHNULL, + 1, /* index col to scan */ + InvalidStrategy, /* no strategy */ + InvalidOid, /* no strategy subtype */ + InvalidOid, /* no collation */ + InvalidOid, /* no reg proc for this */ + (Datum) 0); /* constant */ + + indexScan = index_beginscan(relation, + indexRel, + false, + snapshot, + NULL, + 1, + 0, + SO_NONE); + index_rescan(indexScan, scankeys, 1, NULL, 0); + + /* + * A returned tuple is a visible row with NULL in this column; btree + * never requests a recheck. Returning false makes the caller fall + * back to a full table scan. + */ + found_null = table_index_getnext_slot(indexScan, ForwardScanDirection, + existing_slot); + index_endscan(indexScan); + index_close(indexRel, NoLock); + ExecDropSingleTupleTableSlot(existing_slot); + + if (found_null) + break; + } + UnregisterSnapshot(snapshot); + + return !found_null; +} diff --git a/src/test/modules/test_misc/meson.build b/src/test/modules/test_misc/meson.build index 5d81f5b13be..ae62367d16e 100644 --- a/src/test/modules/test_misc/meson.build +++ b/src/test/modules/test_misc/meson.build @@ -24,6 +24,7 @@ tests += { 't/013_temp_obj_multisession.pl', 't/014_log_statement_max_length.pl', 't/015_temp_schema_exit_deferrable.pl', + 't/016_indexscan_validate_notnull.pl', ], # The injection points are cluster-wide, so disable installcheck 'runningcheck': false, diff --git a/src/test/modules/test_misc/t/016_indexscan_validate_notnull.pl b/src/test/modules/test_misc/t/016_indexscan_validate_notnull.pl new file mode 100644 index 00000000000..5e14ae622d7 --- /dev/null +++ b/src/test/modules/test_misc/t/016_indexscan_validate_notnull.pl @@ -0,0 +1,145 @@ +# Copyright (c) 2026, PostgreSQL Global Development Group + +# Verify that indexscan mechanism can speedup ALTER TABLE ADD NOT NULL + +use strict; +use warnings FATAL => 'all'; + +use PostgreSQL::Test::Cluster; +use PostgreSQL::Test::Utils; +use Test::More; +use Time::HiRes qw(usleep); + +# Initialize a test cluster +my $node = PostgreSQL::Test::Cluster->new('primary'); +$node->init(); +# Turn message level up to DEBUG1 so that we get the messages we want to see +$node->append_conf('postgresql.conf', 'client_min_messages = DEBUG1'); +$node->start; + +# Run a SQL command and return psql's stderr (including debug messages) +sub run_sql_command +{ + my $sql = shift; + my $stderr; + + $node->psql( + 'postgres', + $sql, + stderr => \$stderr, + on_error_die => 1, + on_error_stop => 1); + return $stderr; +} + +# Verify that run_sql_command output confirms an index scan is used for the NOT NULL check. +sub is_indexscan_veritify_notnull +{ + my $output = shift; + return index($output, 'DEBUG: all new not-null constraints on relation') != -1; +} + +my $output; + +note "test alter table SET NOT NULL using indexscan with partitioned table"; + +run_sql_command( + 'CREATE TABLE tp_notnull ( + a int, b int, c int, d int, f1 int default 1, + f2 int default 2, f3 int default 3, f4 int default 4, f5 int default 5) PARTITION BY range(a); + CREATE TABLE tp_notnull_1 partition of tp_notnull for values from ( 1 ) to (10); + CREATE TABLE tp_notnull_2 partition of tp_notnull for values from ( 10 ) to (21); + INSERT INTO tp_notnull(a, b, c, d) SELECT g, g + 10, g + 15, g FROM generate_series(1,19) g; + CREATE INDEX ON tp_notnull(b); + CREATE INDEX ON tp_notnull(c); + CREATE INDEX ON tp_notnull(d); + CREATE INDEX ON tp_notnull(f1) INCLUDE (f2); + CREATE INDEX ON tp_notnull USING hash (f3 int4_ops); + '); + +# Using index scan for NOT NULL verification requires every not-null column must possess a suitable index +$output = run_sql_command( + 'ALTER TABLE tp_notnull ALTER COLUMN a SET NOT NULL, ADD CONSTRAINT c_nn NOT NULL c'); +ok(!is_indexscan_veritify_notnull($output), 'cannot use indexscan to verify not-null constraints on table tp_notnull'); + +# All columns have index on it, OK +$output = run_sql_command( + 'ALTER TABLE tp_notnull ALTER COLUMN d SET NOT NULL, ALTER COLUMN c SET NOT NULL, ALTER COLUMN b SET NOT NULL;'); +ok(is_indexscan_veritify_notnull($output), + 'using indexscan to verify not-null constraints on column b, c, d'); +ok( $output =~ + m/all new not-null constraints on relation "tp_notnull_1" have been validated by using index scan/, + 'all newly added constraints proved by indexscan'); +ok( $output =~ + m/all new not-null constraints on relation "tp_notnull_2" have been validated by using index scan/, + 'all newly added constraints proved by indexscan'); + +run_sql_command('ALTER TABLE tp_notnull ALTER COLUMN b DROP NOT NULL;'); +# Now using index scans on partitions to quickly verify NOT NULL constraints +$output = run_sql_command('ALTER TABLE tp_notnull_1 ALTER COLUMN b SET NOT NULL;'); +ok(is_indexscan_veritify_notnull($output), 'index scan verifies NOT NULL for column b'); + +run_sql_command('ALTER TABLE tp_notnull ALTER COLUMN b DROP NOT NULL;'); +# Cannot use indexscan to verify not-null constraint if table rewrite happens +$output = run_sql_command( + 'ALTER TABLE tp_notnull ALTER COLUMN b SET NOT NULL, ALTER COLUMN b SET DATA TYPE bigint;' +); +ok(!is_indexscan_veritify_notnull($output), 'table rewrite on tp_notnull, cannot use indexscan to verify not-null constraints on it'); + +run_sql_command('ALTER TABLE tp_notnull ALTER COLUMN d DROP NOT NULL'); + +# Cannot use indexscan because ALTER TABLE VALIDATE CONSTRAINT only hold +# ShareUpdateExclusiveLock lock on the table +run_sql_command('ALTER TABLE tp_notnull ADD CONSTRAINT tp_notnull_d NOT NULL d NOT VALID;'); +$output = run_sql_command( + 'ALTER TABLE tp_notnull VALIDATE CONSTRAINT tp_notnull_d;' +); +ok(!is_indexscan_veritify_notnull($output), 'cannot use indexscan for ALTER TABLE VALIDATE CONSTRAINT command'); + +run_sql_command('ALTER TABLE tp_notnull ALTER COLUMN d DROP NOT NULL; + ALTER TABLE tp_notnull ADD CONSTRAINT tp_notnull_d NOT NULL d NOT VALID;'); + +# OK, ALTER COLUMN SET NOT NULL acquires AccessExclusiveLock +$output = run_sql_command( + 'ALTER TABLE tp_notnull VALIDATE CONSTRAINT tp_notnull_d, ALTER COLUMN b SET NOT NULL;' +); +ok(is_indexscan_veritify_notnull($output), 'using indexscan to verify not-null constraints on column b, c, d'); + +# Indexscan mechanism to verify NOT NULL constraints cannot apply to index +# INCLUDE column +$output = run_sql_command( + 'ALTER TABLE tp_notnull ADD CONSTRAINT nnf4 NOT NULL f2;' +); +ok(!is_indexscan_veritify_notnull($output), 'cannot use indexscan to validate not-null constraint on tp_notnull'); + +# Indexscan mechanism to verify NOT NULL constraints cannot apply to non-Btree index +$output = run_sql_command('ALTER TABLE tp_notnull ALTER COLUMN f3 SET NOT NULL;'); +ok(!is_indexscan_veritify_notnull($output), 'cannot use indexscan to validate not-null constraints on tp_notnull'); + +run_sql_command('CREATE INDEX ON tp_notnull USING btree (f4 int4_ops, f5 int4_ops)'); +# Indexscan mechanism to verify NOT NULL constraints only apply to leading column +$output = run_sql_command('ALTER TABLE tp_notnull ALTER COLUMN f4 SET NOT NULL'); +ok(is_indexscan_veritify_notnull($output), 'cannot use indexscan to validate not-null constraints on tp_notnull'); + +$output = run_sql_command('ALTER TABLE tp_notnull ALTER COLUMN f5 SET NOT NULL'); +ok(!is_indexscan_veritify_notnull($output), 'cannot use indexscan to validate not-null constraints on tp_notnull'); + +run_sql_command(' + create table t(id int, v int) with (autovacuum_enabled = false, fillfactor = 50); + insert into t values (1, NULL)' +); +run_sql_command(' + begin; + select count(*) from t; + update t set v = 5 where id = 1; + create index t_v_idx on t (v); + alter table t alter column v set not null; + commit;' +); +# t_v_idx has pg_index.indcheckxmin = true: its underlying tuple belongs to a +# HOT chain, so this index cannot be trusted to answer "is this column NULL" +ok(!is_indexscan_veritify_notnull($output), 'cannot use indexscan to validate not-null constraints on t'); + +$node->stop('fast'); + +done_testing(); \ No newline at end of file -- 2.34.1
v1-0002-Optimize-VALIDATE-CONSTRAINT-for-simple-indexed-colu.patch (application/octet-stream, 16.4K) download From 35b4428e28223df07b2d2c5e24566d8b9f901db5 Mon Sep 17 00:00:00 2001 From: Baji Shaik <baji.pgdev@gmail.com> Date: Sat, 14 Nov 2026 19:07:56 -0600 Subject: [PATCH] Optimize VALIDATE CONSTRAINT for simple indexed-column checks ALTER TABLE ... VALIDATE CONSTRAINT currently always performs a full sequential scan of the table to verify a NOT VALID CHECK constraint. For large tables this can take hours. When the constraint is a simple comparison between a column and a constant, and that column is NOT NULL and is the leading column of a non-partial btree index, the index already has enough information to answer the question. We can validate the constraint with a single index scan for the negated condition instead of scanning every row: for a constraint of the form "id < N", we scan the index for "id >= N", and if no such row exists, the constraint holds. The index's collation must match the constraint's comparison collation exactly, and the scan key's subtype must be the constant's own type (not assumed to match the column's type): a constant outside the column's type's range forces a cross-type comparison operator, and the scan key has to name that type explicitly or the index scan can silently miss violating rows. Fall through to the full scan whenever either check fails. The optimization is transparent: it falls through to the existing full-scan path for any expression it cannot handle, including complex expressions, nullable columns, non-indexed columns, mismatched collations, and cross-type constants, and for constraints that turn out to be violated, so the error message and reported offending row are unchanged. Author: Baji Shaik <baji.pgdev@gmail.com> --- src/backend/commands/tablecmds.c | 228 ++++++++++++++++++++++ src/test/regress/expected/alter_table.out | 75 +++++++ src/test/regress/sql/alter_table.sql | 58 ++++++ 3 files changed, 361 insertions(+) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index b6700929459..187c56ef7e4 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -6203,6 +6203,230 @@ ATRewriteTables(AlterTableStmt *parsetree, List **wqueue, LOCKMODE lockmode, } } +/* + * ValidateCheckConstraintViaIndex + * + * Validate a "column OP constant" CHECK constraint with an index probe + * instead of a full table scan, when the column is NOT NULL and is the + * leading column of a non-partial btree index whose opfamily has a + * negator for OP. For "id < N" we scan the index for "id >= N"; no + * match means the constraint holds. + * + * Returns true if proven valid, false if the optimization doesn't apply + * or a violating row was found (caller falls back to the full scan, + * which reports the error). + * + * Uses the same snapshot source as the full-scan path (GetLatestSnapshot), + * so MVCC semantics are unchanged. + */ +static bool +ValidateCheckConstraintViaIndex(Relation rel, Node *qual) +{ + OpExpr *opexpr; + Node *leftarg; + Node *rightarg; + Var *var; + Const *constval; + Oid negator; + Oid indexoid = InvalidOid; + Relation indexrel; + List *indexlist; + ListCell *lc; + ScanKeyData scankey; + IndexScanDesc iscan; + TupleTableSlot *slot; + Snapshot snapshot; + bool found_violation = false; + AttrNumber varattno; + Oid opfamily; + int strategy; + RegProcedure opproc; + CompactAttribute *att; + + /* Must be a simple OpExpr with two arguments */ + if (!IsA(qual, OpExpr)) + return false; + opexpr = (OpExpr *) qual; + if (list_length(opexpr->args) != 2) + return false; + + leftarg = (Node *) linitial(opexpr->args); + rightarg = (Node *) lsecond(opexpr->args); + + /* peel RelabelType, e.g. from varchar or domain columns */ + while (leftarg && IsA(leftarg, RelabelType)) + leftarg = (Node *) ((RelabelType *) leftarg)->arg; + while (rightarg && IsA(rightarg, RelabelType)) + rightarg = (Node *) ((RelabelType *) rightarg)->arg; + + if (IsA(leftarg, Var) && IsA(rightarg, Const)) + { + var = (Var *) leftarg; + constval = (Const *) rightarg; + negator = get_negator(opexpr->opno); + } + else if (IsA(leftarg, Const) && IsA(rightarg, Var)) + { + /* "N OP id" form: negate the commuted operator, e.g. id > N */ + Oid commutator = get_commutator(opexpr->opno); + + if (!OidIsValid(commutator)) + return false; + + var = (Var *) rightarg; + constval = (Const *) leftarg; + negator = get_negator(commutator); + } + else + return false; + + /* Must have a valid negator */ + if (!OidIsValid(negator)) + return false; + + /* Only handle simple column references (not system columns) */ + if (var->varattno <= 0) + return false; + varattno = var->varattno; + + if (constval->constisnull) + return false; + + /* + * NULL never violates a CHECK constraint, and the negated-condition + * scan below wouldn't find NULLs anyway, so this is conservative, not + * required for correctness -- kept simple for now. + */ + att = TupleDescCompactAttr(RelationGetDescr(rel), varattno - 1); + if (att->attnullability != ATTNULLABLE_VALID) + return false; + + /* find a non-partial btree index whose leading column matches var */ + indexlist = RelationGetIndexList(rel); + foreach(lc, indexlist) + { + Oid idxoid = lfirst_oid(lc); + HeapTuple idxtup; + Form_pg_index idxform; + bool isnull; + Relation tmprel; + + idxtup = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(idxoid)); + if (!HeapTupleIsValid(idxtup)) + continue; + idxform = (Form_pg_index) GETSTRUCT(idxtup); + + /* Must be valid and ready */ + if (!idxform->indisvalid || !idxform->indisready) + { + ReleaseSysCache(idxtup); + continue; + } + + /* Must not be a partial index (has no predicate) */ + (void) SysCacheGetAttr(INDEXRELID, idxtup, + Anum_pg_index_indpred, &isnull); + if (!isnull) + { + ReleaseSysCache(idxtup); + continue; + } + + /* Leading column must match our Var */ + if (idxform->indkey.values[0] != varattno) + { + ReleaseSysCache(idxtup); + continue; + } + + /* Must be a btree index */ + tmprel = index_open(idxoid, AccessShareLock); + if (tmprel->rd_rel->relam == BTREE_AM_OID) + { + indexoid = idxoid; + index_close(tmprel, AccessShareLock); + ReleaseSysCache(idxtup); + break; + } + index_close(tmprel, AccessShareLock); + ReleaseSysCache(idxtup); + } + list_free(indexlist); + + if (!OidIsValid(indexoid)) + return false; + + indexrel = index_open(indexoid, AccessShareLock); + + /* + * The index's on-disk order is fixed by the collation it was built + * with. Scanning it for the negated condition under a different + * collation can miss rows, silently validating a constraint that's + * actually violated. + */ + if (indexrel->rd_indcollation[0] != opexpr->inputcollid) + { + index_close(indexrel, AccessShareLock); + return false; + } + + opfamily = indexrel->rd_opfamily[0]; + strategy = get_op_opfamily_strategy(negator, opfamily); + if (strategy == InvalidStrategy) + { + index_close(indexrel, AccessShareLock); + return false; + } + + opproc = get_opcode(negator); + if (!RegProcedureIsValid(opproc)) + { + index_close(indexrel, AccessShareLock); + return false; + } + + /* + * subtype must be the constant's actual type, not InvalidOid: a + * constant whose type is wider than the column's (e.g. a bigint + * literal against an int4 PK, as happens once the constant falls + * outside int4's range) needs the cross-type comparison member of the + * opfamily, which the scan key has to name explicitly. + */ + ScanKeyEntryInitialize(&scankey, + 0, + 1, + strategy, + constval->consttype, + opexpr->inputcollid, + opproc, + constval->constvalue); + + /* snapshot must be registered, not just a bare GetLatestSnapshot() */ + slot = MakeSingleTupleTableSlot(RelationGetDescr(rel), + table_slot_callbacks(rel)); + snapshot = RegisterSnapshot(GetLatestSnapshot()); + iscan = index_beginscan(rel, indexrel, false, snapshot, NULL, + 1, 0, SO_NONE); + index_rescan(iscan, &scankey, 1, NULL, 0); + + if (table_index_getnext_slot(iscan, ForwardScanDirection, slot)) + found_violation = true; + + index_endscan(iscan); + UnregisterSnapshot(snapshot); + ExecDropSingleTupleTableSlot(slot); + index_close(indexrel, AccessShareLock); + + if (found_violation) + return false; + + ereport(DEBUG1, + errmsg_internal("check constraint validated via index on \"%s\"", + RelationGetRelationName(rel))); + + return true; +} + /* * ATRewriteTable: scan or rewrite one table * @@ -6276,6 +6500,10 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap) switch (con->contype) { case CONSTR_CHECK: + + if (!tab->rewrite && !newrel && + ValidateCheckConstraintViaIndex(oldrel, con->qual)) + break; /* proven valid via index, skip full scan */ needscan = true; con->qualstate = ExecPrepareExpr((Expr *) expand_generated_columns_in_expr(con->qual, oldrel, 1), estate); break; diff --git a/src/test/regress/expected/alter_table.out b/src/test/regress/expected/alter_table.out index f99b2b9e663..c2feb56035a 100644 --- a/src/test/regress/expected/alter_table.out +++ b/src/test/regress/expected/alter_table.out @@ -888,6 +888,81 @@ select convalidated from pg_constraint where conrelid = 'parent_noinh_convalid': -- cleanup drop table parent_noinh_convalid, child_noinh_convalid; +-- VALIDATE CONSTRAINT via index scan, for simple comparisons on NOT NULL +-- indexed columns +CREATE TABLE validate_via_index (id bigint PRIMARY KEY, val text); +INSERT INTO validate_via_index SELECT g, 'v'||g FROM generate_series(1, 1000) g; +-- Should use index (simple < on PK) +ALTER TABLE validate_via_index ADD CONSTRAINT chk_lt CHECK (id < 2000) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_lt; +SELECT convalidated FROM pg_constraint WHERE conname = 'chk_lt'; + convalidated +-------------- + t +(1 row) + +-- Should use index (simple > on PK) +ALTER TABLE validate_via_index ADD CONSTRAINT chk_gt CHECK (id > 0) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_gt; +SELECT convalidated FROM pg_constraint WHERE conname = 'chk_gt'; + convalidated +-------------- + t +(1 row) + +-- Should use index (reversed form: constant OP column) +ALTER TABLE validate_via_index ADD CONSTRAINT chk_rev CHECK (0 < id) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_rev; +SELECT convalidated FROM pg_constraint WHERE conname = 'chk_rev'; + convalidated +-------------- + t +(1 row) + +-- Should fail (constraint is violated) +ALTER TABLE validate_via_index ADD CONSTRAINT chk_fail CHECK (id < 500) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_fail; +ERROR: check constraint "chk_fail" of relation "validate_via_index" is violated by some row +ALTER TABLE validate_via_index DROP CONSTRAINT chk_fail; +-- Should fall through to full scan (AND expression) +ALTER TABLE validate_via_index ADD CONSTRAINT chk_and CHECK (id > 0 AND id < 2000) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_and; +SELECT convalidated FROM pg_constraint WHERE conname = 'chk_and'; + convalidated +-------------- + t +(1 row) + +-- Should fall through to full scan (nullable column) +ALTER TABLE validate_via_index ADD COLUMN nullable_col int; +CREATE INDEX ON validate_via_index(nullable_col); +UPDATE validate_via_index SET nullable_col = id; +ALTER TABLE validate_via_index ADD CONSTRAINT chk_nullable CHECK (nullable_col < 2000) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_nullable; +SELECT convalidated FROM pg_constraint WHERE conname = 'chk_nullable'; + convalidated +-------------- + t +(1 row) + +-- Should fall through to full scan (index collation != constraint collation) +ALTER TABLE validate_via_index ADD COLUMN label text NOT NULL DEFAULT ''; +UPDATE validate_via_index SET label = 'B' WHERE id <= 10; +UPDATE validate_via_index SET label = 'a' WHERE id > 10; +CREATE INDEX ON validate_via_index (label COLLATE "C"); +ALTER TABLE validate_via_index ADD CONSTRAINT chk_coll CHECK (label < 'b') NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_coll; +ERROR: check constraint "chk_coll" of relation "validate_via_index" is violated by some row +DROP TABLE validate_via_index; +-- Should fail (constant is outside the column's own type's range, forcing a +-- cross-type comparison operator; the scan key's subtype must be set to the +-- constant's type for that case, not just for same-type comparisons) +CREATE TABLE validate_via_index_crosstype (id int PRIMARY KEY); +INSERT INTO validate_via_index_crosstype VALUES (1), (2), (3); +ALTER TABLE validate_via_index_crosstype ADD CONSTRAINT chk_crosstype CHECK (id < -3000000000) NOT VALID; +ALTER TABLE validate_via_index_crosstype VALIDATE CONSTRAINT chk_crosstype; +ERROR: check constraint "chk_crosstype" of relation "validate_via_index_crosstype" is violated by some row +DROP TABLE validate_via_index_crosstype; -- Try (and fail) to create constraint from attmp5(a) to attmp4(a) - unique constraint on -- attmp4 is a,b ALTER TABLE attmp5 add constraint attmpconstr foreign key(a) references attmp4(a) match full; diff --git a/src/test/regress/sql/alter_table.sql b/src/test/regress/sql/alter_table.sql index a6b4265b811..b5ad57c360e 100644 --- a/src/test/regress/sql/alter_table.sql +++ b/src/test/regress/sql/alter_table.sql @@ -670,6 +670,64 @@ select convalidated from pg_constraint where conrelid = 'parent_noinh_convalid': -- cleanup drop table parent_noinh_convalid, child_noinh_convalid; +-- VALIDATE CONSTRAINT via index scan, for simple comparisons on NOT NULL +-- indexed columns +CREATE TABLE validate_via_index (id bigint PRIMARY KEY, val text); +INSERT INTO validate_via_index SELECT g, 'v'||g FROM generate_series(1, 1000) g; + +-- Should use index (simple < on PK) +ALTER TABLE validate_via_index ADD CONSTRAINT chk_lt CHECK (id < 2000) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_lt; +SELECT convalidated FROM pg_constraint WHERE conname = 'chk_lt'; + +-- Should use index (simple > on PK) +ALTER TABLE validate_via_index ADD CONSTRAINT chk_gt CHECK (id > 0) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_gt; +SELECT convalidated FROM pg_constraint WHERE conname = 'chk_gt'; + +-- Should use index (reversed form: constant OP column) +ALTER TABLE validate_via_index ADD CONSTRAINT chk_rev CHECK (0 < id) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_rev; +SELECT convalidated FROM pg_constraint WHERE conname = 'chk_rev'; + +-- Should fail (constraint is violated) +ALTER TABLE validate_via_index ADD CONSTRAINT chk_fail CHECK (id < 500) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_fail; +ALTER TABLE validate_via_index DROP CONSTRAINT chk_fail; + +-- Should fall through to full scan (AND expression) +ALTER TABLE validate_via_index ADD CONSTRAINT chk_and CHECK (id > 0 AND id < 2000) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_and; +SELECT convalidated FROM pg_constraint WHERE conname = 'chk_and'; + +-- Should fall through to full scan (nullable column) +ALTER TABLE validate_via_index ADD COLUMN nullable_col int; +CREATE INDEX ON validate_via_index(nullable_col); +UPDATE validate_via_index SET nullable_col = id; +ALTER TABLE validate_via_index ADD CONSTRAINT chk_nullable CHECK (nullable_col < 2000) NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_nullable; +SELECT convalidated FROM pg_constraint WHERE conname = 'chk_nullable'; + +-- Should fall through to full scan (index collation != constraint collation) +ALTER TABLE validate_via_index ADD COLUMN label text NOT NULL DEFAULT ''; +UPDATE validate_via_index SET label = 'B' WHERE id <= 10; +UPDATE validate_via_index SET label = 'a' WHERE id > 10; +CREATE INDEX ON validate_via_index (label COLLATE "C"); +ALTER TABLE validate_via_index ADD CONSTRAINT chk_coll CHECK (label < 'b') NOT VALID; +ALTER TABLE validate_via_index VALIDATE CONSTRAINT chk_coll; + +DROP TABLE validate_via_index; + +-- Should fail (constant is outside the column's own type's range, forcing a +-- cross-type comparison operator; the scan key's subtype must be set to the +-- constant's type for that case, not just for same-type comparisons) +CREATE TABLE validate_via_index_crosstype (id int PRIMARY KEY); +INSERT INTO validate_via_index_crosstype VALUES (1), (2), (3); +ALTER TABLE validate_via_index_crosstype ADD CONSTRAINT chk_crosstype CHECK (id < -3000000000) NOT VALID; +ALTER TABLE validate_via_index_crosstype VALIDATE CONSTRAINT chk_crosstype; + +DROP TABLE validate_via_index_crosstype; + -- Try (and fail) to create constraint from attmp5(a) to attmp4(a) - unique constraint on -- attmp4 is a,b -- 2.54.0 (Apple Git-157)

view thread (23+ messages)

Message-ID: <CA+fm-RPCcMxpHVJrUZWKwMPtX_D2eq=2ArAqU7U1GsJrGyiCDg@mail.gmail.com>
Permalink:  ../CA+fm-RPCcMxpHVJrUZWKwMPtX_D2eq=2ArAqU7U1GsJrGyiCDg@mail.gmail.com/
Also on:    postgresql.org/message-id/CA+fm-RPCcMxpHVJrUZWKwMPtX_D2eq=2ArAqU7U1GsJrGyiCDg@mail.gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: baji.pgdev@gmail.com, jian.universality@gmail.com, alvherre@kurilemu.de
  Subject: Re: using index to speedup add not null constraints to a table
  In-Reply-To: <CA+fm-RPCcMxpHVJrUZWKwMPtX_D2eq=2ArAqU7U1GsJrGyiCDg@mail.gmail.com>
  References: <CACJufxE-+VSkxMvBRbpM45iex=7ws4-km4MvmOBPXtTJ7jjdQA@mail.gmail.com> <202604070954.qesyoeqbnvm6@alvherre.pgsql> <CACJufxGr=t0N1C87ZgKD4pEQ42mFjS+jEVW=s=D_Mu8i0dnzLA@mail.gmail.com> <CACJufxFaNH_LVHi8cd+AkKS4Jx7ihbyBEN9tyCxSW=MZPVTS6A@mail.gmail.com> <CACJufxEkSyhOotK0b3hj_D3c9HO=dxGoGxL3Cx4j6enG_t_P6Q@mail.gmail.com> <CA+fm-RPCcMxpHVJrUZWKwMPtX_D2eq=2ArAqU7U1GsJrGyiCDg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox