002_session_end_hook.patch
text/x-patch
Filename: 002_session_end_hook.patch
Type: text/x-patch
Part: 1
Message:
Re: [PATCH] A hook for session start
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: unified
| File | + | − |
|---|---|---|
| src/backend/tcop/postgres.c | 22 | 0 |
| src/include/tcop/tcopprot.h | 5 | 0 |
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index b8d860e..8b5e408 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -160,6 +160,9 @@ static bool RecoveryConflictPending = false;
static bool RecoveryConflictRetryable = true;
static ProcSignalReason RecoveryConflictReason;
+/* Hook for plugins to get control at end of session */
+session_end_hook_type session_end_hook = NULL;
+
/* ----------------------------------------------------------------
* decls for routines only used in this file
* ----------------------------------------------------------------
@@ -182,6 +185,7 @@ static bool IsTransactionExitStmtList(List *pstmts);
static bool IsTransactionStmtList(List *pstmts);
static void drop_unnamed_stmt(void);
static void log_disconnections(int code, Datum arg);
+static void do_session_end_hook(int code, Datum arg);
/* ----------------------------------------------------------------
@@ -3809,6 +3813,12 @@ PostgresMain(int argc, char *argv[],
PgStartTime = GetCurrentTimestamp();
/*
+ * Setup handler to session end hook
+ */
+ if (IsUnderPostmaster)
+ on_proc_exit(do_session_end_hook, 0);
+
+ /*
* POSTGRES main processing loop begins here
*
* If an exception is encountered, processing resumes here so we abort the
@@ -4514,3 +4524,15 @@ log_disconnections(int code, Datum arg)
port->user_name, port->database_name, port->remote_host,
port->remote_port[0] ? " port=" : "", port->remote_port)));
}
+
+/*
+ * on_proc_exit handler to call session end hook
+ */
+static void
+do_session_end_hook(int code, Datum arg)
+{
+ Port *port = MyProcPort;
+
+ if (session_end_hook)
+ (*session_end_hook) (port->database_name, port->user_name);
+}
diff --git a/src/include/tcop/tcopprot.h b/src/include/tcop/tcopprot.h
index f8c535c..6d38d0b 100644
--- a/src/include/tcop/tcopprot.h
+++ b/src/include/tcop/tcopprot.h
@@ -35,6 +35,11 @@ extern PGDLLIMPORT const char *debug_query_string;
extern int max_stack_depth;
extern int PostAuthDelay;
+/* Hook for plugins to get control at end of session */
+typedef void (*session_end_hook_type) (const char *dbname,
+ const char *username);
+extern PGDLLIMPORT session_end_hook_type session_end_hook;
+
/* GUC-configurable parameters */
typedef enum