v2-0003-connection_hooks.patch

text/plain

Filename: v2-0003-connection_hooks.patch
Type: text/plain
Part: 0
Message: Re: Patch proposal: New hooks in the connection path

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 v2-0003
File+
src/backend/postmaster/postmaster.c 6 0
src/backend/utils/init/postinit.c 12 0
src/include/libpq/auth.h 16 0
src/tools/pgindent/typedefs.list 1 0
diff --git a/src/backend/postmaster/postmaster.c b/src/backend/postmaster/postmaster.c
index d7257e4056..d9e1e3b4c1 100644
--- a/src/backend/postmaster/postmaster.c
+++ b/src/backend/postmaster/postmaster.c
@@ -567,6 +567,8 @@ int			postmaster_alive_fds[2] = {-1, -1};
 HANDLE		PostmasterHandle;
 #endif
 
+FailedConnection_hook_type FailedConnection_hook = NULL;
+
 /*
  * Postmaster main entry point
  */
@@ -4462,7 +4464,11 @@ BackendInitialize(Port *port)
 	 * already did any appropriate error reporting.
 	 */
 	if (status != STATUS_OK)
+	{
+		if (FailedConnection_hook)
+			(*FailedConnection_hook) (FCET_BAD_STARTUP_PACKET, port);
 		proc_exit(0);
+	}
 
 	/*
 	 * Now that we have the user and database name, we can set the process
diff --git a/src/backend/utils/init/postinit.c b/src/backend/utils/init/postinit.c
index 6b9082604f..e9bbd185f4 100644
--- a/src/backend/utils/init/postinit.c
+++ b/src/backend/utils/init/postinit.c
@@ -360,10 +360,14 @@ CheckMyDatabase(const char *name, bool am_superuser, bool override_allow_connect
 		if (!am_superuser &&
 			pg_database_aclcheck(MyDatabaseId, GetUserId(),
 								 ACL_CONNECT) != ACLCHECK_OK)
+		{
+			if (FailedConnection_hook)
+				(*FailedConnection_hook) (FCET_BAD_DATABASE_PERMISSION, MyProcPort);
 			ereport(FATAL,
 					(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
 					 errmsg("permission denied for database \"%s\"", name),
 					 errdetail("User does not have CONNECT privilege.")));
+		}
 
 		/*
 		 * Check connection limit for this database.
@@ -918,9 +922,13 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 
 		tuple = GetDatabaseTuple(in_dbname);
 		if (!HeapTupleIsValid(tuple))
+		{
+			if (FailedConnection_hook)
+				(*FailedConnection_hook) (FCET_BAD_DATABASE_NAME, MyProcPort);
 			ereport(FATAL,
 					(errcode(ERRCODE_UNDEFINED_DATABASE),
 					 errmsg("database \"%s\" does not exist", in_dbname)));
+		}
 		dbform = (Form_pg_database) GETSTRUCT(tuple);
 		MyDatabaseId = dbform->oid;
 		MyDatabaseTableSpace = dbform->dattablespace;
@@ -935,9 +943,13 @@ InitPostgres(const char *in_dbname, Oid dboid, const char *username,
 
 		tuple = GetDatabaseTupleByOid(dboid);
 		if (!HeapTupleIsValid(tuple))
+		{
+			if (FailedConnection_hook)
+				(*FailedConnection_hook) (FCET_BAD_DATABASE_OID, MyProcPort);
 			ereport(FATAL,
 					(errcode(ERRCODE_UNDEFINED_DATABASE),
 					 errmsg("database %u does not exist", dboid)));
+		}
 		dbform = (Form_pg_database) GETSTRUCT(tuple);
 		MyDatabaseId = dbform->oid;
 		MyDatabaseTableSpace = dbform->dattablespace;
diff --git a/src/include/libpq/auth.h b/src/include/libpq/auth.h
index d3c189efe3..a604b3f1e6 100644
--- a/src/include/libpq/auth.h
+++ b/src/include/libpq/auth.h
@@ -28,4 +28,20 @@ extern void sendAuthRequest(Port *port, AuthRequest areq, const char *extradata,
 typedef void (*ClientAuthentication_hook_type) (Port *, int);
 extern PGDLLIMPORT ClientAuthentication_hook_type ClientAuthentication_hook;
 
+/*
+ * The failed connection events to be used in the FailedConnection_hook.
+ */
+typedef enum FailedConnectionEventType
+{
+	FCET_BAD_STARTUP_PACKET,
+	FCET_BAD_DATABASE_NAME,
+	FCET_BAD_DATABASE_OID,
+	FCET_BAD_DATABASE_PERMISSION
+} FailedConnectionEventType;
+
+/* kluge to avoid including libpq/libpq-be.h here */
+struct Port;
+typedef void (*FailedConnection_hook_type) (FailedConnectionEventType event, const struct Port *port);
+extern PGDLLIMPORT FailedConnection_hook_type FailedConnection_hook;
+
 #endif							/* AUTH_H */
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index 4fb746930a..4fadafbddb 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -717,6 +717,7 @@ FPI
 FSMAddress
 FSMPage
 FSMPageData
+FailedConnection_hook_type
 FakeRelCacheEntry
 FakeRelCacheEntryData
 FastPathStrongRelationLockData