Age Owner TLA Line data Source code
1 : /* ----------
2 : * backend_progress.c
3 : *
4 : * Command progress reporting infrastructure.
5 : *
6 : * Copyright (c) 2001-2023, PostgreSQL Global Development Group
7 : *
8 : * src/backend/utils/activity/backend_progress.c
9 : * ----------
10 : */
11 : #include "postgres.h"
12 :
13 : #include "port/atomics.h" /* for memory barriers */
14 : #include "utils/backend_progress.h"
15 : #include "utils/backend_status.h"
16 :
17 :
18 : /*-----------
19 : * pgstat_progress_start_command() -
20 : *
21 : * Set st_progress_command (and st_progress_command_target) in own backend
22 : * entry. Also, zero-initialize st_progress_param array.
23 : *-----------
24 : */
25 : void
736 andres 26 CBC 111253 : pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid)
27 : {
28 111253 : volatile PgBackendStatus *beentry = MyBEEntry;
29 :
30 111253 : if (!beentry || !pgstat_track_activities)
736 andres 31 UBC 0 : return;
32 :
736 andres 33 CBC 111253 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
34 111253 : beentry->st_progress_command = cmdtype;
35 111253 : beentry->st_progress_command_target = relid;
36 2336313 : MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param));
37 111253 : PGSTAT_END_WRITE_ACTIVITY(beentry);
38 : }
39 :
40 : /*-----------
41 : * pgstat_progress_update_param() -
42 : *
43 : * Update index'th member in st_progress_param[] of own backend entry.
44 : *-----------
45 : */
46 : void
47 17287934 : pgstat_progress_update_param(int index, int64 val)
48 : {
49 17287934 : volatile PgBackendStatus *beentry = MyBEEntry;
50 :
51 17287934 : Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
52 :
53 17287934 : if (!beentry || !pgstat_track_activities)
736 andres 54 UBC 0 : return;
55 :
736 andres 56 CBC 17287934 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
57 17287934 : beentry->st_progress_param[index] = val;
58 17287934 : PGSTAT_END_WRITE_ACTIVITY(beentry);
59 : }
60 :
61 : /*-----------
62 : * pgstat_progress_incr_param() -
63 : *
64 : * Increment index'th member in st_progress_param[] of own backend entry.
65 : *-----------
66 : */
67 : void
15 tgl 68 GNC 1076 : pgstat_progress_incr_param(int index, int64 incr)
69 : {
70 1076 : volatile PgBackendStatus *beentry = MyBEEntry;
71 :
72 1076 : Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM);
73 :
74 1076 : if (!beentry || !pgstat_track_activities)
15 tgl 75 UNC 0 : return;
76 :
15 tgl 77 GNC 1076 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
78 1076 : beentry->st_progress_param[index] += incr;
79 1076 : PGSTAT_END_WRITE_ACTIVITY(beentry);
80 : }
81 :
82 : /*-----------
83 : * pgstat_progress_update_multi_param() -
84 : *
85 : * Update multiple members in st_progress_param[] of own backend entry.
86 : * This is atomic; readers won't see intermediate states.
87 : *-----------
88 : */
736 andres 89 ECB : void
736 andres 90 GIC 565952 : pgstat_progress_update_multi_param(int nparam, const int *index,
736 andres 91 ECB : const int64 *val)
92 : {
736 andres 93 CBC 565952 : volatile PgBackendStatus *beentry = MyBEEntry;
94 : int i;
736 andres 95 ECB :
736 andres 96 GBC 565952 : if (!beentry || !pgstat_track_activities || nparam == 0)
736 andres 97 UIC 0 : return;
736 andres 98 ECB :
736 andres 99 CBC 565952 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
736 andres 100 ECB :
736 andres 101 GIC 1793227 : for (i = 0; i < nparam; ++i)
102 : {
103 1227275 : Assert(index[i] >= 0 && index[i] < PGSTAT_NUM_PROGRESS_PARAM);
104 :
105 1227275 : beentry->st_progress_param[index[i]] = val[i];
106 : }
107 :
108 565952 : PGSTAT_END_WRITE_ACTIVITY(beentry);
109 : }
110 :
736 andres 111 ECB : /*-----------
112 : * pgstat_progress_end_command() -
113 : *
114 : * Reset st_progress_command (and st_progress_command_target) in own backend
115 : * entry. This signals the end of the command.
116 : *-----------
117 : */
736 andres 118 EUB : void
736 andres 119 GIC 134832 : pgstat_progress_end_command(void)
736 andres 120 ECB : {
736 andres 121 GIC 134832 : volatile PgBackendStatus *beentry = MyBEEntry;
736 andres 122 ECB :
736 andres 123 GIC 134832 : if (!beentry || !pgstat_track_activities)
736 andres 124 LBC 0 : return;
125 :
736 andres 126 CBC 134832 : if (beentry->st_progress_command == PROGRESS_COMMAND_INVALID)
736 andres 127 GIC 24212 : return;
128 :
736 andres 129 CBC 110620 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry);
736 andres 130 GIC 110620 : beentry->st_progress_command = PROGRESS_COMMAND_INVALID;
131 110620 : beentry->st_progress_command_target = InvalidOid;
132 110620 : PGSTAT_END_WRITE_ACTIVITY(beentry);
133 : }
|