diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 3aac459e483d..f11ca4b20042 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -210,16 +210,19 @@ typedef struct AlteredTableInfo List *changedStatisticsDefs; /* string definitions 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 are handled here too. +*/ 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; @@ -6184,6 +6187,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; @@ -6237,10 +6243,75 @@ ATRewriteTable(AlteredTableInfo *tab, Oid OIDNewHeap) wholeatt->attnum); } } - if (notnull_attrs || notnull_virtual_attrs) - needscan = true; } + /* + * The conditions for using indexscan mechanism fast vertifing not-null + * constraints are quite strict. All of the following conditions must be + * met. + * 1. AlteredTableInfo->verify_new_notnull is true. + * 2. If table scan (for other reason, like check constraint verification) + * or table rewrite is expected later, using indexscan is just wastes + * cycles. + * 3. Indexes cannot be created on virtual generated columns, so fast + * checking not-null constraints is not applicable to them. + * 4. This applies to plain relation only. + * 5. To prevent possible concurrency issues, the table must already be + * locked with an AccessExclusiveLock, which is the lock obtained during + * ALTER TABLE SET NOT NULL. + */ + if (!needscan && + newrel == NULL && + tab->verify_new_notnull && + notnull_virtual_attrs == NIL && + 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); + + Assert(attr->attnotnull); + Assert(attr->attnum == con->attnum); + + if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL) + { + needscan = true; + break; + } + + notnull_attnums = lappend_int(notnull_attnums, attr->attnum); + } + + /* + * notnull_attnums contains all attributes with NOT NULL constraints + * that needs to be validation. + * If needscan is really needed, later table scan will do the + * validation, indedxscan would just waste cycle. + */ + if (notnull_attnums != NIL && !needscan) + { + if (!index_check_notnull(oldrel, notnull_attnums)) + needscan = true; + else + ereport(DEBUG1, + errmsg_internal("all new not-null constraints on relation \"%s\" have been validated by using index scan", + RelationGetRelationName(oldrel))); + } + } + else if (notnull_attrs || notnull_virtual_attrs) + needscan = true; + if (newrel || needscan) { ExprContext *econtext; @@ -7912,6 +7983,7 @@ ATExecSetNotNull(List **wqueue, Relation rel, char *conName, char *colName, CookedConstraint *ccon; List *cooked; bool is_no_inherit = false; + AlteredTableInfo *tab = NULL; /* Guard against stack overflow due to overly deep inheritance tree. */ check_stack_depth(); @@ -8036,10 +8108,31 @@ ATExecSetNotNull(List **wqueue, Relation rel, char *conName, char *colName, constraint->is_no_inherit = is_no_inherit; constraint->conname = conName; + tab = ATGetQueueEntry(wqueue, rel); + /* and do it */ cooked = AddRelationNewConstraints(rel, NIL, list_make1(constraint), false, !recursing, false, NULL); ccon = linitial(cooked); + + Assert(ccon->contype == CONSTR_NOTNULL); + + /* + * We may use indexscan to vertify not-null constraint in Phase3. Add the + * to-be-validated not-null constraint to Phase 3's queue. + */ + if (!ccon->skip_validation) + { + NewConstraint *newcon; + newcon = (NewConstraint *) palloc0(sizeof(NewConstraint)); + + newcon->name = ccon->name; + newcon->contype = ccon->contype; + newcon->attnum = ccon->attnum; + + tab->constraints = lappend(tab->constraints, newcon); + } + ObjectAddressSet(address, ConstraintRelationId, ccon->conoid); InvokeObjectPostAlterHook(RelationRelationId, @@ -9945,13 +10038,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 = (NewConstraint *) palloc0(sizeof(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); @@ -13220,6 +13315,7 @@ QueueNNConstraintValidation(List **wqueue, Relation conrel, Relation rel, List *children = NIL; AttrNumber attnum; char *colname; + NewConstraint *newcon; con = (Form_pg_constraint) GETSTRUCT(contuple); Assert(con->contype == CONSTRAINT_NOTNULL); @@ -13283,7 +13379,19 @@ QueueNNConstraintValidation(List **wqueue, Relation conrel, Relation rel, /* Set attnotnull appropriately without queueing another validation */ set_attnotnull(NULL, rel, attnum, true, false); + /* + * Queue validation for phase 3. + * ALTER TABLE SET NOT NULL will add NOT NULL constraint to + * AlteredTableInfo->constraints, for consistency, do the same here. + */ + newcon = (NewConstraint *) palloc0(sizeof(NewConstraint)); + newcon->name = colname; + newcon->contype = CONSTR_NOTNULL; + newcon->attnum = attnum; + + /* Find or create work queue entry for this table */ tab = ATGetQueueEntry(wqueue, rel); + tab->constraints = lappend(tab->constraints, newcon); tab->verify_new_notnull = true; /* diff --git a/src/backend/executor/execIndexing.c b/src/backend/executor/execIndexing.c index 401606f840ad..a4cfde2ecfda 100644 --- a/src/backend/executor/execIndexing.c +++ b/src/backend/executor/execIndexing.c @@ -108,12 +108,15 @@ #include "access/genam.h" #include "access/relscan.h" +#include "access/table.h" #include "access/tableam.h" #include "access/xact.h" #include "catalog/index.h" +#include "catalog/pg_am_d.h" #include "executor/executor.h" #include "nodes/nodeFuncs.h" #include "storage/lmgr.h" +#include "utils/fmgroids.h" #include "utils/multirangetypes.h" #include "utils/rangetypes.h" #include "utils/snapmgr.h" @@ -145,6 +148,7 @@ static bool index_expression_changed_walker(Node *node, Bitmapset *allUpdatedCols); static void ExecWithoutOverlapsNotEmpty(Relation rel, NameData attname, Datum attval, char typtype, Oid atttypid); +static bool index_check_notnull_internal(Relation relation, List *attnums, List *idxs); /* ---------------------------------------------------------------- * ExecOpenIndices @@ -1172,3 +1176,212 @@ ExecWithoutOverlapsNotEmpty(Relation rel, NameData attname, Datum attval, char t errmsg("empty WITHOUT OVERLAPS value found in column \"%s\" in relation \"%s\"", NameStr(attname), RelationGetRelationName(rel)))); } + +/* + * notnull_attnums: list of attribute numbers of all new not-null constraints. + * + * First check whether the relation has a suitable index for each not-null + * attribute, then using indexscan mechanism to verify that *all* attributes on + * notnull_attnums are indeed not null. + * + * Returning true means all new not-null constraints have been verified, no + * need an extra scan to verify newly added not-null constraints. + */ +bool +index_check_notnull(Relation relation, List *notnull_attnums) +{ + Relation pg_index; + Relation index_rel; + SysScanDesc indscan; + ScanKeyData skey; + HeapTuple htup; + TupleDesc tupdesc; + Form_pg_attribute attr; + List *idxs = NIL; + List *attnums = NIL; + + tupdesc = RelationGetDescr(relation); + + /* Prepare to scan pg_index for entries having indrelid = this rel */ + ScanKeyInit(&skey, + Anum_pg_index_indrelid, + BTEqualStrategyNumber, F_OIDEQ, + ObjectIdGetDatum(RelationGetRelid(relation))); + + pg_index = table_open(IndexRelationId, AccessShareLock); + indscan = systable_beginscan(pg_index, IndexIndrelidIndexId, true, + NULL, 1, &skey); + + while (HeapTupleIsValid(htup = systable_getnext(indscan))) + { + Form_pg_index index = (Form_pg_index) GETSTRUCT(htup); + + /* + * we can only use non-deferred, validated, and alive btree index to + * validate not-null constraints + */ + if (!index->indimmediate || !index->indisvalid || !index->indislive) + continue; + + /* can not use expression index or partial index too */ + if (!heap_attisnull(htup, Anum_pg_index_indexprs, NULL) || + !heap_attisnull(htup, Anum_pg_index_indpred, NULL)) + continue; + + index_rel = index_open(index->indexrelid, AccessShareLock); + + /* can only apply to btree index */ + if (index_rel->rd_rel->relam != BTREE_AM_OID) + { + index_close(index_rel, AccessShareLock); + continue; + } + + /* collect index attnums while loop */ + for (int i = 0; i < index->indnkeyatts; i++) + { + attr = TupleDescAttr(tupdesc, (index->indkey.values[i] - 1)); + + if (attr->attisdropped) + continue; + + if (list_member_int(notnull_attnums, attr->attnum) && + !list_member_int(attnums, attr->attnum)) + { + attnums = lappend_int(attnums, attr->attnum); + idxs = lappend_oid(idxs, index->indexrelid); + } + } + index_close(index_rel, NoLock); + } + systable_endscan(indscan); + table_close(pg_index, AccessShareLock); + + if (attnums == NIL) + return false; + + /* + * every not-null constraint requires an index for indexscan. If no such + * index exists, we need to use regular table scan to validate not-null + * constraints. + */ + foreach_int(attno, notnull_attnums) + { + if(!list_member_int(attnums, attno)) + return false; + } + + return index_check_notnull_internal(relation, attnums, idxs); +} + +/* + * Use indexscan mechanism to fast verify each attribute in "attnums" are + * not-null or not. + */ +static bool +index_check_notnull_internal(Relation relation, List *attnums, List *idxs) +{ + EState *estate; + ExprContext *econtext; + TupleTableSlot *existing_slot; + bool all_not_null = true; + ListCell *lc, + *lc2; + + /* + * Need an EState for slot to hold the current tuple. + */ + estate = CreateExecutorState(); + econtext = GetPerTupleExprContext(estate); + + forboth(lc, attnums, lc2, idxs) + { + SnapshotData DirtySnapshot; + IndexScanDesc index_scan; + ScanKeyData scankeys[INDEX_MAX_KEYS]; + IndexInfo *indexInfo; + AttrNumber sk_attno = -1; + Relation index; + int indnkeyatts; + + AttrNumber attno = lfirst_int(lc); + Oid indexoid = lfirst_oid(lc2); + + existing_slot = table_slot_create(relation, NULL); + + /* Arrange for econtext's scan tuple to be the tuple under test */ + econtext->ecxt_scantuple = existing_slot; + + index = index_open(indexoid, NoLock); + + indexInfo = BuildIndexInfo(index); + indnkeyatts = IndexRelationGetNumberOfKeyAttributes(index); + + /* + * Search the tuples that are in the index for any violations, including + * tuples that aren't visible yet. + */ + InitDirtySnapshot(DirtySnapshot); + + for (int i = 0; i < indnkeyatts; i++) + { + if (indexInfo->ii_IndexAttrNumbers[i] == attno) + { + sk_attno = i+1; + break; + } + } + + if (sk_attno == -1) + elog(ERROR, "cache lookup failed for attribute number %d on index %u", indexoid, attno); + + for (int i = 0; i < indnkeyatts; i++) + { + /* set up an IS NULL scan key so that we ignore not nulls */ + ScanKeyEntryInitialize(&scankeys[i], + SK_ISNULL | SK_SEARCHNULL, + sk_attno, /* index col to scan */ + InvalidStrategy, /* no strategy */ + InvalidOid, /* no strategy subtype */ + InvalidOid, /* no collation */ + InvalidOid, /* no reg proc for this */ + (Datum) 0); /* constant */ + } + + index_scan = index_beginscan(relation, index, &DirtySnapshot, NULL, indnkeyatts, 0); + index_rescan(index_scan, scankeys, indnkeyatts, NULL, 0); + + while (index_getnext_slot(index_scan, ForwardScanDirection, existing_slot)) + { + Datum values[INDEX_MAX_KEYS]; + bool nulls[INDEX_MAX_KEYS]; + + /* + * Extract the index column values and isnull flags from the + * existing tuple. + */ + FormIndexDatum(indexInfo, existing_slot, estate, values, nulls); + + if (nulls[sk_attno - 1]) + { + all_not_null = false; + break; + } + } + + index_endscan(index_scan); + index_close(index, NoLock); + ExecDropSingleTupleTableSlot(existing_slot); + + /* exit earlier */ + if (!all_not_null) + { + FreeExecutorState(estate); + return false; + } + } + + FreeExecutorState(estate); + + return true; +} diff --git a/src/include/executor/executor.h b/src/include/executor/executor.h index fa2b657fb2ff..3034d642b886 100644 --- a/src/include/executor/executor.h +++ b/src/include/executor/executor.h @@ -756,6 +756,8 @@ extern void check_exclusion_constraint(Relation heap, Relation index, const Datum *values, const bool *isnull, EState *estate, bool newIndex); +extern bool index_check_notnull(Relation relation, List *notnull_attnums); + /* * prototypes from functions in execReplication.c */ diff --git a/src/test/isolation/expected/indexscan-check-notnull.out b/src/test/isolation/expected/indexscan-check-notnull.out new file mode 100644 index 000000000000..c6a652e2518e --- /dev/null +++ b/src/test/isolation/expected/indexscan-check-notnull.out @@ -0,0 +1,97 @@ +Parsed test spec with 2 sessions + +starting permutation: s1_b1 s2_b1 s1_d1 s2_a1 s1_c1 s2_c1 s1_q1 +step s1_b1: BEGIN ISOLATION LEVEL REPEATABLE READ; +step s2_b1: BEGIN ISOLATION LEVEL REPEATABLE READ; +step s1_d1: DELETE FROM t; +step s2_a1: ALTER TABLE t ADD CONSTRAINT t1_nn NOT NULL a; +step s1_c1: COMMIT; +step s2_a1: <... completed> +step s2_c1: COMMIT; +step s1_q1: SELECT conname, conrelid::regclass, contype, convalidated + FROM pg_constraint WHERE conname = 't1_nn'; + +conname|conrelid|contype|convalidated +-------+--------+-------+------------ +t1_nn |t |n |t +(1 row) + + +starting permutation: s1_b2 s2_b1 s1_d1 s2_a1 s1_c1 s2_c1 s1_q1 +step s1_b2: BEGIN; +step s2_b1: BEGIN ISOLATION LEVEL REPEATABLE READ; +step s1_d1: DELETE FROM t; +step s2_a1: ALTER TABLE t ADD CONSTRAINT t1_nn NOT NULL a; +step s1_c1: COMMIT; +step s2_a1: <... completed> +step s2_c1: COMMIT; +step s1_q1: SELECT conname, conrelid::regclass, contype, convalidated + FROM pg_constraint WHERE conname = 't1_nn'; + +conname|conrelid|contype|convalidated +-------+--------+-------+------------ +t1_nn |t |n |t +(1 row) + + +starting permutation: s1_b1 s2_b2 s1_d1 s2_a1 s1_c1 s2_c1 s1_q1 +step s1_b1: BEGIN ISOLATION LEVEL REPEATABLE READ; +step s2_b2: BEGIN; +step s1_d1: DELETE FROM t; +step s2_a1: ALTER TABLE t ADD CONSTRAINT t1_nn NOT NULL a; +step s1_c1: COMMIT; +step s2_a1: <... completed> +step s2_c1: COMMIT; +step s1_q1: SELECT conname, conrelid::regclass, contype, convalidated + FROM pg_constraint WHERE conname = 't1_nn'; + +conname|conrelid|contype|convalidated +-------+--------+-------+------------ +t1_nn |t |n |t +(1 row) + + +starting permutation: s1_b1 s2_b1 s2_a1 s1_r1 s2_r1 +step s1_b1: BEGIN ISOLATION LEVEL REPEATABLE READ; +step s2_b1: BEGIN ISOLATION LEVEL REPEATABLE READ; +step s2_a1: ALTER TABLE t ADD CONSTRAINT t1_nn NOT NULL a; +ERROR: column "a" of relation "t" contains null values +step s1_r1: ROLLBACK; +step s2_r1: ROLLBACK; + +starting permutation: s1_b1 s2_b2 s2_d1 s1_d1 s2_r1 s1_a1 s1_q1 s1_r1 +step s1_b1: BEGIN ISOLATION LEVEL REPEATABLE READ; +step s2_b2: BEGIN; +step s2_d1: DROP INDEX t_ab_idx; +step s1_d1: DELETE FROM t; +step s2_r1: ROLLBACK; +step s1_d1: <... completed> +step s1_a1: ALTER TABLE t ADD CONSTRAINT t1_nn NOT NULL a; +step s1_q1: SELECT conname, conrelid::regclass, contype, convalidated + FROM pg_constraint WHERE conname = 't1_nn'; + +conname|conrelid|contype|convalidated +-------+--------+-------+------------ +t1_nn |t |n |t +(1 row) + +step s1_r1: ROLLBACK; + +starting permutation: s1_b2 s2_b1 s1_d1 s2_d1 s1_s0 s1_a1 s1_c1 s2_c1 s1_q1 +step s1_b2: BEGIN; +step s2_b1: BEGIN ISOLATION LEVEL REPEATABLE READ; +step s1_d1: DELETE FROM t; +step s2_d1: DROP INDEX t_ab_idx; +step s1_s0: SAVEPOINT s0; +step s1_a1: ALTER TABLE t ADD CONSTRAINT t1_nn NOT NULL a; +step s1_c1: COMMIT; +step s2_d1: <... completed> +step s2_c1: COMMIT; +step s1_q1: SELECT conname, conrelid::regclass, contype, convalidated + FROM pg_constraint WHERE conname = 't1_nn'; + +conname|conrelid|contype|convalidated +-------+--------+-------+------------ +t1_nn |t |n |t +(1 row) + diff --git a/src/test/isolation/isolation_schedule b/src/test/isolation/isolation_schedule index 5afae33d3703..a5926dc81d73 100644 --- a/src/test/isolation/isolation_schedule +++ b/src/test/isolation/isolation_schedule @@ -59,6 +59,7 @@ test: merge-delete test: merge-update test: merge-match-recheck test: merge-join +test: indexscan-check-notnull test: delete-abort-savept test: delete-abort-savept-2 test: aborted-keyrevoke diff --git a/src/test/isolation/specs/indexscan-check-notnull.spec b/src/test/isolation/specs/indexscan-check-notnull.spec new file mode 100644 index 000000000000..05b4488550c9 --- /dev/null +++ b/src/test/isolation/specs/indexscan-check-notnull.spec @@ -0,0 +1,45 @@ + +# +# using indexscan to check a column not-null constraint +# is satisfied or not. +# + +setup +{ + CREATE TABLE t (a int, b int); + CREATE INDEX t_ab_idx on t(a,b); + INSERT INTO t values (null, 1); + INSERT INTO t SELECT x, x*10 FROM generate_series(1,3) g(x); +} + +teardown +{ + DROP TABLE t; +} + +session s1 +step s1_b1 { BEGIN ISOLATION LEVEL REPEATABLE READ; } +step s1_b2 { BEGIN; } +step s1_d1 { DELETE FROM t;} +step s1_s0 { SAVEPOINT s0;} +step s1_a1 { ALTER TABLE t ADD CONSTRAINT t1_nn NOT NULL a;} +step s1_q1 { SELECT conname, conrelid::regclass, contype, convalidated + FROM pg_constraint WHERE conname = 't1_nn'; + } +step s1_r1 { ROLLBACK; } +step s1_c1 { COMMIT; } + +session s2 +step s2_b1 { BEGIN ISOLATION LEVEL REPEATABLE READ; } +step s2_b2 { BEGIN; } +step s2_a1 { ALTER TABLE t ADD CONSTRAINT t1_nn NOT NULL a;} +step s2_d1 { DROP INDEX t_ab_idx;} +step s2_r1 { ROLLBACK; } +step s2_c1 { COMMIT; } + +permutation s1_b1 s2_b1 s1_d1 s2_a1 s1_c1 s2_c1 s1_q1 +permutation s1_b2 s2_b1 s1_d1 s2_a1 s1_c1 s2_c1 s1_q1 +permutation s1_b1 s2_b2 s1_d1 s2_a1 s1_c1 s2_c1 s1_q1 +permutation s1_b1 s2_b1 s2_a1 s1_r1 s2_r1 +permutation s1_b1 s2_b2 s2_d1 s1_d1 s2_r1 s1_a1 s1_q1 s1_r1 +permutation s1_b2 s2_b1 s1_d1 s2_d1 s1_s0 s1_a1 s1_c1 s2_c1 s1_q1 diff --git a/src/test/modules/test_misc/meson.build b/src/test/modules/test_misc/meson.build index f258bf1ccd94..cfcc7fa6a053 100644 --- a/src/test/modules/test_misc/meson.build +++ b/src/test/modules/test_misc/meson.build @@ -18,6 +18,7 @@ tests += { 't/007_catcache_inval.pl', 't/008_replslot_single_user.pl', 't/009_log_temp_files.pl', + 't/010_indexscan_validate_notnull.pl', ], }, } diff --git a/src/test/modules/test_misc/t/010_indexscan_validate_notnull.pl b/src/test/modules/test_misc/t/010_indexscan_validate_notnull.pl new file mode 100644 index 000000000000..a3f3cb409b66 --- /dev/null +++ b/src/test/modules/test_misc/t/010_indexscan_validate_notnull.pl @@ -0,0 +1,163 @@ +# Copyright (c) 2025, 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; + +# 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; +} + +# Check whether result of run_sql_command shows that we did a verify pass +sub is_table_verified +{ + my $output = shift; + return index($output, 'DEBUG: verifying table') != -1; +} + +# Check whether result of run_sql_command shows that we did a verify pass +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 (a int, b int, c int, d int ) PARTITION BY range(a); + CREATE TABLE tpp1 partition of tp for values from ( 1 ) to (10); + CREATE TABLE tpp2 partition of tp for values from ( 10 ) to (21); + INSERT INTO tp select g, g + 10, g + 15, g + random(min=>1::int, max=>10::int) from generate_series(1,19) g; + CREATE INDEX ON tp(b); '); + +$output = run_sql_command('ALTER TABLE tp ALTER COLUMN b SET NOT NULL;'); +ok(is_indexscan_veritify_notnull($output), + 'column b will use indexscan to verify not-null status, no need scan table'); +ok( $output =~ + m/all new not-null constraints on relation "tpp1" have been validated by using index scan/, + 'all newly added constraints proved by indexscan'); + +run_sql_command( + 'ALTER TABLE tp ALTER COLUMN b DROP NOT NULL;'); + +# use indexes on partitions to fast vertifing not-null should be ok +$output = run_sql_command('ALTER TABLE tpp1 ALTER COLUMN b SET NOT NULL;'); +ok(is_indexscan_veritify_notnull($output), 'use indexscan to validate column b not-null will not scan table'); +ok(!is_table_verified($output), 'no table scan for pp1'); +ok( $output =~ + m/all new not-null constraints on relation "tpp1" have been validated by using index scan/, + 'all newly added constraints proved by indexscan'); + +$output = run_sql_command('ALTER TABLE tpp2 ALTER COLUMN b SET NOT NULL;'); +ok(is_indexscan_veritify_notnull($output), 'use indexscan to validate column b not-null, will not scan table'); +ok(!is_table_verified($output), 'no table scan for tpp2'); +ok( $output =~ + m/all new not-null constraints on relation "tpp2" have been validated by using index scan/, + 'all newly added constraints proved by indexscan'); + +run_sql_command( + 'ALTER TABLE tp ALTER COLUMN b DROP NOT NULL;'); + +# table rewrite then can not use indexscan to verify not-null constraint +$output = run_sql_command( + 'ALTER TABLE tp ALTER COLUMN b SET NOT NULL, ALTER COLUMN b SET DATA TYPE bigint;' +); +ok(!is_table_verified($output), 'no table scan for tp'); +ok(!is_indexscan_veritify_notnull($output), 'table tp will rewrite, can not use indexscan to verify not-null constraints on it'); + +# test using indexscan with multiple not-null constraints +run_sql_command( + 'ALTER TABLE tp ALTER COLUMN b DROP NOT NULL; + CREATE INDEX ON tp(c); + CREATE INDEX ON tp(d); '); + +# one column can use indexscan, another can not, so can not use indexscan to verify not-null +$output = run_sql_command( + 'ALTER TABLE tp ALTER COLUMN a SET NOT NULL, ADD CONSTRAINT c_nn NOT NULL c'); +ok(is_table_verified($output), 'table scan for tp'); +ok(!is_indexscan_veritify_notnull($output), 'can not use indexscan to verify not-null constraints on table tp'); + +# all columns have index on it +# use indexscan to verify multiple not-null constraints should just fine +$output = run_sql_command( + 'ALTER TABLE tp ALTER COLUMN d SET NOT NULL, ADD CONSTRAINT c_nn NOT NULL c, ALTER COLUMN b SET NOT NULL;'); +ok(!is_table_verified($output), 'no table scan for tp'); +ok(is_indexscan_veritify_notnull($output), + 'using indexscan to verify not-null constraints on column b, c, d, no need table scan'); + +# test with non-partitioned table and other non-Btree index. +run_sql_command( + 'CREATE TABLE t1(f1 INT, f2 int, f3 int,f4 int, f5 int); + INSERT INTO t1 select g, g+1, g+2, g+3, g+4 from generate_series(1, 100) g; + CREATE INDEX t1_f1_f2_idx ON t1(f1,f2); + CREATE UNIQUE INDEX t1_f3idx ON t1(f3); + CREATE INDEX t1_f3f4idx ON t1(f3) include(f4); + CREATE INDEX hash_f5_idx ON t1 USING hash (f5 int4_ops); '); + +# ALTER TABLE VALIDATE CONSTRAINT can not use indexscan, +# because it only hold ShareUpdateExclusiveLock lock. +run_sql_command('ALTER TABLE t1 ADD CONSTRAINT nn_t1_f2 NOT NULL f2 NOT VALID;'); + +$output = run_sql_command( + 'ALTER TABLE t1 VALIDATE CONSTRAINT nn_t1_f2;' +); +ok(!is_indexscan_veritify_notnull($output), 'can not use indexscan for ALTER TABLE VALIDATE CONSTRAINT command'); +run_sql_command('ALTER TABLE t1 DROP CONSTRAINT nn_t1_f2;'); + +$output = run_sql_command( + 'ALTER TABLE t1 ALTER COLUMN f1 SET NOT NULL, ALTER f1 type bigint;' +); +ok(!is_indexscan_veritify_notnull($output), 'table t1 will rewrite, can not use indexscan to validate not-null constraints on t1'); + +run_sql_command('ALTER TABLE t1 ALTER COLUMN f1 DROP NOT NULL;'); +run_sql_command('ALTER TABLE t1 ADD CONSTRAINT nn NOT NULL f1, ALTER f1 type bigint;'); + +# can use indexscan mechanism to fast add multiple not-null constraints +$output = run_sql_command( + 'ALTER TABLE t1 ALTER COLUMN f2 SET NOT NULL, ALTER COLUMN f1 SET NOT NULL;' +); +ok( $output =~ + m/all new not-null constraints on relation "t1" have been validated by using index scan/, + 'all not-null constraints are validated by indexscan'); + +# using indexscan mechanism to fast add a not-null can only apply to key columns, not include column +$output = run_sql_command( + 'ALTER TABLE t1 ADD CONSTRAINT nnf4 NOT NULL f4;' +); +ok(!is_indexscan_veritify_notnull($output), 'can not use indexscan to validate not-null constraint on t1'); + +# if index is not btree index then cannot use indexscan mechanism to fast add not-null constraint +$output = run_sql_command( + 'ALTER TABLE t1 ADD CONSTRAINT nn_f5 NOT NULL f5;' +); +ok(!is_indexscan_veritify_notnull($output), 'can not use indexscan to validate not-null constraints on t1'); + +$node->stop('fast'); + +done_testing();