Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: postgresql-cfbot/postgresql
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: faced8e
Choose a base ref
...
head repository: postgresql-cfbot/postgresql
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 2d14797
Choose a head ref
  • 2 commits
  • 8 files changed
  • 2 contributors

Commits on Apr 29, 2025

  1. using indexscan to speedup add not null constraints

    This patch tries to use index_beginscan() / index_getnext() / index_endscan()
    mentioned in [1] to speedup adding not-null constraints to the existing table.
    
    the main logic happen in phase3 ATRewriteTable
    1. collect all not-null constraints.
    2. does each not-null constraint have a corresponding index to vertify it, if
       not then do the normal processing.
    3. if table scan, table rewrite, table was not in AccessExclusiveLock.
       not-null constraint was on virtual generated column
       then we can not use indexcan to fast check not-null constraint.
    3. If a table scan or rewrite occurs and the table is not held with an
       AccessExclusiveLock, and the NOT NULL constraint is on a virtual generated
       column, then we can not use indexscan to fast validate not-null constraints.
    
    concurrency concern:
    ALTER TABLE SET NOT NULL will take an ACCESS EXCLUSIVE lock, so there is less
    variant of racing issue can occur?  to prove accurate, I wrote some isolation
    tests. see[2]
    
    performance:
    demo:
    case when: %20 percent values are NULL and have been deleted from heap but they
    still on the index.
    
    drop table if exists t;
    create unlogged table t(a int, b int) with (autovacuum_enabled = off, vacuum_index_cleanup=off);
    insert into t select case when g % 5 = 0 then null else g end, g+1
    from generate_series(1,1_000_000) g;
    create index t_idx_a on t(a);
    delete from t where a is null;
    
    alter table t alter column a drop not null;
    alter table t add constraint t1 not null a;
    
    the above two statement running serval times:
    patch Time:: 1.084  ms
    master Time: 12.045 ms
    
    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
    jianhe-fun authored and Commitfest Bot committed Apr 29, 2025
    Configuration menu
    Copy the full SHA
    45db07d View commit details
    Browse the repository at this point in the history
  2. [CF 5444] v5 - using index to speedup add not null constraints to a t…

    …able
    
    This branch was automatically generated by a robot using patches from an
    email thread registered at:
    
    https://commitfest.postgresql.org/patch/5444
    
    The branch will be overwritten each time a new patch version is posted to
    the thread, and also periodically to check for bitrot caused by changes
    on the master branch.
    
    Patch(es): https://www.postgresql.org/message-id/CACJufxHdR0Rd4-v78XhH+xNungof=zQrcTZK0GSN16zoNHWdHg@mail.gmail.com
    Author(s): Jian He
    Commitfest Bot committed Apr 29, 2025
    Configuration menu
    Copy the full SHA
    2d14797 View commit details
    Browse the repository at this point in the history
Loading