003_session_hooks_samples_v1.patch
text/x-patch
Filename: 003_session_hooks_samples_v1.patch
Type: text/x-patch
Part: 2
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 v1
| File | + | − |
|---|---|---|
| contrib/session_end/Makefile | 15 | 0 |
| contrib/session_end/session_end.c | 45 | 0 |
| contrib/session_start/Makefile | 15 | 0 |
| contrib/session_start/session_start.c | 52 | 0 |
diff --git a/contrib/session_end/Makefile b/contrib/session_end/Makefile
new file mode 100644
index 0000000..a2c0a72
--- /dev/null
+++ b/contrib/session_end/Makefile
@@ -0,0 +1,15 @@
+# contrib/session_start/Makefile
+
+MODULES = session_end
+PGFILEDESC = "session_end - sample for session end hook"
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = contrib/session_end
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/contrib/session_end/session_end.c b/contrib/session_end/session_end.c
new file mode 100644
index 0000000..c979b40
--- /dev/null
+++ b/contrib/session_end/session_end.c
@@ -0,0 +1,45 @@
+/* -------------------------------------------------------------------------
+ *
+ * session_end.c
+ *
+ * Copyright (c) 2010-2017, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * contrib/session_end/session_end.c
+ *
+ * -------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "tcop/tcopprot.h"
+
+PG_MODULE_MAGIC;
+
+void _PG_init(void);
+
+/* Original Hook */
+static session_end_hook_type original_session_end_hook = NULL;
+
+/* sample hook function */
+static void
+sample_session_end_hook(const char *dbname, const char *username)
+{
+ if (original_session_end_hook)
+ original_session_end_hook(dbname, username);
+
+ if (!strcmp(dbname, "test"))
+ elog(LOG, "end session hooked at '%s' database for user '%s'",
+ dbname, username);
+}
+
+/*
+ * Module Load Callback
+ */
+void
+_PG_init(void)
+{
+ /* Install Hooks */
+
+ original_session_end_hook = session_end_hook;
+ session_end_hook = sample_session_end_hook;
+}
diff --git a/contrib/session_start/Makefile b/contrib/session_start/Makefile
new file mode 100644
index 0000000..f94355b
--- /dev/null
+++ b/contrib/session_start/Makefile
@@ -0,0 +1,15 @@
+# contrib/session_start/Makefile
+
+MODULES = session_start
+PGFILEDESC = "session_start - sample for session start hook"
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+subdir = contrib/session_start
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/contrib/session_start/session_start.c b/contrib/session_start/session_start.c
new file mode 100644
index 0000000..7070242
--- /dev/null
+++ b/contrib/session_start/session_start.c
@@ -0,0 +1,52 @@
+/* -------------------------------------------------------------------------
+ *
+ * session_start.c
+ *
+ * Copyright (c) 2010-2017, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ * contrib/session_start/session_start.c
+ *
+ * -------------------------------------------------------------------------
+ */
+#include "postgres.h"
+
+#include "access/xact.h"
+#include "executor/spi.h"
+#include "tcop/tcopprot.h"
+
+PG_MODULE_MAGIC;
+
+void _PG_init(void);
+
+/* Original Hook */
+static session_start_hook_type original_session_start_hook = NULL;
+
+/* sample hook function */
+static void
+sample_session_start_hook(const char *dbname, const char *username)
+{
+ if (original_session_start_hook)
+ original_session_start_hook(dbname, username);
+
+ if (!strcmp(dbname, "test"))
+ {
+ StartTransactionCommand();
+ SPI_connect();
+ SPI_exec("set work_mem to 10240", 1);
+ SPI_finish();
+ CommitTransactionCommand();
+ }
+}
+
+/*
+ * Module Load Callback
+ */
+void
+_PG_init(void)
+{
+ /* Install Hooks */
+
+ original_session_start_hook = session_start_hook;
+ session_start_hook = sample_session_start_hook;
+}