Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dY9vg-0000Mo-GH for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Jul 2017 11:47:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1dY9vg-0003xB-36 for pgsql-hackers@arkaria.postgresql.org; Thu, 20 Jul 2017 11:47:40 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1dY9ve-0003r5-BD for pgsql-hackers@postgresql.org; Thu, 20 Jul 2017 11:47:38 +0000 Received: from sraihb2.sra.co.jp ([202.32.10.6]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dY9vb-0001m8-4B for pgsql-hackers@postgresql.org; Thu, 20 Jul 2017 11:47:37 +0000 Received: from srascf.sra.co.jp (srascf [133.137.8.80]) by sraihb2.sra.co.jp (Postfix) with ESMTP id 83AAA2A1659 for ; Thu, 20 Jul 2017 20:47:33 +0900 (JST) Received: from srascb.sra.co.jp (unknown [133.137.8.65]) by srascf.sra.co.jp with smtp id 27a3_01db_79dc00b5_45d7_4f24_910f_2ee75a1b79f5; Thu, 20 Jul 2017 20:47:33 +0900 Received: from sranhm.sra.co.jp (osspc25 [133.137.174.97]) by srascb.sra.co.jp (Postfix) with ESMTP id 6AE38110C70 for ; Thu, 20 Jul 2017 20:47:33 +0900 (JST) Received: from yugo-n-ubuntu (dhcp-175-143.sra.co.jp [133.137.175.143]) by sranhm.sra.co.jp (Postfix) with SMTP id 5A8B7A1078 for ; Thu, 20 Jul 2017 20:47:33 +0900 (JST) Date: Thu, 20 Jul 2017 20:47:33 +0900 From: Yugo Nagata To: pgsql-hackers@postgresql.org Subject: [PATCH] A hook for session start Message-Id: <20170720204733.40f2b7eb.nagata@sraoss.co.jp> X-Mailer: Sylpheed Pro 2.1.4 (GTK+ 2.24.23; x86_64-unknown-linux-gnu; Pro 2.1.4) Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="Multipart=_Thu__20_Jul_2017_20_47_33_+0900_2uCzk5dDwBCyRc2H" List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org This is a multi-part message in MIME format. --Multipart=_Thu__20_Jul_2017_20_47_33_+0900_2uCzk5dDwBCyRc2H Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Hi, Currently, PostgreSQL doen't have a hook triggered at session start. Although we already have ClientAuthentication_hook, this is triggered during authentication, so we can not access the database. If we have a hook triggerd only once at session start, we may do something useful on the session for certain database or user. For example, one of our clients wanted such feature. He wanted to handle encription for specific users, though I don't know the detail. The attached patch (session_start_hook.patch) implements such hook. Another patch, session_start_sample.patch, is a very simple example of this hook that changes work_mem values for sessions of a specific database. I would appreciate hearing your opinion on this hook. Regards, -- Yugo Nagata --Multipart=_Thu__20_Jul_2017_20_47_33_+0900_2uCzk5dDwBCyRc2H Content-Type: text/x-diff; name="session_start_hook.patch" Content-Disposition: attachment; filename="session_start_hook.patch" Content-Transfer-Encoding: 7bit diff --git a/src/backend/tcop/postgres.c b/src/backend/tcop/postgres.c index b8d860e..7a1fa3b 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 start of session */ +session_start_hook_type session_start_hook = NULL; + /* ---------------------------------------------------------------- * decls for routines only used in this file * ---------------------------------------------------------------- @@ -3808,6 +3811,9 @@ PostgresMain(int argc, char *argv[], if (!IsUnderPostmaster) PgStartTime = GetCurrentTimestamp(); + if (session_start_hook) + (*session_start_hook) (dbname, username); + /* * POSTGRES main processing loop begins here * diff --git a/src/include/tcop/tcopprot.h b/src/include/tcop/tcopprot.h index f8c535c..d349592 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 start of session */ +typedef void (*session_start_hook_type) (const char *dbname, + const char *username); +extern PGDLLIMPORT session_start_hook_type session_start_hook; + /* GUC-configurable parameters */ typedef enum --Multipart=_Thu__20_Jul_2017_20_47_33_+0900_2uCzk5dDwBCyRc2H Content-Type: text/x-diff; name="session_start_sample.patch" Content-Disposition: attachment; filename="session_start_sample.patch" Content-Transfer-Encoding: 7bit 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..1792879 --- /dev/null +++ b/contrib/session_start/session_start.c @@ -0,0 +1,53 @@ +/* ------------------------------------------------------------------------- + * + * session_start.c + * + * Copyright (c) 2010-2017, PostgreSQL Global Development Group + * + * IDENTIFICATION + * contrib/session_start/session_start.c + * + * ------------------------------------------------------------------------- + */ +#include "postgres.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) +{ + CommandDest back; + + 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; +} --Multipart=_Thu__20_Jul_2017_20_47_33_+0900_2uCzk5dDwBCyRc2H Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --Multipart=_Thu__20_Jul_2017_20_47_33_+0900_2uCzk5dDwBCyRc2H--