-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg_dbsm.c
253 lines (210 loc) · 7.58 KB
/
pg_dbsm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*----------------------------------------------------------------------------
*
* pg_dbsm.c
* Monitor the databse size.
*
* Copyright (c) 2019, Japin Li <japinli@hotmail.com>.
*
*----------------------------------------------------------------------------
*/
#include "postgres.h"
/* These are always necessary for a bgworker */
#include "miscadmin.h"
#include "postmaster/bgworker.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/lwlock.h"
#include "storage/proc.h"
#include "storage/shmem.h"
#include "access/xact.h"
#include "executor/spi.h"
#include "pgstat.h"
#include "utils/guc.h"
#include "utils/snapmgr.h"
#include "utils/timestamp.h"
PG_MODULE_MAGIC;
void _PG_init(void);
/* GUC variables */
static char *dbsm_database;
static int dbsm_naptime;
/* flags set by signal handlers */
static volatile sig_atomic_t got_sighup = false;
static volatile sig_atomic_t got_sigterm = false;
void dbsm_main(Datum) pg_attribute_noreturn();
static void initialize_dbsm_object(void);
void
_PG_init(void)
{
BackgroundWorker worker;
if (!process_shared_preload_libraries_in_progress)
return;
DefineCustomStringVariable("dbsm.database",
"Database used for monitor database size",
"Database used to check the database size (default: postgres).",
&dbsm_database,
"postgres",
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
DefineCustomIntVariable("dbsm.naptime",
"Duration between each check (in seconds).",
NULL,
&dbsm_naptime,
60 * 60 * 24,
1,
INT_MAX,
PGC_SIGHUP,
0,
NULL,
NULL,
NULL);
/* See: see: https://www.postgresql.org/docs/current/bgworker.html */
memset(&worker, 0, sizeof(worker));
worker.bgw_flags = BGWORKER_SHMEM_ACCESS |
BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
worker.bgw_restart_time = BGW_NEVER_RESTART;
sprintf(worker.bgw_library_name, "pg_dbsm");
sprintf(worker.bgw_function_name, "dbsm_main");
worker.bgw_notify_pid = 0;
snprintf(worker.bgw_name, BGW_MAXLEN, "database size monitor");
snprintf(worker.bgw_type, BGW_MAXLEN, "pg_dbsm");
worker.bgw_main_arg = Int32GetDatum(0);
RegisterBackgroundWorker(&worker);
}
/*
* Signal handler for SIGTERM
* Set a flag to let the main loop to terminate, and set our latch to wake
* it up.
*/
static void
dbsm_sigterm(SIGNAL_ARGS)
{
int save_errno = errno;
got_sigterm = true;
SetLatch(MyLatch);
errno = save_errno;
}
/*
* Signal handler for SIGHUP
* Set a flag to tell the main loop to reread the config file, and set
* our latch to wake it up.
*/
static void
dbsm_sighup(SIGNAL_ARGS)
{
int save_errno = errno;
got_sighup = true;
SetLatch(MyLatch);
errno = save_errno;
}
void
dbsm_main()
{
StringInfoData buf;
/* Establish signal handlers before unblocking signals. */
pqsignal(SIGHUP, dbsm_sighup);
pqsignal(SIGTERM, dbsm_sigterm);
/* We're now ready to receive signals */
BackgroundWorkerUnblockSignals();
BackgroundWorkerInitializeConnection(dbsm_database, NULL, 0);
initialize_dbsm_object();
initStringInfo(&buf);
appendStringInfo(&buf,
"WITH m AS (SELECT DISTINCT ON (datname) datname, "
"datsize FROM dbsm ORDER BY datname, created DESC) "
"INSERT INTO dbsm SELECT d.datname, now(), "
"pg_database_size(d.datname) AS datsize, "
"CASE WHEN m.datsize = NULL THEN 0 "
"ELSE pg_database_size(d.datname) - m.datsize END AS incsize "
"FROM pg_database d LEFT JOIN m ON m.datname = d.datname;");
/*
* Main loop: do this until the SIGTERM handler tells us to terminate
*/
while (!got_sigterm)
{
int ret;
/*
* Background workers mustn't call usleep() or any direct equivalent:
* instead, they may wait on their process latch, which sleeps as
* necessary, but is awakened if postmaster dies. That way the
* background process goes away immediately in an emergency.
*/
(void) WaitLatch(MyLatch,
WL_LATCH_SET | WL_TIMEOUT | WL_POSTMASTER_DEATH,
dbsm_naptime * 1000L,
PG_WAIT_EXTENSION);
ResetLatch(MyLatch);
CHECK_FOR_INTERRUPTS();
/*
* In case of a SIGHUP, just reload the configuration.
*/
if (got_sighup)
{
got_sighup = false;
ProcessConfigFile(PGC_SIGHUP);
}
/*
* Start a transaction on which we can run queries. Note that each
* StartTransactionCommand() call should be preceded by a
* SetCurrentStatementStartTimestamp() call, which sets both the time
* for the statement we're about the run, and also the transaction
* start time. Also, each other query sent to SPI should probably be
* preceded by SetCurrentStatementStartTimestamp(), so that statement
* start time is always up to date.
*
* The SPI_connect() call lets us run queries through the SPI manager,
* and the PushActiveSnapshot() call creates an "active" snapshot
* which is necessary for queries to have MVCC data to work on.
*
* The pgstat_report_activity() call makes our activity visible
* through the pgstat views.
*/
SetCurrentStatementStartTimestamp();
StartTransactionCommand();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
pgstat_report_activity(STATE_RUNNING, buf.data);
ret = SPI_execute(buf.data, false, 0);
if (ret != SPI_OK_INSERT)
elog(FATAL, "execute: \"%s\" failed", buf.data);
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
pgstat_report_stat(false);
pgstat_report_activity(STATE_IDLE, NULL);
}
pfree(buf.data);
proc_exit(0);
}
static void
initialize_dbsm_object(void)
{
int ret;
StringInfoData buf;
initStringInfo(&buf);
appendStringInfo(&buf,
"CREATE TABLE IF NOT EXISTS dbsm ( "
"datname NAME NOT NULL, created TimestampTz DEFAULT now(), "
"datsize int8 NOT NULL, "
"incsize int8);"
"CREATE INDEX IF NOT EXISTS idx_datname_created ON "
"dbsm USING BTREE(datname, created);");
SetCurrentStatementStartTimestamp();
StartTransactionCommand();
SPI_connect();
PushActiveSnapshot(GetTransactionSnapshot());
pgstat_report_activity(STATE_RUNNING, buf.data);
ret = SPI_execute(buf.data, false, 0);
if (ret != SPI_OK_UTILITY)
elog(FATAL, "execute: \"%s\" failed", buf.data);
resetStringInfo(&buf);
SPI_finish();
PopActiveSnapshot();
CommitTransactionCommand();
pgstat_report_stat(false);
pgstat_report_activity(STATE_IDLE, NULL);
pfree(buf.data);
}