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 26 221324 : pgstat_progress_start_command(ProgressCommandType cmdtype, Oid relid) 27 : { 28 221324 : volatile PgBackendStatus *beentry = MyBEEntry; 29 : 30 221324 : if (!beentry || !pgstat_track_activities) 31 0 : return; 32 : 33 221324 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry); 34 221324 : beentry->st_progress_command = cmdtype; 35 221324 : beentry->st_progress_command_target = relid; 36 4647804 : MemSet(&beentry->st_progress_param, 0, sizeof(beentry->st_progress_param)); 37 221324 : 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 34389116 : pgstat_progress_update_param(int index, int64 val) 48 : { 49 34389116 : volatile PgBackendStatus *beentry = MyBEEntry; 50 : 51 : Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM); 52 : 53 34389116 : if (!beentry || !pgstat_track_activities) 54 0 : return; 55 : 56 34389116 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry); 57 34389116 : beentry->st_progress_param[index] = val; 58 34389116 : 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 68 2152 : pgstat_progress_incr_param(int index, int64 incr) 69 : { 70 2152 : volatile PgBackendStatus *beentry = MyBEEntry; 71 : 72 : Assert(index >= 0 && index < PGSTAT_NUM_PROGRESS_PARAM); 73 : 74 2152 : if (!beentry || !pgstat_track_activities) 75 0 : return; 76 : 77 2152 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry); 78 2152 : beentry->st_progress_param[index] += incr; 79 2152 : 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 : */ 89 : void 90 1084980 : pgstat_progress_update_multi_param(int nparam, const int *index, 91 : const int64 *val) 92 : { 93 1084980 : volatile PgBackendStatus *beentry = MyBEEntry; 94 : int i; 95 : 96 1084980 : if (!beentry || !pgstat_track_activities || nparam == 0) 97 0 : return; 98 : 99 1084980 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry); 100 : 101 3483770 : for (i = 0; i < nparam; ++i) 102 : { 103 : Assert(index[i] >= 0 && index[i] < PGSTAT_NUM_PROGRESS_PARAM); 104 : 105 2398790 : beentry->st_progress_param[index[i]] = val[i]; 106 : } 107 : 108 1084980 : PGSTAT_END_WRITE_ACTIVITY(beentry); 109 : } 110 : 111 : /*----------- 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 : */ 118 : void 119 268482 : pgstat_progress_end_command(void) 120 : { 121 268482 : volatile PgBackendStatus *beentry = MyBEEntry; 122 : 123 268482 : if (!beentry || !pgstat_track_activities) 124 0 : return; 125 : 126 268482 : if (beentry->st_progress_command == PROGRESS_COMMAND_INVALID) 127 48440 : return; 128 : 129 220042 : PGSTAT_BEGIN_WRITE_ACTIVITY(beentry); 130 220042 : beentry->st_progress_command = PROGRESS_COMMAND_INVALID; 131 220042 : beentry->st_progress_command_target = InvalidOid; 132 220042 : PGSTAT_END_WRITE_ACTIVITY(beentry); 133 : }