Skip to content

Commit c1068bd

Browse files
committed
Merge branch 'master' into stable
2 parents 1cb4967 + 830cc91 commit c1068bd

11 files changed

+579
-62
lines changed

Diff for: .travis.yml

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ notifications:
1818
on_failure: always
1919

2020
env:
21+
- PG_VERSION=12 LEVEL=hardcore
22+
- PG_VERSION=12
2123
- PG_VERSION=11 LEVEL=hardcore
2224
- PG_VERSION=11
2325
- PG_VERSION=10 LEVEL=hardcore
@@ -28,4 +30,4 @@ env:
2830
matrix:
2931
allow_failures:
3032
- env: PG_VERSION=10 LEVEL=nightmare
31-
- env: PG_VERSION=9.6 LEVEL=nightmare
33+
- env: PG_VERSION=9.6 LEVEL=nightmare

Diff for: Dockerfile.tmpl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ RUN apk add --no-cache \
66
perl perl-ipc-run \
77
make musl-dev gcc bison flex coreutils \
88
zlib-dev libedit-dev \
9-
clang clang-analyzer \
9+
clang clang-analyzer linux-headers \
1010
python2 python2-dev py2-virtualenv;
1111

1212

Diff for: LICENSE

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pg_query_state is released under the PostgreSQL License, a liberal Open Source license, similar to the BSD or MIT licenses.
2+
3+
Copyright (c) 2016-2019, Postgres Professional
4+
Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
5+
Portions Copyright (c) 1994, The Regents of the University of California
6+
7+
Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies.
8+
9+
IN NO EVENT SHALL POSTGRES PROFESSIONAL BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF POSTGRES PROFESSIONAL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10+
11+
POSTGRES PROFESSIONAL SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND POSTGRES PROFESSIONAL HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ EXTRA_CLEAN = ./isolation_output $(EXTENSION)--$(EXTVERSION).sql \
1212
Dockerfile ./tests/*.pyc
1313

1414
ifdef USE_PGXS
15-
PG_CONFIG = pg_config
15+
PG_CONFIG ?= pg_config
1616
PGXS := $(shell $(PG_CONFIG) --pgxs)
1717
include $(PGXS)
1818
else

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[![Build Status](https://travis-ci.org/postgrespro/pg_query_state.svg?branch=master)](https://travis-ci.org/postgrespro/pg_query_state)
2+
[![codecov](https://codecov.io/gh/postgrespro/pg_query_state/branch/master/graph/badge.svg)](https://codecov.io/gh/postgrespro/pg_query_state)
23

34
# pg\_query\_state
45
The `pg_query_state` module provides facility to know the current state of query execution on working backend. To enable this extension you have to patch the latest stable version of PostgreSQL. Different branches are intended for different version numbers of PostgreSQL, e.g., branch _PG9_5_ corresponds to PostgreSQL 9.5.

Diff for: expected/corner_cases_2.out

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Parsed test spec with 2 sessions
2+
3+
starting permutation: s1_pg_qs_1
4+
step s1_pg_qs_1: select pg_query_state(1);
5+
ERROR: backend with pid=1 not found
6+
7+
starting permutation: s1_pg_qs_2
8+
step s1_pg_qs_2: select pg_query_state(pg_backend_pid());
9+
ERROR: attempt to extract state of current process
10+
11+
starting permutation: s1_save_pid s2_pg_qs_counterpart
12+
step s1_save_pid: select save_own_pid(0);
13+
save_own_pid
14+
15+
16+
s2: INFO: state of backend is idle
17+
step s2_pg_qs_counterpart: select pg_query_state(counterpart_pid(0));
18+
pg_query_state
19+
20+
21+
starting permutation: s1_save_pid s1_disable_pg_qs s2_pg_qs_counterpart
22+
step s1_save_pid: select save_own_pid(0);
23+
save_own_pid
24+
25+
26+
step s1_disable_pg_qs: set pg_query_state.enable to off;
27+
s2: INFO: query execution statistics disabled
28+
step s2_pg_qs_counterpart: select pg_query_state(counterpart_pid(0));
29+
pg_query_state
30+
31+
32+
starting permutation: s1_set_bob s2_set_bob s1_save_pid s2_pg_qs_counterpart
33+
step s1_set_bob: set role bob;
34+
step s2_set_bob: set role bob;
35+
step s1_save_pid: select save_own_pid(0);
36+
save_own_pid
37+
38+
39+
s2: INFO: state of backend is idle
40+
step s2_pg_qs_counterpart: select pg_query_state(counterpart_pid(0));
41+
pg_query_state
42+
43+
44+
starting permutation: s1_set_bob s2_set_su s1_save_pid s2_pg_qs_counterpart
45+
step s1_set_bob: set role bob;
46+
step s2_set_su: set role super;
47+
step s1_save_pid: select save_own_pid(0);
48+
save_own_pid
49+
50+
51+
s2: INFO: state of backend is idle
52+
step s2_pg_qs_counterpart: select pg_query_state(counterpart_pid(0));
53+
pg_query_state
54+
55+
56+
starting permutation: s1_set_bob s2_set_alice s1_save_pid s2_pg_qs_counterpart
57+
step s1_set_bob: set role bob;
58+
step s2_set_alice: set role alice;
59+
step s1_save_pid: select save_own_pid(0);
60+
save_own_pid
61+
62+
63+
step s2_pg_qs_counterpart: select pg_query_state(counterpart_pid(0));
64+
ERROR: permission denied

Diff for: patches/custom_signals_12.0.patch

+196
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
diff --git a/src/backend/storage/ipc/procsignal.c b/src/backend/storage/ipc/procsignal.c
2+
index 7605b2c367..6a4327fe76 100644
3+
--- a/src/backend/storage/ipc/procsignal.c
4+
+++ b/src/backend/storage/ipc/procsignal.c
5+
@@ -60,12 +60,20 @@ typedef struct
6+
*/
7+
#define NumProcSignalSlots (MaxBackends + NUM_AUXPROCTYPES)
8+
9+
+#define IsCustomProcSignalReason(reason) \
10+
+ ((reason) >= PROCSIG_CUSTOM_1 && (reason) <= PROCSIG_CUSTOM_N)
11+
+
12+
+static bool CustomSignalPendings[NUM_CUSTOM_PROCSIGNALS];
13+
+static ProcSignalHandler_type CustomInterruptHandlers[NUM_CUSTOM_PROCSIGNALS];
14+
+
15+
static ProcSignalSlot *ProcSignalSlots = NULL;
16+
static volatile ProcSignalSlot *MyProcSignalSlot = NULL;
17+
18+
static bool CheckProcSignal(ProcSignalReason reason);
19+
static void CleanupProcSignalState(int status, Datum arg);
20+
21+
+static void CheckAndSetCustomSignalInterrupts(void);
22+
+
23+
/*
24+
* ProcSignalShmemSize
25+
* Compute space needed for procsignal's shared memory
26+
@@ -165,6 +173,36 @@ CleanupProcSignalState(int status, Datum arg)
27+
slot->pss_pid = 0;
28+
}
29+
30+
+/*
31+
+ * RegisterCustomProcSignalHandler
32+
+ * Assign specific handler of custom process signal with new
33+
+ * ProcSignalReason key.
34+
+ *
35+
+ * This function has to be called in _PG_init function of extensions at the
36+
+ * stage of loading shared preloaded libraries. Otherwise it throws fatal error.
37+
+ *
38+
+ * Return INVALID_PROCSIGNAL if all slots for custom signals are occupied.
39+
+ */
40+
+ProcSignalReason
41+
+RegisterCustomProcSignalHandler(ProcSignalHandler_type handler)
42+
+{
43+
+ ProcSignalReason reason;
44+
+
45+
+ if (!process_shared_preload_libraries_in_progress)
46+
+ ereport(FATAL, (errcode(ERRCODE_INTERNAL_ERROR),
47+
+ errmsg("cannot register custom signal after startup")));
48+
+
49+
+ /* Iterate through custom signal slots to find a free one */
50+
+ for (reason = PROCSIG_CUSTOM_1; reason <= PROCSIG_CUSTOM_N; reason++)
51+
+ if (!CustomInterruptHandlers[reason - PROCSIG_CUSTOM_1])
52+
+ {
53+
+ CustomInterruptHandlers[reason - PROCSIG_CUSTOM_1] = handler;
54+
+ return reason;
55+
+ }
56+
+
57+
+ return INVALID_PROCSIGNAL;
58+
+}
59+
+
60+
/*
61+
* SendProcSignal
62+
* Send a signal to a Postgres process
63+
@@ -292,9 +330,63 @@ procsignal_sigusr1_handler(SIGNAL_ARGS)
64+
if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
65+
RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);
66+
67+
+ CheckAndSetCustomSignalInterrupts();
68+
+
69+
SetLatch(MyLatch);
70+
71+
latch_sigusr1_handler();
72+
73+
errno = save_errno;
74+
}
75+
+
76+
+/*
77+
+ * Handle receipt of an interrupt indicating any of custom process signals.
78+
+ */
79+
+static void
80+
+CheckAndSetCustomSignalInterrupts()
81+
+{
82+
+ ProcSignalReason reason;
83+
+
84+
+ for (reason = PROCSIG_CUSTOM_1; reason <= PROCSIG_CUSTOM_N; reason++)
85+
+ {
86+
+ if (CheckProcSignal(reason))
87+
+ {
88+
+
89+
+ /* set interrupt flags */
90+
+ InterruptPending = true;
91+
+ CustomSignalPendings[reason - PROCSIG_CUSTOM_1] = true;
92+
+ }
93+
+ }
94+
+
95+
+ SetLatch(MyLatch);
96+
+}
97+
+
98+
+/*
99+
+ * CheckAndHandleCustomSignals
100+
+ * Check custom signal flags and call handler assigned to that signal
101+
+ * if it is not NULL
102+
+ *
103+
+ * This function is called within CHECK_FOR_INTERRUPTS if interrupt occurred.
104+
+ */
105+
+void
106+
+CheckAndHandleCustomSignals(void)
107+
+{
108+
+ int i;
109+
+
110+
+ /* Disable interrupts to avoid recursive calls */
111+
+ HOLD_INTERRUPTS();
112+
+
113+
+ /* Check on expiring of custom signals and call its handlers if exist */
114+
+ for (i = 0; i < NUM_CUSTOM_PROCSIGNALS; i++)
115+
+ if (CustomSignalPendings[i])
116+
+ {
117+
+ ProcSignalHandler_type handler;
118+
+
119+
+ CustomSignalPendings[i] = false;
120+
+ handler = CustomInterruptHandlers[i];
121+
+ if (handler != NULL)
122+
+ handler();
123+
+ }
124+
+
125+
+ RESUME_INTERRUPTS();
126+
+}
127+
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
128+
index c28cc37012..f5a48b98e8 100644
129+
--- a/src/backend/tcop/postgres.c
130+
+++ b/src/backend/tcop/postgres.c
131+
@@ -3139,6 +3139,8 @@ ProcessInterrupts(void)
132+
133+
if (ParallelMessagePending)
134+
HandleParallelMessages();
135+
+
136+
+ CheckAndHandleCustomSignals();
137+
}
138+
139+
140+
diff --git a/src/include/storage/procsignal.h b/src/include/storage/procsignal.h
141+
index 05b186a05c..d961790b7e 100644
142+
--- a/src/include/storage/procsignal.h
143+
+++ b/src/include/storage/procsignal.h
144+
@@ -17,6 +17,8 @@
145+
#include "storage/backendid.h"
146+
147+
148+
+#define NUM_CUSTOM_PROCSIGNALS 64
149+
+
150+
/*
151+
* Reasons for signalling a Postgres child process (a backend or an auxiliary
152+
* process, like checkpointer). We can cope with concurrent signals for different
153+
@@ -29,6 +31,8 @@
154+
*/
155+
typedef enum
156+
{
157+
+ INVALID_PROCSIGNAL = -1, /* Must be first */
158+
+
159+
PROCSIG_CATCHUP_INTERRUPT, /* sinval catchup interrupt */
160+
PROCSIG_NOTIFY_INTERRUPT, /* listen/notify interrupt */
161+
PROCSIG_PARALLEL_MESSAGE, /* message from cooperating parallel backend */
162+
@@ -42,9 +46,20 @@ typedef enum
163+
PROCSIG_RECOVERY_CONFLICT_BUFFERPIN,
164+
PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK,
165+
166+
+ PROCSIG_CUSTOM_1,
167+
+ /*
168+
+ * PROCSIG_CUSTOM_2,
169+
+ * ...,
170+
+ * PROCSIG_CUSTOM_N-1,
171+
+ */
172+
+ PROCSIG_CUSTOM_N = PROCSIG_CUSTOM_1 + NUM_CUSTOM_PROCSIGNALS - 1,
173+
+
174+
NUM_PROCSIGNALS /* Must be last! */
175+
} ProcSignalReason;
176+
177+
+/* Handler of custom process signal */
178+
+typedef void (*ProcSignalHandler_type) (void);
179+
+
180+
/*
181+
* prototypes for functions in procsignal.c
182+
*/
183+
@@ -52,9 +67,13 @@ extern Size ProcSignalShmemSize(void);
184+
extern void ProcSignalShmemInit(void);
185+
186+
extern void ProcSignalInit(int pss_idx);
187+
+extern ProcSignalReason
188+
+ RegisterCustomProcSignalHandler(ProcSignalHandler_type handler);
189+
extern int SendProcSignal(pid_t pid, ProcSignalReason reason,
190+
BackendId backendId);
191+
192+
+extern void CheckAndHandleCustomSignals(void);
193+
+
194+
extern void procsignal_sigusr1_handler(SIGNAL_ARGS);
195+
196+
#endif /* PROCSIGNAL_H */

0 commit comments

Comments
 (0)