-
Notifications
You must be signed in to change notification settings - Fork 9
/
conf.c
221 lines (212 loc) · 12.8 KB
/
conf.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
#include "include.h"
#include <commands/dbcommands.h>
#include <commands/user.h>
#if PG_VERSION_NUM < 130000
#include <catalog/pg_type.h>
#include <miscadmin.h>
#endif
#include <nodes/makefuncs.h>
#if PG_VERSION_NUM < 100000
#include <parser/parse_node.h>
#endif
#include <pgstat.h>
#include <postmaster/bgworker.h>
#if PG_VERSION_NUM < 90500
#include <storage/barrier.h>
#endif
#include <storage/ipc.h>
#include <storage/proc.h>
#include <utils/acl.h>
#if PG_VERSION_NUM < 150000
#include <utils/guc.h>
#endif
#include <utils/builtins.h>
#include <utils/memutils.h>
#include <utils/ps_status.h>
#if PG_VERSION_NUM >= 100000
#include <utils/regproc.h>
#endif
extern char *task_null;
extern int conf_close;
extern int conf_fetch;
extern int work_restart;
extern WorkShared *workshared;
static volatile dlist_head head;
static void conf_data(const Work *w) {
List *names = stringToQualifiedNameListMy(w->data);
StringInfoData src;
elog(DEBUG1, "user = %s, data = %s", w->shared->user, w->shared->data);
set_ps_display_my("data");
initStringInfoMy(&src);
appendStringInfo(&src, SQL(CREATE DATABASE %s WITH OWNER = %s), w->data, w->user);
SPI_connect_my(src.data);
if (!OidIsValid(get_database_oid(strVal(linitial(names)), true))) {
CreatedbStmt *stmt = makeNode(CreatedbStmt);
ParseState *pstate = make_parsestate(NULL);
stmt->dbname = w->shared->data;
stmt->options = list_make1(makeDefElemMy("owner", (Node *)makeString(w->shared->user)));
pstate->p_sourcetext = src.data;
createdb_my(pstate, stmt);
list_free_deep(stmt->options);
free_parsestate(pstate);
pfree(stmt);
}
SPI_finish_my();
list_free_deep(names);
pfree(src.data);
set_ps_display_my("idle");
}
static void conf_free(Work *w) {
dlist_delete(&w->node);
pfree(w->shared);
pfree(w);
}
static void conf_shmem_exit(int code, Datum arg) {
elog(DEBUG1, "code = %i", code);
}
static void conf_user(const Work *w) {
List *names = stringToQualifiedNameListMy(w->user);
StringInfoData src;
elog(DEBUG1, "user = %s", w->shared->user);
set_ps_display_my("user");
initStringInfoMy(&src);
appendStringInfo(&src, SQL(CREATE ROLE %s WITH LOGIN), w->user);
SPI_connect_my(src.data);
if (!OidIsValid(get_role_oid(strVal(linitial(names)), true))) {
CreateRoleStmt *stmt = makeNode(CreateRoleStmt);
ParseState *pstate = make_parsestate(NULL);
stmt->role = w->shared->user;
stmt->options = list_make1(makeDefElemMy("canlogin", (Node *)makeInteger(1)));
pstate->p_sourcetext = src.data;
CreateRoleMy(pstate, stmt);
list_free_deep(stmt->options);
free_parsestate(pstate);
pfree(stmt);
}
SPI_finish_my();
list_free_deep(names);
pfree(src.data);
set_ps_display_my("idle");
}
static int conf_bgw_main_arg(WorkShared *ws) {
LWLockAcquire(BackgroundWorkerLock, LW_EXCLUSIVE);
for (int slot = 0; slot < max_worker_processes; slot++) if (!workshared[slot].in_use) {
pg_write_barrier();
workshared[slot] = *ws;
workshared[slot].in_use = true;
LWLockRelease(BackgroundWorkerLock);
elog(DEBUG1, "slot = %i", slot);
return slot;
}
LWLockRelease(BackgroundWorkerLock);
return -1;
}
static void conf_work(Work *w) {
BackgroundWorkerHandle *handle;
BackgroundWorker worker = {0};
size_t len;
set_ps_display_my("work");
w->data = quote_identifier(w->shared->data);
w->user = quote_identifier(w->shared->user);
conf_user(w);
conf_data(w);
if (w->data != w->shared->data) pfree((void *)w->data);
if (w->user != w->shared->user) pfree((void *)w->user);
if ((len = strlcpy(worker.bgw_function_name, "work_main", sizeof(worker.bgw_function_name))) >= sizeof(worker.bgw_function_name)) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("strlcpy %li >= %li", len, sizeof(worker.bgw_function_name))));
if ((len = strlcpy(worker.bgw_library_name, "pg_task", sizeof(worker.bgw_library_name))) >= sizeof(worker.bgw_library_name)) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("strlcpy %li >= %li", len, sizeof(worker.bgw_library_name))));
if ((len = snprintf(worker.bgw_name, sizeof(worker.bgw_name) - 1, "%s %s pg_work %s %s %li", w->shared->user, w->shared->data, w->shared->schema, w->shared->table, w->shared->sleep)) >= sizeof(worker.bgw_name) - 1) ereport(WARNING, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("snprintf %li >= %li", len, sizeof(worker.bgw_name) - 1)));
#if PG_VERSION_NUM >= 110000
if ((len = strlcpy(worker.bgw_type, worker.bgw_name, sizeof(worker.bgw_type))) >= sizeof(worker.bgw_type)) ereport(ERROR, (errcode(ERRCODE_OUT_OF_MEMORY), errmsg("strlcpy %li >= %li", len, sizeof(worker.bgw_type))));
#endif
worker.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
if ((worker.bgw_main_arg = Int32GetDatum(conf_bgw_main_arg(w->shared))) == Int32GetDatum(-1)) ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("could not find empty slot")));
worker.bgw_notify_pid = MyProcPid;
worker.bgw_restart_time = work_restart;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
if (!RegisterDynamicBackgroundWorker(&worker, &handle)) {
workshared_free(worker.bgw_main_arg);
ereport(ERROR, (errcode(ERRCODE_CONFIGURATION_LIMIT_EXCEEDED), errmsg("could not register background worker"), errhint("Consider increasing configuration parameter \"max_worker_processes\".")));
}
switch (WaitForBackgroundWorkerStartup(handle, &w->pid)) {
case BGWH_NOT_YET_STARTED: workshared_free(worker.bgw_main_arg); ereport(ERROR, (errcode(ERRCODE_INTERNAL_ERROR), errmsg("BGWH_NOT_YET_STARTED is never returned!"))); break;
case BGWH_POSTMASTER_DIED: workshared_free(worker.bgw_main_arg); ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("cannot start background worker without postmaster"), errhint("Kill all remaining database processes and restart the database."))); break;
case BGWH_STARTED: elog(DEBUG1, "started"); conf_free(w); break;
case BGWH_STOPPED: workshared_free(worker.bgw_main_arg); ereport(ERROR, (errcode(ERRCODE_INSUFFICIENT_RESOURCES), errmsg("could not start background worker"), errhint("More details may be available in the server log."))); break;
}
if (handle) pfree(handle);
}
void conf_main(Datum main_arg) {
dlist_mutable_iter iter;
Portal portal;
StringInfoData src;
before_shmem_exit(conf_shmem_exit, main_arg);
BackgroundWorkerUnblockSignals();
BackgroundWorkerInitializeConnectionMy("postgres", NULL);
set_config_option_my("application_name", "pg_conf", PGC_USERSET, PGC_S_SESSION, GUC_ACTION_SET, true, ERROR);
pgstat_report_appname("pg_conf");
set_ps_display_my("main");
process_session_preload_libraries();
if (!lock_data_user(MyDatabaseId, GetUserId())) { elog(WARNING, "!lock_data_user(%i, %i)", MyDatabaseId, GetUserId()); return; }
dlist_init((dlist_head *)&head);
initStringInfoMy(&src);
appendStringInfo(&src, SQL(
WITH j AS (
WITH s AS (
WITH s AS (
SELECT "setdatabase", "setrole", pg_catalog.regexp_split_to_array(pg_catalog.unnest("setconfig"), '=') AS "setconfig" FROM "pg_catalog"."pg_db_role_setting"
) SELECT "setdatabase", "setrole", pg_catalog.%1$s(pg_catalog.array_agg("setconfig"[1]), pg_catalog.array_agg("setconfig"[2])) AS "setconfig" FROM s GROUP BY 1, 2
) SELECT COALESCE("data", "user", pg_catalog.current_setting('pg_task.data'))::pg_catalog.text AS "data",
(EXTRACT(epoch FROM COALESCE("reset", (u."setconfig" OPERATOR(pg_catalog.->>) 'pg_task.reset')::pg_catalog.interval, (d."setconfig" OPERATOR(pg_catalog.->>) 'pg_task.reset')::pg_catalog.interval, pg_catalog.current_setting('pg_task.reset')::pg_catalog.interval))::pg_catalog.int8 OPERATOR(pg_catalog.*) 1000)::pg_catalog.int8 AS "reset",
COALESCE("run", (u."setconfig" OPERATOR(pg_catalog.->>) 'pg_task.run')::pg_catalog.int4, (d."setconfig" OPERATOR(pg_catalog.->>) 'pg_task.run')::pg_catalog.int4, pg_catalog.current_setting('pg_task.run')::pg_catalog.int4)::pg_catalog.int4 AS "run",
COALESCE("schema", u."setconfig" OPERATOR(pg_catalog.->>) 'pg_task.schema', d."setconfig" OPERATOR(pg_catalog.->>) 'pg_task.schema', pg_catalog.current_setting('pg_task.schema'))::pg_catalog.text AS "schema",
COALESCE("table", u."setconfig" OPERATOR(pg_catalog.->>) 'pg_task.table', d."setconfig" OPERATOR(pg_catalog.->>) 'pg_task.table', pg_catalog.current_setting('pg_task.table'))::pg_catalog.text AS "table",
COALESCE("sleep", (u."setconfig" OPERATOR(pg_catalog.->>) 'pg_task.sleep')::pg_catalog.int8, (d."setconfig" OPERATOR(pg_catalog.->>) 'pg_task.sleep')::pg_catalog.int8, pg_catalog.current_setting('pg_task.sleep')::pg_catalog.int8)::pg_catalog.int8 AS "sleep",
COALESCE("user", "data", pg_catalog.current_setting('pg_task.user'))::pg_catalog.text AS "user"
FROM pg_catalog.jsonb_to_recordset(pg_catalog.current_setting('pg_task.json')::pg_catalog.jsonb) AS j ("data" text, "reset" interval, "run" int4, "schema" text, "table" text, "sleep" int8, "user" text)
LEFT JOIN s AS d on d."setdatabase" OPERATOR(pg_catalog.=) (SELECT "oid" FROM "pg_catalog"."pg_database" WHERE "datname" OPERATOR(pg_catalog.=) COALESCE("data", "user", pg_catalog.current_setting('pg_task.data')))
LEFT JOIN s AS u on u."setrole" OPERATOR(pg_catalog.=) (SELECT "oid" FROM "pg_catalog"."pg_roles" WHERE "rolname" OPERATOR(pg_catalog.=) COALESCE("user", "data", pg_catalog.current_setting('pg_task.user')))
) SELECT DISTINCT j.*, pg_catalog.hashtext(pg_catalog.concat_ws('.', "schema", "table"))::pg_catalog.int4 AS "hash" FROM j
LEFT JOIN "pg_catalog"."pg_locks" AS l ON "locktype" OPERATOR(pg_catalog.=) 'userlock'
AND "mode" OPERATOR(pg_catalog.=) 'AccessExclusiveLock'
AND "granted" AND "objsubid" OPERATOR(pg_catalog.=) 3
AND "database" OPERATOR(pg_catalog.=) (SELECT "oid" FROM "pg_catalog"."pg_database" WHERE "datname" OPERATOR(pg_catalog.=) "data")
AND "classid" OPERATOR(pg_catalog.=) (SELECT "oid" FROM "pg_catalog"."pg_roles" WHERE "rolname" OPERATOR(pg_catalog.=) "user")
AND "objid" OPERATOR(pg_catalog.=) pg_catalog.hashtext(pg_catalog.concat_ws('.', "schema", "table"))::pg_catalog.oid
WHERE "pid" IS NULL
),
#if PG_VERSION_NUM >= 90500
"jsonb_object"
#else
"json_object"
#endif
);
SPI_connect_my(src.data);
portal = SPI_cursor_open_with_args_my(src.data, 0, NULL, NULL, NULL, true);
do {
SPI_cursor_fetch_my(src.data, portal, true, conf_fetch);
for (uint64 row = 0; row < SPI_processed; row++) {
HeapTuple val = SPI_tuptable->vals[row];
TupleDesc tupdesc = SPI_tuptable->tupdesc;
Work *w = MemoryContextAllocZero(TopMemoryContext, sizeof(*w));
set_ps_display_my("row");
w->shared = MemoryContextAllocZero(TopMemoryContext, sizeof(*w->shared));
w->shared->hash = DatumGetInt32(SPI_getbinval_my(val, tupdesc, "hash", false, INT4OID));
w->shared->reset = DatumGetInt64(SPI_getbinval_my(val, tupdesc, "reset", false, INT8OID));
w->shared->run = DatumGetInt32(SPI_getbinval_my(val, tupdesc, "run", false, INT4OID));
w->shared->sleep = DatumGetInt64(SPI_getbinval_my(val, tupdesc, "sleep", false, INT8OID));
text_to_cstring_buffer((text *)DatumGetPointer(SPI_getbinval_my(val, tupdesc, "data", false, TEXTOID)), w->shared->data, sizeof(w->shared->data));
text_to_cstring_buffer((text *)DatumGetPointer(SPI_getbinval_my(val, tupdesc, "schema", false, TEXTOID)), w->shared->schema, sizeof(w->shared->schema));
text_to_cstring_buffer((text *)DatumGetPointer(SPI_getbinval_my(val, tupdesc, "table", false, TEXTOID)), w->shared->table, sizeof(w->shared->table));
text_to_cstring_buffer((text *)DatumGetPointer(SPI_getbinval_my(val, tupdesc, "user", false, TEXTOID)), w->shared->user, sizeof(w->shared->user));
elog(DEBUG1, "row = %lu, user = %s, data = %s, schema = %s, table = %s, sleep = %li, reset = %li, run = %i, hash = %i", row, w->shared->user, w->shared->data, w->shared->schema, w->shared->table, w->shared->sleep, w->shared->reset, w->shared->run, w->shared->hash);
dlist_push_tail((dlist_head *)&head, &w->node);
SPI_freetuple(val);
}
} while (SPI_processed);
SPI_cursor_close_my(portal);
SPI_finish_my();
pfree(src.data);
set_ps_display_my("idle");
dlist_foreach_modify(iter, (dlist_head *)&head) conf_work(dlist_container(Work, node, iter.cur));
if (!unlock_data_user(MyDatabaseId, GetUserId())) elog(WARNING, "!unlock_data_user(%i, %i)", MyDatabaseId, GetUserId());
}