Hi Antonin,

Thanks for Cc-ing me. I tested and found two problems.

1. The progress row stays after CREATE INDEX CONCURRENTLY and REINDEX
CONCURRENTLY.

    CREATE TABLE h (a int, b text);
    INSERT INTO h SELECT g, repeat('x', 10)
      FROM generate_series(1, 20000) g;
    CREATE INDEX CONCURRENTLY h_a ON h (a);
    SELECT command, phase FROM pg_stat_progress_create_index
      WHERE pid = pg_backend_pid();

              command          |           phase
    ---------------------------+---------------------------
     CREATE INDEX CONCURRENTLY | waiting for old snapshots

The command has finished, but the row is still there. In the same
session, a plain CREATE INDEX then updates this row and leaves it
behind again, and an error does not remove it:

    CREATE INDEX h_b ON h (b);
    SELECT 1/0;
    SELECT command, phase FROM pg_stat_progress_create_index
      WHERE pid = pg_backend_pid();

              command          |                 phase
    ---------------------------+----------------------------------------
     CREATE INDEX CONCURRENTLY | building index: loading tuples in tree

In a new session, REINDEX TABLE CONCURRENTLY h leaves this row:

    ---------+-------------------------------------
             | waiting for readers before dropping

And a CREATE UNIQUE INDEX CONCURRENTLY that fails on a duplicate key
leaves this one:

              command          |                phase
    ---------------------------+-------------------------------------
     CREATE INDEX CONCURRENTLY | building index: sorting live tuples

On master, all of these queries return no row.

These commands start the report in one transaction and end it in a
later one, and StartTransaction() resets progress_depth to 0. When I
remove that reset, none of the three cases leaves a row.

2. In a parallel BRIN build, the leader's row loses its command and
phase, and the workers get rows of their own.

    CREATE TABLE big AS
      SELECT g a, g % 97 b FROM generate_series(1, 4000000) g;
    ALTER TABLE big SET (parallel_workers = 2);
    SET max_parallel_maintenance_workers = 2;
    SET min_parallel_table_scan_size = 0;
    CREATE INDEX big_brin ON big USING brin (a);

I sampled pg_stat_progress_create_index from another session about
every 1 ms during the build, joined with pg_stat_activity.

master, 101 samples, all the same:

    client backend    CREATE INDEX   building index

v03, 104 of 106 samples:

    client backend    command NULL   initializing
    parallel worker   command NULL   initializing
    parallel worker   command NULL   initializing

The other 2 samples show "CREATE INDEX, building index" for the
leader. blocks_total and blocks_done still advance in the leader's
row.

-- 
Adam


Reply via email to