Skip to content

Commit 910ecbb

Browse files
author
Vladislav Shchetinin
committed
Feat: Moved gp_relsizes_stats extention from gp
1 parent 51889a4 commit 910ecbb

4 files changed

Lines changed: 19 additions & 25 deletions

File tree

gpcontrib/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ ifeq "$(enable_debug_extensions)" "yes"
2020
gp_inject_fault \
2121
gp_exttable_fdw \
2222
gp_legacy_string_agg \
23+
gp_relsizes_stats \
2324
gp_replica_check \
2425
gp_toolkit \
2526
pg_hint_plan
@@ -28,6 +29,7 @@ else
2829
gp_distribution_policy \
2930
gp_internal_tools \
3031
gp_legacy_string_agg \
32+
gp_relsizes_stats \
3133
gp_exttable_fdw \
3234
gp_toolkit \
3335
pg_hint_plan

gpcontrib/gp_relsizes_stats/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# gp_relsizes_stats: Table sizes monitoring tool for Greenplum
1+
# gp_relsizes_stats: Table sizes monitoring tool for Cloudberry
22

33
### Features
4-
gp_relsizes_stats is an extension for the Greenplum database that calculates and stores statistics on the size of files and tables, occupied space on the disks of the master and segment hosts.
4+
gp_relsizes_stats is an extension for the Cloudberry database that calculates and stores statistics on the size of files and tables, occupied space on the disks of the master and segment hosts.
55

66
#### Features include
77
- BackgroundWorker support for collecting statistics automatically
88
- the ability to fine-tune the timeout values between actions, for example, between launches for different databases, or during file processing to distribute the load over time
99

1010
### Supported versions and platforms
11-
At the moment, the program is being tested only for GP6 and Linux.
11+
At the moment, the program is being tested only for Cloudberry and Linux.
1212

1313
### Installation
1414
Install from source:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# gp_relsizes_stats extension
2-
comment = 'gp_relsizes_stats - an extension to track table on-disc sizes in greenplum'
2+
comment = 'gp_relsizes_stats - an extension to track table on-disc sizes in cloudberry'
33
default_version = '1.3'
44
module_pathname = '$libdir/gp_relsizes_stats'
55
trusted = true

gpcontrib/gp_relsizes_stats/src/gp_relsizes_stats.c

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ static int update_table_sizes_history(void);
5555
static void get_stats_for_databases(Oid *databases_oids, int databases_cnt, bool fast);
5656
static void run_database_stats_worker(bool fast, Oid db);
5757
static int plugin_created(void);
58-
static BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle);
58+
static BgwHandleStatus WaitForBackgroundWorkerShutdownSafely(BackgroundWorkerHandle *handle);
5959
static int delete_data_in_history(void);
6060
static int put_data_into_history(void);
6161
void _PG_init(void);
@@ -109,29 +109,24 @@ static void worker_sigterm(SIGNAL_ARGS) {
109109
* error handling to prevent infinite loops in case of hung workers.
110110
* Returns BGWH_STOPPED on success, BGWH_POSTMASTER_DIED on error/timeout.
111111
*/
112-
static BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *handle) {
112+
static BgwHandleStatus WaitForBackgroundWorkerShutdownSafely(BackgroundWorkerHandle *handle) {
113113
BgwHandleStatus status;
114114
int rc;
115-
bool save_set_latch_on_sigusr1;
116115
int attempts = 0;
117116
const int max_attempts = 5 * HOUR_TIME / 100; /* maximum 5 hours wait time */
118117

119-
save_set_latch_on_sigusr1 = set_latch_on_sigusr1;
120-
set_latch_on_sigusr1 = true;
121-
122118
PG_TRY();
123119
{
124120
while (attempts < max_attempts) {
125121
pid_t pid;
126122

127123
status = GetBackgroundWorkerPid(handle, &pid);
128124
if (status == BGWH_STOPPED) {
129-
set_latch_on_sigusr1 = save_set_latch_on_sigusr1;
130125
return status;
131126
}
132127

133128
/* Add 100ms timeout instead of infinite wait */
134-
rc = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, 100L);
129+
rc = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, 100L, WAIT_EVENT_BGWORKER_SHUTDOWN);
135130

136131
ResetLatch(&MyProc->procLatch);
137132

@@ -142,7 +137,7 @@ static BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *h
142137

143138
/* Check for interrupts but don't let them break the entire process */
144139
if (QueryCancelPending || ProcDiePending) {
145-
ereport(WARNING, (errmsg("WaitForBackgroundWorkerShutdown: received interrupt signal, stopping wait")));
140+
ereport(WARNING, (errmsg("WaitForBackgroundWorkerShutdownSafely: received interrupt signal, stopping wait")));
146141
status = BGWH_POSTMASTER_DIED; /* Return status as if postmaster died */
147142
break;
148143
}
@@ -152,21 +147,18 @@ static BgwHandleStatus WaitForBackgroundWorkerShutdown(BackgroundWorkerHandle *h
152147

153148
/* If maximum attempts reached */
154149
if (attempts >= max_attempts) {
155-
ereport(WARNING, (errmsg("WaitForBackgroundWorkerShutdown: timeout after %d attempts", max_attempts)));
150+
ereport(WARNING, (errmsg("WaitForBackgroundWorkerShutdownSafely: timeout after %d attempts", max_attempts)));
156151
status = BGWH_POSTMASTER_DIED; /* Return error status */
157152
}
158153
}
159154
PG_CATCH();
160155
{
161156
/* Log error but do NOT re-throw exception */
162-
ereport(WARNING, (errmsg("WaitForBackgroundWorkerShutdown: caught exception, returning error status")));
163-
set_latch_on_sigusr1 = save_set_latch_on_sigusr1;
157+
ereport(WARNING, (errmsg("WaitForBackgroundWorkerShutdownSafely: caught exception, returning error status")));
164158
/* Return error status instead of PG_RE_THROW() */
165159
return BGWH_POSTMASTER_DIED;
166160
}
167161
PG_END_TRY();
168-
169-
set_latch_on_sigusr1 = save_set_latch_on_sigusr1;
170162
return status;
171163
}
172164

@@ -356,7 +348,7 @@ void relsizes_database_stats_job(Datum args) {
356348
pqsignal(SIGTERM, worker_sigterm);
357349
BackgroundWorkerUnblockSignals();
358350

359-
BackgroundWorkerInitializeConnectionByOid(wa.s.db, InvalidOid);
351+
BackgroundWorkerInitializeConnectionByOid(wa.s.db, InvalidOid, 0);
360352

361353
SetCurrentStatementStartTimestamp();
362354
StartTransactionCommand();
@@ -394,7 +386,7 @@ void relsizes_database_stats_job(Datum args) {
394386
/* Remove this condition after decision how to upgrade extensions is made. */
395387
if (SearchSysCacheExists3(PROCNAMEARGSNSP,
396388
CStringGetDatum("get_stats_for_database"),
397-
PointerGetDatum((&(oidvector){ .dim1 = 1, .values = { INT4OID } })),
389+
PointerGetDatum(buildoidvector((Oid[]){INT4OID}, 1)),
398390
ObjectIdGetDatum(get_namespace_oid("relsizes_stats_schema", true))))
399391
{
400392
const char* sql_get_stats =
@@ -495,7 +487,7 @@ static void run_database_stats_worker(bool fast, Oid db) {
495487
ereport(WARNING, (errmsg("Failed to start background worker [%s], skipping", database_worker.bgw_name)));
496488
return;
497489
}
498-
status = WaitForBackgroundWorkerShutdown(handle);
490+
status = WaitForBackgroundWorkerShutdownSafely(handle);
499491
if (status != BGWH_STOPPED) {
500492
ereport(WARNING, (errmsg("Failure during background worker execution [%s], continuing", database_worker.bgw_name)));
501493
/* Don't abort execution, just log and continue */
@@ -618,7 +610,7 @@ Datum get_stats_for_database(PG_FUNCTION_ARGS) {
618610
/* Brief pause between file processing to reduce system load */
619611
int retcode = WaitLatch(&MyProc->procLatch,
620612
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
621-
worker_file_naptime);
613+
worker_file_naptime, WAIT_EVENT_BUFFER_IO);
622614
ResetLatch(&MyProc->procLatch);
623615

624616
CHECK_FOR_INTERRUPTS();
@@ -671,7 +663,7 @@ static void get_stats_for_databases(Oid *databases_oids, int databases_cnt, bool
671663
CHECK_FOR_INTERRUPTS();
672664
else {
673665
int naptime = (databases_cnt > 0) ? (worker_database_naptime / databases_cnt) : worker_database_naptime;
674-
int retcode = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, naptime);
666+
int retcode = WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, naptime, WAIT_EVENT_BGWORKER_STARTUP);
675667
ResetLatch(&MyProc->procLatch);
676668
CHECK_FOR_INTERRUPTS();
677669
/* emergency bailout if postmaster has died */
@@ -839,14 +831,14 @@ void relsizes_collect_stats(Datum main_arg) {
839831
optimizer = false;
840832
pqsignal(SIGTERM, worker_sigterm);
841833
BackgroundWorkerUnblockSignals();
842-
BackgroundWorkerInitializeConnection("postgres", NULL);
834+
BackgroundWorkerInitializeConnection("postgres", NULL, 0);
843835

844836
while (!got_sigterm) {
845837
if (enabled)
846838
relsizes_collect_stats_once_internal(true);
847839

848840
int retcode =
849-
WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, worker_restart_naptime);
841+
WaitLatch(&MyProc->procLatch, WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH, worker_restart_naptime, WAIT_EVENT_BGWORKER_STARTUP);
850842
ResetLatch(&MyProc->procLatch);
851843
CHECK_FOR_INTERRUPTS();
852844
if (retcode & WL_POSTMASTER_DEATH) {

0 commit comments

Comments
 (0)