0002-session_end_hook-v4.patch
text/x-patch
Filename: 0002-session_end_hook-v4.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
Series: patch v4-0002
| File | + | − |
|---|---|---|
| src/backend/tcop/postgres.c | 21 | 2 |
| src/include/tcop/tcopprot.h | 5 | 1 |
diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c
index 24346fb..0dbbd7b 100644
--- a/src/backend/tcop/postgres.c
+++ b/src/backend/tcop/postgres.c
@@ -169,9 +169,9 @@ static ProcSignalReason RecoveryConflictReason;
static MemoryContext row_description_context = NULL;
static StringInfoData row_description_buf;
-/* Hook for plugins to get control at start of session */
+/* Hook for plugins to get control at start or end of session */
session_start_hook_type session_start_hook = NULL;
-
+session_end_hook_type session_end_hook = NULL;
/* ----------------------------------------------------------------
* decls for routines only used in this file
* ----------------------------------------------------------------
@@ -196,6 +196,7 @@ static void drop_unnamed_stmt(void);
static void log_disconnections(int code, Datum arg);
static void enable_statement_timeout(void);
static void disable_statement_timeout(void);
+static void do_session_end_hook(int code, Datum arg);
/* ----------------------------------------------------------------
@@ -3864,6 +3865,12 @@ PostgresMain(int argc, char *argv[],
(*session_start_hook) (dbname, username);
/*
+ * 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
@@ -4615,3 +4622,15 @@ disable_statement_timeout(void)
stmt_timeout_active = false;
}
}
+
+/*
+ * 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);
+}
\ No newline at end of file
diff --git a/src/include/tcop/tcopprot.h b/src/include/tcop/tcopprot.h
index d349592..b7fb8c3 100644
--- a/src/include/tcop/tcopprot.h
+++ b/src/include/tcop/tcopprot.h
@@ -35,10 +35,14 @@ extern PGDLLIMPORT const char *debug_query_string;
extern int max_stack_depth;
extern int PostAuthDelay;
-/* Hook for plugins to get control at start of session */
+/* Hook for plugins to get control at start and end of session */
typedef void (*session_start_hook_type) (const char *dbname,
const char *username);
+typedef void (*session_end_hook_type) (const char *dbname,
+ const char *username);
+
extern PGDLLIMPORT session_start_hook_type session_start_hook;
+extern PGDLLIMPORT session_end_hook_type session_end_hook;
/* GUC-configurable parameters */