I think the testing discussion should be moved to a different thread.
What do you think?
See v4.

0001 deals with reporting queryId in exec_execute_message and exec_bind_message.
0002 deals with reporting queryId after a cache invalidation.

There are no tests as this requires more discussion in a separate thread(?)


Regards,


Sami




From 0727a81033c34350ddc782366ea4f8b4061cdf6e Mon Sep 17 00:00:00 2001
From: Sami Imseih <samims...@gmail.com>
Date: Wed, 14 Aug 2024 20:50:01 +0000
Subject: [PATCH v4 1/2] Add missing queryId reporting in extended query

---
 src/backend/tcop/postgres.c | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 8bc6bea113..5ef59deeed 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -1654,6 +1654,7 @@ exec_bind_message(StringInfo input_message)
        char            msec_str[32];
        ParamsErrorCbData params_data;
        ErrorContextCallback params_errcxt;
+       ListCell   *lc;
 
        /* Get the fixed part of the message */
        portal_name = pq_getmsgstring(input_message);
@@ -1689,6 +1690,17 @@ exec_bind_message(StringInfo input_message)
 
        pgstat_report_activity(STATE_RUNNING, psrc->query_string);
 
+       foreach(lc, psrc->query_list)
+       {
+               Query *query = lfirst_node(Query, lc);
+
+               if (query->queryId != UINT64CONST(0))
+               {
+                       pgstat_report_query_id(query->queryId, false);
+                       break;
+               }
+       }
+
        set_ps_display("BIND");
 
        if (save_log_statement_stats)
@@ -2111,6 +2123,7 @@ exec_execute_message(const char *portal_name, long 
max_rows)
        ErrorContextCallback params_errcxt;
        const char *cmdtagname;
        size_t          cmdtaglen;
+       ListCell   *lc;
 
        /* Adjust destination to tell printtup.c what to do */
        dest = whereToSendOutput;
@@ -2157,6 +2170,17 @@ exec_execute_message(const char *portal_name, long 
max_rows)
 
        pgstat_report_activity(STATE_RUNNING, sourceText);
 
+       foreach(lc, portal->stmts)
+       {
+               PlannedStmt *stmt = lfirst_node(PlannedStmt, lc);
+
+               if (stmt->queryId != UINT64CONST(0))
+               {
+                       pgstat_report_query_id(stmt->queryId, false);
+                       break;
+               }
+       }
+
        cmdtagname = GetCommandTagNameAndLen(portal->commandTag, &cmdtaglen);
 
        set_ps_display_with_len(cmdtagname, cmdtaglen);
-- 
2.43.0

From f0cf4fb884e3a75940300691ce0c7908f2f39cad Mon Sep 17 00:00:00 2001
From: Sami Imseih <samims...@gmail.com>
Date: Wed, 14 Aug 2024 20:50:37 +0000
Subject: [PATCH v4 2/2] Report new queryId after plancache re-validation

---
 src/backend/utils/cache/plancache.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/src/backend/utils/cache/plancache.c 
b/src/backend/utils/cache/plancache.c
index 5af1a168ec..aa507846d6 100644
--- a/src/backend/utils/cache/plancache.c
+++ b/src/backend/utils/cache/plancache.c
@@ -66,6 +66,7 @@
 #include "storage/lmgr.h"
 #include "tcop/pquery.h"
 #include "tcop/utility.h"
+#include "utils/backend_status.h"
 #include "utils/inval.h"
 #include "utils/memutils.h"
 #include "utils/resowner.h"
@@ -590,6 +591,7 @@ RevalidateCachedQuery(CachedPlanSource *plansource,
        TupleDesc       resultDesc;
        MemoryContext querytree_context;
        MemoryContext oldcxt;
+       ListCell        *lc;
 
        /*
         * For one-shot plans, we do not support revalidation checking; it's
@@ -805,6 +807,21 @@ RevalidateCachedQuery(CachedPlanSource *plansource,
 
        plansource->is_valid = true;
 
+       /*
+        * Override the current queryId with the one from the re-validated
+        * query tree.
+        */
+       foreach(lc, tlist)
+       {
+               Query *stmt = lfirst_node(Query, lc);
+
+               if (stmt->queryId != UINT64CONST(0))
+               {
+                       pgstat_report_query_id(stmt->queryId, true);
+                       break;
+               }
+       }
+
        /* Return transient copy of querytrees for possible use in planning */
        return tlist;
 }
-- 
2.43.0

Reply via email to