| 1 |
/* ---------- |
| 2 |
* backend_progress.c |
| 3 |
* |
| 4 |
* Command progress reporting infrastructure. |
| 5 |
* |
| 6 |
* Copyright (c) 2001-2025, PostgreSQL Global Development Group |
| 7 |
* |
| 8 |
* src/backend/utils/activity/backend_progress.c |
| 9 |
* ---------- |
| 10 |
*/ |
| 11 |
#include "postgres.h" |
| 12 |
|
| 13 |
#include "access/parallel.h" |
| 14 |
#include "libpq/pqformat.h" |
| 15 |
#include "utils/backend_progress.h" |
| 16 |
#include "utils/backend_status.h" |
| 17 |
|
| 18 |
|
| 19 |
/*----------- |
| 20 |
* pgstat_progress_start_command() - |
| 21 |
* |
| 22 |
* Set st_progress_command (and st_progress_command_target) in own backend |
| 23 |
* entry. Also, zero-initialize st_progress_param array. |
| 24 |
*----------- |
| 25 |
*/ |
| 26 |
void |
| 27 |
pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid) |
| 28 |
{ |
| 29 |
volatile PgBackendStatus *beentry = MyBEEntry; |
| 30 |
|
| 31 |
if (!beentry || !pgstat_track_activities) |
| 32 |
return; |
| 33 |
|
| 34 |
PGSTAT_BEGIN_WRITE_ACTIVITY(beentry); |
| 35 |
beentry->st_progress_command = cmdtype; |
| 36 |
beentry->st_progress_command_target = relid; |
| 37 |
MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param)); |
| 38 |
PGSTAT_END_WRITE_ACTIVITY(beentry); |
| 39 |
} |
| 40 |
|
| 41 |
/*----------- |
| 42 |
* pgstat_progress_update_param() - |
| 43 |
* |
| 44 |
* Update index'th member in st_progress_param[] of own backend entry. |
| 45 |
*----------- |
| 46 |
*/ |
| 47 |
void |
| 48 |
pgstat_progress_update_param(int index, int64 val) |
| 49 |
{ |
| 50 |
volatile PgBackendStatus *beentry = MyBEEntry; |
| 51 |
|
| 52 |
Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM); |
| 53 |
|
| 54 |
if (!beentry || !pgstat_track_activities) |
| 55 |
return; |
| 56 |
|
| 57 |
PGSTAT_BEGIN_WRITE_ACTIVITY(beentry); |
| 58 |
beentry->st_progress_param[index] = val; |
| 59 |
PGSTAT_END_WRITE_ACTIVITY(beentry); |
| 60 |
} |
| 61 |
|
| 62 |
/*----------- |
| 63 |
* pgstat_progress_incr_param() - |
| 64 |
* |
| 65 |
* Increment index'th member in st_progress_param[] of own backend entry. |
| 66 |
*----------- |
| 67 |
*/ |
| 68 |
void |
| 69 |
pgstat_progress_incr_param(int index, int64 incr) |
| 70 |
{ |
| 71 |
volatile PgBackendStatus *beentry = MyBEEntry; |
| 72 |
|
| 73 |
Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM); |
| 74 |
|
| 75 |
if (!beentry || !pgstat_track_activities) |
| 76 |
return; |
| 77 |
|
| 78 |
PGSTAT_BEGIN_WRITE_ACTIVITY(beentry); |
| 79 |
beentry->st_progress_param[index] += incr; |
| 80 |
PGSTAT_END_WRITE_ACTIVITY(beentry); |
| 81 |
} |
| 82 |
|
| 83 |
/*----------- |
| 84 |
* pgstat_progress_parallel_incr_param() - |
| 85 |
* |
| 86 |
* A variant of pgstat_progress_incr_param to allow a worker to poke at |
| 87 |
* a leader to do an incremental progress update. |
| 88 |
*----------- |
| 89 |
*/ |
| 90 |
void |
| 91 |
pgstat_progress_parallel_incr_param(int index, int64 incr) |
| 92 |
{ |
| 93 |
/* |
| 94 |
* Parallel workers notify a leader through a PqMsg_Progress message to |
| 95 |
* update progress, passing the progress index and incremented value. |
| 96 |
* Leaders can just call pgstat_progress_incr_param directly. |
| 97 |
*/ |
| 98 |
if (IsParallelWorker()) |
| 99 |
{ |
| 100 |
static StringInfoData progress_message; |
| 101 |
|
| 102 |
pq_beginmessage(&progress_message, PqMsg_Progress); |
| 103 |
pq_sendint32(&progress_message, index); |
| 104 |
pq_sendint64(&progress_message, incr); |
| 105 |
pq_endmessage(&progress_message); |
| 106 |
} |
| 107 |
else |
| 108 |
pgstat_progress_incr_param(index, incr); |
| 109 |
} |
| 110 |
|
| 111 |
/*----------- |
| 112 |
* pgstat_progress_update_multi_param() - |
| 113 |
* |
| 114 |
* Update multiple members in st_progress_param[] of own backend entry. |
| 115 |
* This is atomic; readers won't see intermediate states. |
| 116 |
*----------- |
| 117 |
*/ |
| 118 |
void |
| 119 |
pgstat_progress_update_multi_param(int nparam, const int *index, |
| 120 |
const int64 *val) |
| 121 |
{ |
| 122 |
volatile PgBackendStatus *beentry = MyBEEntry; |
| 123 |
int i; |
| 124 |
|
| 125 |
if (!beentry || !pgstat_track_activities || nparam == 0) |
| 126 |
return; |
| 127 |
|
| 128 |
PGSTAT_BEGIN_WRITE_ACTIVITY(beentry); |
| 129 |
|
| 130 |
for (i = 0; i < nparam; ++i) |
| 131 |
{ |
| 132 |
Assert(index[i] >= 0 && index[i] < PGSTAT_NUM_PROGRESS_PARAM); |
| 133 |
|
| 134 |
beentry->st_progress_param[index[i]] = val[i]; |
| 135 |
} |
| 136 |
|
| 137 |
PGSTAT_END_WRITE_ACTIVITY(beentry); |
| 138 |
} |
| 139 |
|
| 140 |
/*----------- |
| 141 |
* pgstat_progress_end_command() - |
| 142 |
* |
| 143 |
* Reset st_progress_command (and st_progress_command_target) in own backend |
| 144 |
* entry. This signals the end of the command. |
| 145 |
*----------- |
| 146 |
*/ |
| 147 |
void |
| 148 |
pgstat_progress_end_command(void) |
| 149 |
{ |
| 150 |
volatile PgBackendStatus *beentry = MyBEEntry; |
| 151 |
|
| 152 |
if (!beentry || !pgstat_track_activities) |
| 153 |
return; |
| 154 |
|
| 155 |
if (beentry->st_progress_command == PROGRESS_COMMAND_INVALID) |
| 156 |
return; |
| 157 |
|
| 158 |
PGSTAT_BEGIN_WRITE_ACTIVITY(beentry); |
| 159 |
beentry->st_progress_command = PROGRESS_COMMAND_INVALID; |
| 160 |
beentry->st_progress_command_target = InvalidOid; |
| 161 |
PGSTAT_END_WRITE_ACTIVITY(beentry); |
| 162 |
} |