Re: using index to speedup add not null constraints to a table

Поиск
Список
Период
Сортировка
Искать

Re: using index to speedup add not null constraints to a table

От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
Can you please rebase this?  It stopped applying a week ago.

Thanks!

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/
"Sallah, I said NO camels! That's FIVE camels; can't you count?"
(Indiana Jones)


Re: using index to speedup add not null constraints to a table

От:
jian he <jian.universality@gmail.com>
Дата:
Вложения:
On Fri, Oct 17, 2025 at 3:57 AM Álvaro Herrera  wrote:
> Can you please rebase this?  It stopped applying a week ago.
>

hi.

rebase and minor polishment.

Re: using index to speedup add not null constraints to a table

От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
Вложения:
Hello

I rebased this patch to review it.  Overall, I think this is a great
idea.  Here are some comments on the patch, which I hope are not
anything major.

I'm not really sure that I agree with the way ATRewriteTable works now.
It seems that we scan the list of columns, and verify each one for nulls,
but abort midway if a column is generated.  Surely we should check for
generated columns first, to avoid wasting time verifying earlier
columns in case a later column is generated?

That said, we do check for notnull_virtual_attrs to be NIL.  It seems to
me that this implies that the checked columns are not generated.  In
other words, the test for whether columns are generated is unnecessary
and confusing, and in which case you don't have to change anything, just
remove the "if (attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)"
inner block.

However, if we do this, then I think computing notnull_attrs is
pointless.  So we should only change the order: do this new check first,
and if we find that any new not-null column is on a generated column,
then we compute both notnull_virtual_attrs and notnull_attrs.  No?


The separation of labor between index_check_notnull() and
index_check_notnull_internal() seems a bit pointless.  Why not have a
single routine that does both things?  Though, to be honest, it does
look like the former should live in tablecmds.c instead of
execIndexing.c.  (But if you do split things that way, then you need to
make index_check_notnull_internal extern).

The tests look a bit excessive.  Why do we need an isolation test for
this?  And it looks to me like the other test could be in
src/test/regress rather than be a TAP test.  After all, that's what you
have the ereport(DEBUG1) there, isn't it?

"veritify" doesn't exist.  The word is "verify".

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
Syntax error: function hell() needs an argument.
Please choose what hell you want to involve.

Re: using index to speedup add not null constraints to a table

От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
On 2026-Jan-13, Álvaro Herrera wrote:

> However, if we do this, then I think computing notnull_attrs is
> pointless.  So we should only change the order: do this new check
> first, and if we find that any new not-null column is on a generated
> column, then we compute both notnull_virtual_attrs and notnull_attrs.
> No?

Oh, another thing we should do is have a first pass that verifies
whether all columns have an appropriate index, without scanning any of
them; only if we verify that they all have one (and no generated column
is involved) then we start scanning the indexes.  Otherwise we waste
time scanning one index and verify that it contains no null values, only
to realize that the next column does not have an appropriate index to
use, and thus we must scan the table.  Then the first index scan is
wasted work.

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
"The saddest aspect of life right now is that science gathers knowledge faster
 than society gathers wisdom."  (Isaac Asimov)


Re: using index to speedup add not null constraints to a table

От:
jian he <jian.universality@gmail.com>
Дата:
Вложения:
hi.

---------------------
create unlogged table t2_copy(col1 int, col2 int, col3 int, col4 int,
col5 int) with (autovacuum_enabled = off, vacuum_index_cleanup=off);
insert into t2_copy select g, g, g, g,g from generate_series(1, 10_000_000) g;
set enable_seqscan to off;
set enable_bitmapscan to off;
set max_parallel_workers=0;
set max_parallel_workers_per_gather=0;

create index t2_copy_idx on t2_copy(col1, col2, col3, col4, col5);
explain (costs off, analyze) select from t2_copy where col1 is NULL or
col2 is NULL or col3 is NULL or col4 is null or col5 is null \watch
i=0.1 c=10

drop index t2_copy_idx;
create index t2_copy_idx1 on t2_copy(col1);
create index t2_copy_idx2 on t2_copy(col2);
create index t2_copy_idx3 on t2_copy(col3);
create index t2_copy_idx4 on t2_copy(col4);
create index t2_copy_idx5 on t2_copy(col5);
explain (costs off, analyze) select from t2_copy where col1 is NULL or
col2 is NULL or col3 is NULL or col4 is null or col5 is null \watch
i=0.1 c=10
------------------------------------------
By comparing the above two EXPLAIN, I found out that there will be regression
for using one multiple column indexes fast verify mutiple not-null constraints.

create unlogged table t3_copy(col1 int, col2 int, col3 int, col4 int);
create index t3_copy_idx on t3_copy(col1, col2, col3, col4);
alter table t3_copy add not null col1, add not null col2, add not null
col3, add not null col4;

In this case, scanning the t3_copy_idx index to check these four NOT NULL
constraints is actually slower than a full table scan.

Therefore, I further restricted the use of the index-scan mechanism for fast NOT
NULL verification to scenarios where the attribute being checked is the leading
column of the index.

So, for the above example, to let

alter table t3_copy add not null col1, add not null col2, add not null
col3, add not null col4;

utilize indexscan mechanism, we require four indexes, each index leading column
is corresponding to the not-null constraint attribute.

I have not yet addressed all of your other comments, as this
represents a more of a
major change. Therefore, I am submitting the attached patch first.

index_check_notnull function, in v8 it is:

+ /* collect index attnums while loop */
+ for (int i = 0; i < index->indnkeyatts; i++)
+ {
+ attr = TupleDescAttr(tupdesc, (index->indkey.values[i] - 1));
+
+
+ }

in v9, it's:
+ /* collect index attnums while loop */
+ attr = TupleDescAttr(tupdesc, (index->indkey.values[0] - 1));
+

I am not sure that regression tests would be helpful, since the test hinges on
whether ereport(DEBUG1, ...) is reached.

I am not sure about the concurrency implications; therefore, isolation tests are
attached.  maybe it's not necessary.



--
jian
https://www.enterprisedb.com

Re: using index to speedup add not null constraints to a table

От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
On 2026-Jan-16, jian he wrote:

> In this case, scanning the t3_copy_idx index to check these four NOT NULL
> constraints is actually slower than a full table scan.
> 
> Therefore, I further restricted the use of the index-scan mechanism for fast NOT
> NULL verification to scenarios where the attribute being checked is the leading
> column of the index.

Makes sense.

> I have not yet addressed all of your other comments, as this
> represents a more of a
> major change. Therefore, I am submitting the attached patch first.

OK, this means we're still waiting then.

Please do see my v8 patch.  It contained other cleanups as well.

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
"La gente vulgar sólo piensa en pasar el tiempo;
el que tiene talento, en aprovecharlo"


Re: using index to speedup add not null constraints to a table

От:
jian he <jian.universality@gmail.com>
Дата:
Вложения:
hi.

Please check the attached v10.

Turns out regress test is doable, I just need to
SET client_min_messages TO DEBUG1;

It is now implemented as a single static function, index_check_notnull, located
in tablecmd.c. I believe I have addressed all of your other points as well.


--
jian
https://www.enterprisedb.com/

Re: using index to speedup add not null constraints to a table

От:
jian he <jian.universality@gmail.com>
Дата:
Вложения:
hi.

v11 is attached.
Mainly fixes regress test failures.



--
jian
https://www.enterprisedb.com/

Re: using index to speedup add not null constraints to a table

От:
jian he <jian.universality@gmail.com>
Дата:
Вложения:
On Wed, Feb 4, 2026 at 8:52 PM jian he  wrote:
>
> hi.
>
> v11 is attached.
> Mainly fixes regress test failures.

I have tested locally on https://cirrus-ci.com/build/5064590561640448
this time, the attachment should have no regress test failure.



--
jian
https://www.enterprisedb.com/

Re: using index to speedup add not null constraints to a table

От:
jian he <jian.universality@gmail.com>
Дата:
Вложения:
On Fri, Feb 6, 2026 at 12:58 PM jian he  wrote:
>

+-- should produce a debug message containing "have been validated by
using index scan"
+ ALTER TABLE tp_notnull ALTER COLUMN b SET NOT NULL, ALTER COLUMN b
SET DATA TYPE BIGINT;
DEBUG:  rehashing catalog cache id 64 for pg_class; 257 tups, 128 buckets

The above DEBUG message make me realize that
+SET client_min_messages TO 'debug1';
+--debug message should contain "have been validated by using index scan"
+ALTER TABLE tp_notnull ALTER COLUMN b SET NOT NULL;
+DEBUG:  all new not-null constraints on relation "tp_notnull_1" have
been validated by using index scan
+DEBUG:  all new not-null constraints on relation "tp_notnull_2" have
been validated by using index scan

Using the above regression test to check if certain
``ereport(DEBUG1,`` is reached is unstable.
Therefore, we fall back to the previously used TAP tests to ensure
    ereport(DEBUG1,
            errmsg_internal("all new not-null constraints on relation
\"%s\" have been validated by using index scan",
                            RelationGetRelationName(oldrel)));
is being reached.

Rebased and made some minor comment adjustments; no big change.



--
jian
https://www.enterprisedb.com/

Re: using index to speedup add not null constraints to a table

От:
Álvaro Herrera <alvherre@kurilemu.de>
Дата:
Hello,

On 2026-Apr-07, jian he wrote:

> Rebased and made some minor comment adjustments; no big change.

I think you may have not looked at my review at 
https://postgr.es/m/202601131715.cheonohttbyj@alvherre.pgsql
because as far as I can tell, this new version has essentially the same
structure as before that review.

-- 
Álvaro Herrera        Breisgau, Deutschland  —  https://www.EnterpriseDB.com/


Re: using index to speedup add not null constraints to a table

От:
jian he <jian.universality@gmail.com>
Дата:
Вложения:
On Tue, Apr 7, 2026 at 6:02 PM Álvaro Herrera  wrote:
>
> Hello,
>
> I think you may have not looked at my review at
> https://postgr.es/m/202601131715.cheonohttbyj@alvherre.pgsql
> because as far as I can tell, this new version has essentially the same
> structure as before that review.
>

Hi.

I guess the point I haven't addressed yet is:

> However, if we do this, then I think computing notnull_attrs is
> pointless.  So we should only change the order: do this new check first,
> and if we find that any new not-null column is on a generated column,
> then we compute both notnull_virtual_attrs and notnull_attrs.  No?
>

The attached patch implements this reordering,
first do index_check_notnull then compute both notnull_virtual_attrs
and notnull_attrs.



--
jian
https://www.enterprisedb.com/

Re: using index to speedup add not null constraints to a table

От:
jian he <jian.universality@gmail.com>
Дата:
Вложения:
Hi.
Please check the attached v16.

The condition for using indexscan to verify the column has not changed.
What changed: index_check_notnull logic simplifction, common helper
function: queue_notnull_validation
for setting up tab->constraints and tab->verify_new_notnull.

I also used the attached bench.txt to benchmark.



--
jian
https://www.enterprisedb.com/

Re: using index to speedup add not null constraints to a table

От:
Baji Shaik <baji.pgdev@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 
FAQ