This is an automated email from the ASF dual-hosted git repository.

tuhaihe pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 43c3c269990caae178e80b5e6c2349b00b99e5c8
Author: liushengsong <[email protected]>
AuthorDate: Mon Jun 1 17:16:04 2026 +0800

    Fix pg_stat_activity sess_id not updated after gang reset
    
    After a gang loss (e.g. QE terminated by pg_terminate_backend), the QD
    assigns a new gp_session_id via GpDropTempTables(), but the QD's
    pg_stat_activity entry (MyBEEntry->st_session_id) was never updated
    because pgstat_report_sessionid() was commented out and the function
    was removed during the PG16 merge.
    
    This caused pg_stat_activity.sess_id on QD to become stale, diverging
    from the actual gp_session_id. Any query that joins QD's
    pg_stat_activity with segment pg_stat_activity on sess_id would fail
    to match, making gp_sync_lc_gucs test flaky.
    
    Restore pgstat_report_sessionid() following the existing
    pgstat_report_resgroup() pattern, and uncomment the call in
    GpDropTempTables(). Update the gp_sync_lc_gucs expected output since
    the second pg_terminate_backend now correctly finds and terminates QEs.
---
 src/backend/cdb/dispatcher/cdbgang.c          |  2 +-
 src/backend/utils/activity/backend_status.c   | 22 ++++++++++++++++++++++
 src/include/utils/backend_status.h            |  1 +
 src/test/regress/expected/gp_sync_lc_gucs.out |  5 +----
 4 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/src/backend/cdb/dispatcher/cdbgang.c 
b/src/backend/cdb/dispatcher/cdbgang.c
index 229e52e43ae..85e69086974 100644
--- a/src/backend/cdb/dispatcher/cdbgang.c
+++ b/src/backend/cdb/dispatcher/cdbgang.c
@@ -801,7 +801,7 @@ GpDropTempTables(void)
 
                gp_session_id = newSessionId;
                gp_command_count = 0;
-               //pgstat_report_sessionid(newSessionId);
+               pgstat_report_sessionid(newSessionId);
 
                /* Update the slotid for our singleton reader. */
                if (SharedLocalSnapshotSlot != NULL)
diff --git a/src/backend/utils/activity/backend_status.c 
b/src/backend/utils/activity/backend_status.c
index 647c4482f09..b4ca82151c4 100644
--- a/src/backend/utils/activity/backend_status.c
+++ b/src/backend/utils/activity/backend_status.c
@@ -750,6 +750,28 @@ pgstat_report_resgroup(Oid groupId)
        PGSTAT_END_WRITE_ACTIVITY(beentry);
 }
 
+/* ----------
+ * pgstat_report_sessionid() -
+ *
+ *     Called to update the session id in MyBEEntry after a gang reset
+ *     assigns a new gp_session_id.
+ * ----------
+ */
+void
+pgstat_report_sessionid(int session_id)
+{
+       volatile PgBackendStatus *beentry = MyBEEntry;
+
+       if (!beentry)
+               return;
+
+       PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
+
+       beentry->st_session_id = session_id;
+
+       PGSTAT_END_WRITE_ACTIVITY(beentry);
+}
+
 /* ----------
  * pgstat_read_current_status() -
  *
diff --git a/src/include/utils/backend_status.h 
b/src/include/utils/backend_status.h
index d8fa3854ca1..58c20f1a5e8 100644
--- a/src/include/utils/backend_status.h
+++ b/src/include/utils/backend_status.h
@@ -326,6 +326,7 @@ extern void pgstat_report_tempfile(size_t filesize);
 extern void pgstat_report_appname(const char *appname);
 extern void pgstat_report_xact_timestamp(TimestampTz tstamp);
 extern void pgstat_report_resgroup(Oid groupId);
+extern void pgstat_report_sessionid(int session_id);
 extern const char *pgstat_get_backend_current_activity(int pid, bool 
checkUser);
 extern const char *pgstat_get_crashed_backend_activity(int pid, char *buffer,
                                                                                
                           int buflen);
diff --git a/src/test/regress/expected/gp_sync_lc_gucs.out 
b/src/test/regress/expected/gp_sync_lc_gucs.out
index e0f739c9e4e..f25b2d066dd 100644
--- a/src/test/regress/expected/gp_sync_lc_gucs.out
+++ b/src/test/regress/expected/gp_sync_lc_gucs.out
@@ -73,10 +73,7 @@ SELECT segment_setting('lc_time');
 -- QD should sync the lc_time to the newly created QEs.
 SELECT pg_terminate_backend(pid) FROM gp_dist_random('pg_stat_activity') WHERE 
sess_id
  in (SELECT sess_id from pg_stat_activity WHERE pid in (SELECT 
pg_backend_pid())) ; 
- pg_terminate_backend 
-----------------------
-(0 rows)
-
+ERROR:  terminating connection due to administrator command
 SELECT segment_setting('lc_time');
  segment_setting 
 -----------------


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to