From 071ffc0ac50aa9d5e0d8bb7c9fbf07c69e6e628f Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Tue, 4 Apr 2023 21:32:43 +0000 Subject: [PATCH 1/1] Fix row tracking in pg_stat_statements. In pg_stat_statements, EState->es_processed cannot reliably be used to count the rows processed for a statement that goes through extended query protocol. This is because, such a statement may execute a portal more than once, and es_processed is reset after every execution. This fix addresses this issue by accuumulating the # row processed for all the executions of a portal. Author: Sami Imseih Discussion: https://www.postgresql.org/message-id/flat/EBE6C507-9EB6-4142-9E4D-38B1673363A7%40amazon.com --- contrib/pg_stat_statements/pg_stat_statements.c | 2 +- src/backend/executor/execMain.c | 17 +++++++++++++++++ src/include/nodes/execnodes.h | 5 ++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c index 5285c3f7fa..68215bb2e3 100644 --- a/contrib/pg_stat_statements/pg_stat_statements.c +++ b/contrib/pg_stat_statements/pg_stat_statements.c @@ -1060,7 +1060,7 @@ pgss_ExecutorEnd(QueryDesc *queryDesc) queryDesc->plannedstmt->stmt_len, PGSS_EXEC, queryDesc->totaltime->total * 1000.0, /* convert to msec */ - queryDesc->estate->es_processed, + queryDesc->estate->es_total_processed, &queryDesc->totaltime->bufusage, &queryDesc->totaltime->walusage, queryDesc->estate->es_jit ? &queryDesc->estate->es_jit->instr : NULL, diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index 1b007dc32c..d9164c8245 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -372,6 +372,15 @@ standard_ExecutorRun(QueryDesc *queryDesc, execute_once); } + /* + * If this is a SELECT, add the tuples processed in this + * execution of a portal to the total processed. + * For other operation types, the total processed is set + * during executorFinish. + */ + if (operation == CMD_SELECT) + estate->es_total_processed += estate->es_processed; + /* * shutdown tuple receiver, if we started it */ @@ -412,6 +421,7 @@ standard_ExecutorFinish(QueryDesc *queryDesc) { EState *estate; MemoryContext oldcontext; + CmdType operation = queryDesc->operation; /* sanity checks */ Assert(queryDesc != NULL); @@ -441,6 +451,13 @@ standard_ExecutorFinish(QueryDesc *queryDesc) if (queryDesc->totaltime) InstrStopNode(queryDesc->totaltime, 0); + /* + * For non-SELECT operations, es_total_processed will always be + * equal to es_processed. + */ + if (operation != CMD_SELECT) + queryDesc->estate->es_total_processed = queryDesc->estate->es_processed; + MemoryContextSwitchTo(oldcontext); estate->es_finished = true; diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index d97f5a8e7d..ee20cd5c8a 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -661,7 +661,10 @@ typedef struct EState List *es_tupleTable; /* List of TupleTableSlots */ - uint64 es_processed; /* # of tuples processed */ + uint64 es_processed; /* # of tuples processed for a single + * execution of a portal */ + uint64 es_total_processed; /* # of tuples processed for all + * executions of a portal */ int es_top_eflags; /* eflags passed to ExecutorStart */ int es_instrument; /* OR of InstrumentOption flags */ -- 2.39.2