Skip to content

Commit

Permalink
Merge branch 'master' into stable
Browse files Browse the repository at this point in the history
  • Loading branch information
ololobus committed Nov 28, 2019
2 parents 1cb4967 + 830cc91 commit c1068bd
Show file tree
Hide file tree
Showing 11 changed files with 579 additions and 62 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ notifications:
on_failure: always

env:
- PG_VERSION=12 LEVEL=hardcore
- PG_VERSION=12
- PG_VERSION=11 LEVEL=hardcore
- PG_VERSION=11
- PG_VERSION=10 LEVEL=hardcore
Expand All @@ -28,4 +30,4 @@ env:
matrix:
allow_failures:
- env: PG_VERSION=10 LEVEL=nightmare
- env: PG_VERSION=9.6 LEVEL=nightmare
- env: PG_VERSION=9.6 LEVEL=nightmare
2 changes: 1 addition & 1 deletion Dockerfile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ RUN apk add --no-cache \
perl perl-ipc-run \
make musl-dev gcc bison flex coreutils \
zlib-dev libedit-dev \
clang clang-analyzer \
clang clang-analyzer linux-headers \
python2 python2-dev py2-virtualenv;


Expand Down
11 changes: 11 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pg_query_state is released under the PostgreSQL License, a liberal Open Source license, similar to the BSD or MIT licenses.

Copyright (c) 2016-2019, Postgres Professional
Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
Portions Copyright (c) 1994, The Regents of the University of California

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.

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.

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.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ EXTRA_CLEAN = ./isolation_output $(EXTENSION)--$(EXTVERSION).sql \
Dockerfile ./tests/*.pyc

ifdef USE_PGXS
PG_CONFIG = pg_config
PG_CONFIG ?= pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
else
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[![Build Status](https://travis-ci.org/postgrespro/pg_query_state.svg?branch=master)](https://travis-ci.org/postgrespro/pg_query_state)
[![codecov](https://codecov.io/gh/postgrespro/pg_query_state/branch/master/graph/badge.svg)](https://codecov.io/gh/postgrespro/pg_query_state)

# pg\_query\_state
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.
Expand Down
64 changes: 64 additions & 0 deletions expected/corner_cases_2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
Parsed test spec with 2 sessions

starting permutation: s1_pg_qs_1
step s1_pg_qs_1: select pg_query_state(1);
ERROR: backend with pid=1 not found

starting permutation: s1_pg_qs_2
step s1_pg_qs_2: select pg_query_state(pg_backend_pid());
ERROR: attempt to extract state of current process

starting permutation: s1_save_pid s2_pg_qs_counterpart
step s1_save_pid: select save_own_pid(0);
save_own_pid


s2: INFO: state of backend is idle
step s2_pg_qs_counterpart: select pg_query_state(counterpart_pid(0));
pg_query_state


starting permutation: s1_save_pid s1_disable_pg_qs s2_pg_qs_counterpart
step s1_save_pid: select save_own_pid(0);
save_own_pid


step s1_disable_pg_qs: set pg_query_state.enable to off;
s2: INFO: query execution statistics disabled
step s2_pg_qs_counterpart: select pg_query_state(counterpart_pid(0));
pg_query_state


starting permutation: s1_set_bob s2_set_bob s1_save_pid s2_pg_qs_counterpart
step s1_set_bob: set role bob;
step s2_set_bob: set role bob;
step s1_save_pid: select save_own_pid(0);
save_own_pid


s2: INFO: state of backend is idle
step s2_pg_qs_counterpart: select pg_query_state(counterpart_pid(0));
pg_query_state


starting permutation: s1_set_bob s2_set_su s1_save_pid s2_pg_qs_counterpart
step s1_set_bob: set role bob;
step s2_set_su: set role super;
step s1_save_pid: select save_own_pid(0);
save_own_pid


s2: INFO: state of backend is idle
step s2_pg_qs_counterpart: select pg_query_state(counterpart_pid(0));
pg_query_state


starting permutation: s1_set_bob s2_set_alice s1_save_pid s2_pg_qs_counterpart
step s1_set_bob: set role bob;
step s2_set_alice: set role alice;
step s1_save_pid: select save_own_pid(0);
save_own_pid


step s2_pg_qs_counterpart: select pg_query_state(counterpart_pid(0));
ERROR: permission denied
196 changes: 196 additions & 0 deletions patches/custom_signals_12.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
diff --git a/src/backend/storage/ipc/procsignal.c b/src/backend/storage/ipc/procsignal.c
index 7605b2c367..6a4327fe76 100644
--- a/src/backend/storage/ipc/procsignal.c
+++ b/src/backend/storage/ipc/procsignal.c
@@ -60,12 +60,20 @@ typedef struct
*/
#define NumProcSignalSlots (MaxBackends + NUM_AUXPROCTYPES)

+#define IsCustomProcSignalReason(reason) \
+ ((reason) >= PROCSIG_CUSTOM_1 && (reason) <= PROCSIG_CUSTOM_N)
+
+static bool CustomSignalPendings[NUM_CUSTOM_PROCSIGNALS];
+static ProcSignalHandler_type CustomInterruptHandlers[NUM_CUSTOM_PROCSIGNALS];
+
static ProcSignalSlot *ProcSignalSlots = NULL;
static volatile ProcSignalSlot *MyProcSignalSlot = NULL;

static bool CheckProcSignal(ProcSignalReason reason);
static void CleanupProcSignalState(int status, Datum arg);

+static void CheckAndSetCustomSignalInterrupts(void);
+
/*
* ProcSignalShmemSize
* Compute space needed for procsignal's shared memory
@@ -165,6 +173,36 @@ CleanupProcSignalState(int status, Datum arg)
slot->pss_pid = 0;
}

+/*
+ * RegisterCustomProcSignalHandler
+ * Assign specific handler of custom process signal with new
+ * ProcSignalReason key.
+ *
+ * This function has to be called in _PG_init function of extensions at the
+ * stage of loading shared preloaded libraries. Otherwise it throws fatal error.
+ *
+ * Return INVALID_PROCSIGNAL if all slots for custom signals are occupied.
+ */
+ProcSignalReason
+RegisterCustomProcSignalHandler(ProcSignalHandler_type handler)
+{
+ ProcSignalReason reason;
+
+ if (!process_shared_preload_libraries_in_progress)
+ ereport(FATAL, (errcode(ERRCODE_INTERNAL_ERROR),
+ errmsg("cannot register custom signal after startup")));
+
+ /* Iterate through custom signal slots to find a free one */
+ for (reason = PROCSIG_CUSTOM_1; reason <= PROCSIG_CUSTOM_N; reason++)
+ if (!CustomInterruptHandlers[reason - PROCSIG_CUSTOM_1])
+ {
+ CustomInterruptHandlers[reason - PROCSIG_CUSTOM_1] = handler;
+ return reason;
+ }
+
+ return INVALID_PROCSIGNAL;
+}
+
/*
* SendProcSignal
* Send a signal to a Postgres process
@@ -292,9 +330,63 @@ procsignal_sigusr1_handler(SIGNAL_ARGS)
if (CheckProcSignal(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN))
RecoveryConflictInterrupt(PROCSIG_RECOVERY_CONFLICT_BUFFERPIN);

+ CheckAndSetCustomSignalInterrupts();
+
SetLatch(MyLatch);

latch_sigusr1_handler();

errno = save_errno;
}
+
+/*
+ * Handle receipt of an interrupt indicating any of custom process signals.
+ */
+static void
+CheckAndSetCustomSignalInterrupts()
+{
+ ProcSignalReason reason;
+
+ for (reason = PROCSIG_CUSTOM_1; reason <= PROCSIG_CUSTOM_N; reason++)
+ {
+ if (CheckProcSignal(reason))
+ {
+
+ /* set interrupt flags */
+ InterruptPending = true;
+ CustomSignalPendings[reason - PROCSIG_CUSTOM_1] = true;
+ }
+ }
+
+ SetLatch(MyLatch);
+}
+
+/*
+ * CheckAndHandleCustomSignals
+ * Check custom signal flags and call handler assigned to that signal
+ * if it is not NULL
+ *
+ * This function is called within CHECK_FOR_INTERRUPTS if interrupt occurred.
+ */
+void
+CheckAndHandleCustomSignals(void)
+{
+ int i;
+
+ /* Disable interrupts to avoid recursive calls */
+ HOLD_INTERRUPTS();
+
+ /* Check on expiring of custom signals and call its handlers if exist */
+ for (i = 0; i < NUM_CUSTOM_PROCSIGNALS; i++)
+ if (CustomSignalPendings[i])
+ {
+ ProcSignalHandler_type handler;
+
+ CustomSignalPendings[i] = false;
+ handler = CustomInterruptHandlers[i];
+ if (handler != NULL)
+ handler();
+ }
+
+ RESUME_INTERRUPTS();
+}
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index c28cc37012..f5a48b98e8 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -3139,6 +3139,8 @@ ProcessInterrupts(void)

if (ParallelMessagePending)
HandleParallelMessages();
+
+ CheckAndHandleCustomSignals();
}


diff --git a/src/include/storage/procsignal.h b/src/include/storage/procsignal.h
index 05b186a05c..d961790b7e 100644
--- a/src/include/storage/procsignal.h
+++ b/src/include/storage/procsignal.h
@@ -17,6 +17,8 @@
#include "storage/backendid.h"


+#define NUM_CUSTOM_PROCSIGNALS 64
+
/*
* Reasons for signalling a Postgres child process (a backend or an auxiliary
* process, like checkpointer). We can cope with concurrent signals for different
@@ -29,6 +31,8 @@
*/
typedef enum
{
+ INVALID_PROCSIGNAL = -1, /* Must be first */
+
PROCSIG_CATCHUP_INTERRUPT, /* sinval catchup interrupt */
PROCSIG_NOTIFY_INTERRUPT, /* listen/notify interrupt */
PROCSIG_PARALLEL_MESSAGE, /* message from cooperating parallel backend */
@@ -42,9 +46,20 @@ typedef enum
PROCSIG_RECOVERY_CONFLICT_BUFFERPIN,
PROCSIG_RECOVERY_CONFLICT_STARTUP_DEADLOCK,

+ PROCSIG_CUSTOM_1,
+ /*
+ * PROCSIG_CUSTOM_2,
+ * ...,
+ * PROCSIG_CUSTOM_N-1,
+ */
+ PROCSIG_CUSTOM_N = PROCSIG_CUSTOM_1 + NUM_CUSTOM_PROCSIGNALS - 1,
+
NUM_PROCSIGNALS /* Must be last! */
} ProcSignalReason;

+/* Handler of custom process signal */
+typedef void (*ProcSignalHandler_type) (void);
+
/*
* prototypes for functions in procsignal.c
*/
@@ -52,9 +67,13 @@ extern Size ProcSignalShmemSize(void);
extern void ProcSignalShmemInit(void);

extern void ProcSignalInit(int pss_idx);
+extern ProcSignalReason
+ RegisterCustomProcSignalHandler(ProcSignalHandler_type handler);
extern int SendProcSignal(pid_t pid, ProcSignalReason reason,
BackendId backendId);

+extern void CheckAndHandleCustomSignals(void);
+
extern void procsignal_sigusr1_handler(SIGNAL_ARGS);

#endif /* PROCSIGNAL_H */
Loading

0 comments on commit c1068bd

Please sign in to comment.