-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_pgext_template.c
201 lines (163 loc) · 5.72 KB
/
my_pgext_template.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
/*----------------------------------------------------------------------------------------------------------------------
*
* my_pgext_template.c
* My PostgreSQL Extension Template
*
* Author: SunBeau
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
*
* IDENTIFICATION
* contrib/my_pgext_template/my_pgext_template.c
*
*----------------------------------------------------------------------------------------------------------------------
*/
#include "postgres.h"
#include "fmgr.h"
#include "utils/builtins.h"
#include "tcop/utility.h"
/* These are always necessary for a bgworker */
#include "miscadmin.h"
#include "postmaster/bgworker.h"
#include "postmaster/interrupt.h"
#include "storage/ipc.h"
#include "storage/latch.h"
#include "storage/lwlock.h"
#include "storage/proc.h"
#include "storage/shmem.h"
#include "utils/wait_event.h"
#include "my_pgext_template.h"
#include <stdlib.h>
// ---------------------------------------------------------------------------------------------------------------------
// guc
static char *my_pgext_template_teststr = NULL;
/**
* @brief 初始化自定义配置参数
*/
void init_guc(void)
{
DefineCustomStringVariable("my_pgext_template.teststr",
"My PostgreSQL Extension Template Test String.",
NULL,
&my_pgext_template_teststr,
"Asd_12345_@#$",
PGC_POSTMASTER,
0,
NULL, NULL, NULL);
}
// ---------------------------------------------------------------------------------------------------------------------
// Hook
static ProcessUtility_hook_type prev_ProcessUtility = NULL;
static void MyProcessUtility(PlannedStmt *pstmt,
const char *queryString,
bool readOnlyTree,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
QueryCompletion *qc);
static void MyProcessUtility(PlannedStmt *pstmt,
const char *queryString,
bool readOnlyTree,
ProcessUtilityContext context,
ParamListInfo params,
QueryEnvironment *queryEnv,
DestReceiver *dest,
QueryCompletion *qc)
{
elog(NOTICE, "my_pgext_template -- MyProcessUtility");
if (prev_ProcessUtility)
(*prev_ProcessUtility)(pstmt, queryString, readOnlyTree, context, params, queryEnv, dest, qc);
else
standard_ProcessUtility(pstmt, queryString, readOnlyTree, context, params, queryEnv, dest, qc);
}
/**
* @brief 初始化钩子函数
*/
void init_hook(void)
{
prev_ProcessUtility = ProcessUtility_hook;
ProcessUtility_hook = MyProcessUtility;
}
// ---------------------------------------------------------------------------------------------------------------------
// bgworker
void simple_worker_main(Datum main_arg) pg_attribute_noreturn();
static void do_some_thing(int idx);
static void do_some_thing(int idx)
{
elog(LOG, "----- simple worker [%d] do_some_thing -----", idx);
}
/**
* @brief 简单后台进程入口
*
* @param main_arg
*/
void simple_worker_main(Datum main_arg)
{
int arg = DatumGetInt32(main_arg);
/* Establish signal handlers before unblocking signals. */
pqsignal(SIGHUP, SignalHandlerForConfigReload);
pqsignal(SIGTERM, die);
/* We're now ready to receive signals */
BackgroundWorkerUnblockSignals();
/* Connect to our database */
BackgroundWorkerInitializeConnection("postgres", NULL, 0);
elog(LOG, "%s initialized arg[%d]", MyBgworkerEntry->bgw_name, arg);
/* Main loop: do this until SIGTERM is received and processed by ProcessInterrupts. */
for (;;)
{
long timeout = 5000L; /* 超时(毫秒) */
/*
* 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_EXIT_ON_PM_DEATH,
timeout, PG_WAIT_EXTENSION);
ResetLatch(MyLatch);
CHECK_FOR_INTERRUPTS();
/*
* In case of a SIGHUP, just reload the configuration.
*/
if (ConfigReloadPending)
{
ConfigReloadPending = false;
ProcessConfigFile(PGC_SIGHUP);
}
do_some_thing(arg);
}
/* Not reachable */
}
/**
* @brief 初始化后台进程
*/
void init_bgworker(void)
{
BackgroundWorker worker;
memset(&worker, 0, sizeof(worker));
/* set up common data for all our workers */
worker.bgw_flags = BGWORKER_SHMEM_ACCESS | BGWORKER_BACKEND_DATABASE_CONNECTION;
worker.bgw_start_time = BgWorkerStart_RecoveryFinished;
worker.bgw_restart_time = BGW_NEVER_RESTART;
worker.bgw_notify_pid = 0;
sprintf(worker.bgw_library_name, "my_pgext_template"); /* 库文件名 */
sprintf(worker.bgw_function_name, "simple_worker_main"); /* 进程入口函数名 */
/*
* Now fill in worker-specific data, and do the actual registrations.
*/
for (int i = 1; i <= 3; i++)
{
worker.bgw_main_arg = Int32GetDatum(i);
snprintf(worker.bgw_name, BGW_MAXLEN, "simple worker [%d]", i);
snprintf(worker.bgw_type, BGW_MAXLEN, "my_pgext_template");
RegisterBackgroundWorker(&worker);
}
}
// ---------------------------------------------------------------------------------------------------------------------
// 可以通过 sql 调用的函数
PG_FUNCTION_INFO_V1(my_hello_world);
Datum my_hello_world(PG_FUNCTION_ARGS)
{
PG_RETURN_TEXT_P(cstring_to_text("Hello World !!!"));
}