| From: | "Alberto Piai" <alberto(dot)piai(at)gmail(dot)com> |
|---|---|
| To: | "Antonin Houska" <ah(at)cybertec(dot)at>, "Manu" <manuelreyesbravo(at)gmail(dot)com> |
| Cc: | "Alberto Piai" <alberto(dot)piai(at)gmail(dot)com>, "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 11:59:23 |
| Message-ID: | DN49WENZXU2W.NZRYHQWDF8ED@gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Antonin and Manu,
On Mon Nov 23, 2026 at 7:29 PM CET, Antonin Houska wrote:
> v03 shows the the inner-most command as well as the outer-most one. Anything
> in between is not displayed. For more information please see the commit
> message of v03-0001.
I agree with your reasoning in the commit message, showing the inner-most
(actively started by the user) + outer-most (currently running) commands
should give enough useful information while still being able to use a
fixed-size memory area.
> I considered the stack approach proposed by Alberto [1], where a sub-command
> is ignored if its nested too deeply, but I realized that it wouldn't work. The
> problem is that some commands (mis)use the progress monitoring infrastructure
> to get information for their own purposes, e.g. this in analyze.c:
>
> /*
> * We bypass the changecount mechanism because this value is
> * only updated by the calling process.
> */
> appendStringInfo(&buf, _("delay time: %.3f ms\n"),
> (double) MyBEEntry->st_progress.param[PROGRESS_ANALYZE_DELAY_TIME] / 1000000.0);
Ah yes, I saw cases like this too, my intuition would have been to
change these readers to a pgstat_progress_*() call which accesses the
correct entry (the current one) in the stack. But anyway your approach
with the first and last command is much better, so we don't need to
think about this anymore :)
I started reviewing v3, and noticed a couple of problems and have some
thoughts:
[[local]] albe(at)benchdb =# reindex index concurrently pgbench_accounts_pkey;
REINDEX
[[local]] albe(at)benchdb =# select * from pg_stat_progress_create_index;
─[ RECORD 1 ]──────┬────────────────────────────────────
pid │ 23894
datid │ 16787
datname │ benchdb
relid │ 16868
index_relid │ 0
command │ ¤
phase │ waiting for readers before dropping
lockers_total │ 0
lockers_done │ 0
current_locker_pid │ 0
blocks_total │ 0
blocks_done │ 0
tuples_total │ 0
tuples_done │ 0
partitions_total │ 0
partitions_done │ 0
It seems like the calls to start/end command in
ReindexRelationConcurrently() are not balanced, leaving an entry stuck
there after the command has completed.
Re-running with the following fixes it:
```
diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index ce6900c5b74..be29ef56b5c 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -4456,6 +4456,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
* transaction, and do our wait before any snapshot has been taken in
* it.
*/
+ pgstat_progress_end_command(false);
CommitTransactionCommand();
StartTransactionCommand();
@@ -4468,6 +4469,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
* Because we don't take a snapshot or Xid in this transaction,
* there's no need to set the PROC_IN_SAFE_IC flag here.
*/
+ pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, newidx->tableId);
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
PROGRESS_CREATEIDX_PHASE_WAIT_3);
WaitForOlderSnapshots(limitXmin, true);
@@ -4595,6 +4597,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
}
/* Commit this transaction to make the updates visible. */
+ pgstat_progress_end_command(false);
CommitTransactionCommand();
StartTransactionCommand();
@@ -4611,6 +4614,7 @@ ReindexRelationConcurrently(const ReindexStmt *stmt, Oid relationOid, const Rein
*/
INJECTION_POINT("reindex-relation-concurrently-before-drop", NULL);
+ pgstat_progress_start_command(PROGRESS_COMMAND_CREATE_INDEX, relationOid);
pgstat_progress_update_param(PROGRESS_CREATEIDX_PHASE,
PROGRESS_CREATEIDX_PHASE_WAIT_5);
WaitForLockersMultiple(lockTags, AccessExclusiveLock, true);
```
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.
This is not possible anymore (correct me if I'm not reading this right)
because StartTransaction() and StartSubTransaction() reset
CurrentTransactionState->progress_depth to 0. One problem with this is
that progress_depth and the actual stack in beentry->st_progress can get
out of sync, but the fundamental change here is that progress reporting
is now tied to committing/starting a transaction, whereas before it was
only reset in Abort(Sub)Transaction.
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?
In fact, I wonder why do we need to expose this in xact.c at all? Would
it be possible to keep all state internal to backend_progress.c and
cleanup the whole stack / only the inner-most item depending only on
whether the user called ...end_command(xact) with xact true or false?
If we reset the state only on abort, we could probably do this without
touching xact.c and the transaction state, which would also get rid of
the problem of it going out of sync with the stack in the backend state.
Regards,
Alberto
--
Alberto Piai
Sensational AG
Zürich, Switzerland
| From | Date | Subject | |
|---|---|---|---|
| Next Message | vignesh C | 2026-11-25 12:01:46 | Wrong table in "conflicting or redundant WHERE clauses / column lists" publicaiton error |
| Previous Message | Nisha Moond | 2026-11-25 11:53:29 | Re: Proposal: Conflict log history table for Logical Replication |