Re: Allow progress tracking of sub-commands

From: Manu <manuelreyesbravo(at)gmail(dot)com>
To: Alberto Piai <alberto(dot)piai(at)gmail(dot)com>
Cc: Antonin Houska <ah(at)cybertec(dot)at>, Rahila Syed <rahilasyed90(at)gmail(dot)com>, Fujii Masao <masao(dot)fujii(at)gmail(dot)com>, alvherre(at)kurilemu(dot)de, pgsql-hackers(at)lists(dot)postgresql(dot)org, bharath(dot)rupireddyforpostgres(at)gmail(dot)com, mihailnikalayeu(at)gmail(dot)com, adam8157(at)gmail(dot)com
Subject: Re: Allow progress tracking of sub-commands
Date: 2026-11-25 20:23:47
Message-ID: 179563822756.1989695.9515124812500326000@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Alberto,

I tried your idea on top of v03: keep the state in backend_progress.c
and clean up only on abort. It removes the rows that Adam and you
found after CREATE INDEX CONCURRENTLY and REINDEX CONCURRENTLY, and it
also fixes the cursor case from my last mail, without the change I
proposed there. The part that needs care is the abort of a
subtransaction.

One correction to my last mail: the tree I described there leaves
Adam's rows behind too. "Fixed all of these" only covered my cases.

On Wed, Nov 25, 2026 at 12:59 PM Alberto Piai
<alberto(dot)piai(at)gmail(dot)com> wrote:
> Perhaps it would be enough to reset the state only on abort, so that
> commands like ReindexRelationConcurrently() can keep reporting one
> command with multiple phases?

If AbortSubTransaction() empties the whole stack, an error caught
inside a command ends that command's report:

CREATE FUNCTION f(x int) RETURNS int LANGUAGE plpgsql IMMUTABLE AS
$$ BEGIN RETURN 1 / (x % 2);
EXCEPTION WHEN division_by_zero THEN RETURN 0; END $$;
CREATE TABLE t AS SELECT generate_series(1, 300000) a;
CREATE INDEX ON t (f(a));

The CREATE INDEX row goes away at the first caught error. Master
does the same; v03 keeps the row. Popping only the top entry gives
the same result here, because the subtransaction started no command.

Popping one entry fails the other way when the subtransaction started
two. A COPY into t22 has a trigger that runs a COPY into t23 inside
BEGIN ... EXCEPTION, and t23's own trigger runs a COPY that fails.
Only one entry is popped, so for the rest of the outer COPY the view
shows t23, and when it ends a t22 row stays behind, also after a
later CREATE INDEX.

What worked: count the commands started so far, save that count when
a subtransaction starts, and on its abort end the commands started
after it. With that, both cases above, Adam's four, your REINDEX
INDEX CONCURRENTLY and my cursor case leave no row. It still needs
one field in TransactionStateData, but nothing that must be kept in
step with the stack. The diff is attached, on top of Antonin's two
patches. make check, isolation, file_fdw and injection_points pass,
with progress.spec added to the schedule.

> however I'm not sure ReindexRelationConcurrently() needs to create
> all these subcommands. It would be enough to update the current
> phase of the last command, as it was doing before the patch.

I agree, and the row during the command shows why. With another
session holding ACCESS SHARE on the table, REINDEX INDEX CONCURRENTLY
shows command NULL and index_relid 0 while it waits for that reader,
and between some phases there is no row at all. Master shows REINDEX
CONCURRENTLY the whole time. Fixing the leftover row does not change
this, and with your diff the wait for old snapshots shows command NULL
too.

About Adam's parallel BRIN build: v03 adds a start_command() /
end_command() pair around the scan in _brin_parallel_scan_and_build(),
which runs in the leader and in each worker. In the leader the new
entry starts with all parameters at zero, and the view prefers it to
the outer one, so the row shows command NULL and "initializing" while
the block counters move. Without those lines the rows match master.

The file_fdw problems from my last mail are separate: they come from
active_nodes and pgstat_track_progress in file_fdw, and they show up
with all of these approaches.

Regards,
Manu

Attachment Content-Type Size
nocfbot-progress-subxact-mark.diff.txt text/plain 6.4 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Nathan Bossart 2026-11-25 20:42:37 Re: passwordcheck: Make the user name check case-insensitive
Previous Message Masahiko Sawada 2026-11-25 20:02:54 Re: Fix LogStandbySnapshot() race during logical decoding activation