@@ -321,6 +321,7 @@ pgstat_bestart_initial(void)
321
321
lbeentry .st_progress_command = PROGRESS_COMMAND_INVALID ;
322
322
lbeentry .st_progress_command_target = InvalidOid ;
323
323
lbeentry .st_query_id = UINT64CONST (0 );
324
+ lbeentry .st_plan_id = UINT64CONST (0 );
324
325
325
326
/*
326
327
* we don't zero st_progress_param here to save cycles; nobody should
@@ -599,6 +600,7 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
599
600
/* st_xact_start_timestamp and wait_event_info are also disabled */
600
601
beentry -> st_xact_start_timestamp = 0 ;
601
602
beentry -> st_query_id = UINT64CONST (0 );
603
+ beentry -> st_plan_id = UINT64CONST (0 );
602
604
proc -> wait_event_info = 0 ;
603
605
PGSTAT_END_WRITE_ACTIVITY (beentry );
604
606
}
@@ -659,7 +661,10 @@ pgstat_report_activity(BackendState state, const char *cmd_str)
659
661
* identifier.
660
662
*/
661
663
if (state == STATE_RUNNING )
664
+ {
662
665
beentry -> st_query_id = UINT64CONST (0 );
666
+ beentry -> st_plan_id = UINT64CONST (0 );
667
+ }
663
668
664
669
if (cmd_str != NULL )
665
670
{
@@ -710,6 +715,44 @@ pgstat_report_query_id(uint64 query_id, bool force)
710
715
PGSTAT_END_WRITE_ACTIVITY (beentry );
711
716
}
712
717
718
+ /* --------
719
+ * pgstat_report_plan_id() -
720
+ *
721
+ * Called to update top-level plan identifier.
722
+ * --------
723
+ */
724
+ void
725
+ pgstat_report_plan_id (uint64 plan_id , bool force )
726
+ {
727
+ volatile PgBackendStatus * beentry = MyBEEntry ;
728
+
729
+ /*
730
+ * if track_activities is disabled, st_plan_id should already have been
731
+ * reset
732
+ */
733
+ if (!beentry || !pgstat_track_activities )
734
+ return ;
735
+
736
+ /*
737
+ * We only report the top-level plan identifiers. The stored plan_id is
738
+ * reset when a backend calls pgstat_report_activity(STATE_RUNNING), or
739
+ * with an explicit call to this function using the force flag. If the
740
+ * saved plan identifier is not zero it means that it's not a top-level
741
+ * command, so ignore the one provided unless it's an explicit call to
742
+ * reset the identifier.
743
+ */
744
+ if (beentry -> st_plan_id != 0 && !force )
745
+ return ;
746
+
747
+ /*
748
+ * Update my status entry, following the protocol of bumping
749
+ * st_changecount before and after. We use a volatile pointer here to
750
+ * ensure the compiler doesn't try to get cute.
751
+ */
752
+ PGSTAT_BEGIN_WRITE_ACTIVITY (beentry );
753
+ beentry -> st_plan_id = plan_id ;
754
+ PGSTAT_END_WRITE_ACTIVITY (beentry );
755
+ }
713
756
714
757
/* ----------
715
758
* pgstat_report_appname() -
@@ -1106,6 +1149,21 @@ pgstat_get_my_query_id(void)
1106
1149
return MyBEEntry -> st_query_id ;
1107
1150
}
1108
1151
1152
+ /* ----------
1153
+ * pgstat_get_my_plan_id() -
1154
+ *
1155
+ * Return current backend's plan identifier.
1156
+ */
1157
+ uint64
1158
+ pgstat_get_my_plan_id (void )
1159
+ {
1160
+ if (!MyBEEntry )
1161
+ return 0 ;
1162
+
1163
+ /* No need for a lock, for roughly the same reasons as above. */
1164
+ return MyBEEntry -> st_plan_id ;
1165
+ }
1166
+
1109
1167
/* ----------
1110
1168
* pgstat_get_backend_type_by_proc_number() -
1111
1169
*
0 commit comments