From ca3957264a68dc7871b98ada4cdde1ee26aefade Mon Sep 17 00:00:00 2001
From: "Andrei V. Lepikhov" <lepihov@gmail.com>
Date: Tue, 19 Nov 2024 18:45:36 +0700
Subject: [PATCH v2] Introduce PG_MODULE_MAGIC_EXT macro.

This macro provides dynamically loaded shared libraries (modules) with standard
way to incorporate version (supposedly, defined according to semantic versioning
specification) and name data. The introduced catalogue routine module_info can
be used to find this module by name and check the version. It makes users
independent from file naming conventions.

With a growing number of Postgres core hooks and the introduction of named DSM
segments, the number of modules that don't need to be loaded on startup may
grow fast. Moreover, in many cases related to query tree transformation or
extra path recommendation, such modules might not need database objects except
GUCs - see auto_explain as an example. That means they don't need to execute
the 'CREATE EXTENSION' statement at all and don't have a record in
the pg_extension table. Such a trick provides much flexibility, including
an online upgrade and may become widespread.

In addition, it is also convenient in support to be sure that the installation
(or at least the backend) includes a specific version of the module. Even if
a module has an installation script, it is not rare that it provides
an implementation for a range of UI routine versions. It makes sense to ensure
which specific version of the code is used.

Discussions [1,2] already mentioned module-info stuff, but at that time,
extensibility techniques and extension popularity were low, and it wasn't
necessary to provide that data.

[1] https://www.postgresql.org/message-id/flat/20060507211705.GB3808%40svana.org
[2] https://www.postgresql.org/message-id/flat/20051106162658.34c31d57%40thunder.logicalchaos.org
---
 contrib/auto_explain/auto_explain.c        |  5 +-
 contrib/auto_explain/t/001_auto_explain.pl |  8 +++
 contrib/pg_prewarm/t/001_basic.pl          |  8 ++-
 src/backend/utils/fmgr/dfmgr.c             | 62 +++++++++++++++++++++-
 src/include/catalog/pg_proc.dat            |  6 +++
 src/include/fmgr.h                         | 28 ++++++++--
 6 files changed, 110 insertions(+), 7 deletions(-)

diff --git a/contrib/auto_explain/auto_explain.c b/contrib/auto_explain/auto_explain.c
index f2eaa8e494..ab997830f3 100644
--- a/contrib/auto_explain/auto_explain.c
+++ b/contrib/auto_explain/auto_explain.c
@@ -20,7 +20,10 @@
 #include "executor/instrument.h"
 #include "utils/guc.h"
 
-PG_MODULE_MAGIC;
+PG_MODULE_MAGIC_EXT(
+	.name = "auto_explain",
+	.version = "1.0.0"
+);
 
 /* GUC variables */
 static int	auto_explain_log_min_duration = -1; /* msec or -1 */
diff --git a/contrib/auto_explain/t/001_auto_explain.pl b/contrib/auto_explain/t/001_auto_explain.pl
index 0e5b34afa9..81481606da 100644
--- a/contrib/auto_explain/t/001_auto_explain.pl
+++ b/contrib/auto_explain/t/001_auto_explain.pl
@@ -53,6 +53,14 @@ like(
 	qr/Seq Scan on pg_class/,
 	"sequential scan logged, text mode");
 
+$log_contents = $node->safe_psql(
+	"postgres", q{SELECT substring(libname, 'auto_explain'),module_name,version
+				  FROM module_info() WHERE version IS NOT NULL;});
+like(
+	$log_contents,
+	qr/auto_explain\|auto_explain\|1\.0\.0/,
+	"Check module version");
+
 # Prepared query.
 $log_contents = query_log($node,
 	q{PREPARE get_proc(name) AS SELECT * FROM pg_proc WHERE proname = $1; EXECUTE get_proc('int4pl');}
diff --git a/contrib/pg_prewarm/t/001_basic.pl b/contrib/pg_prewarm/t/001_basic.pl
index 825d3448ee..174822befc 100644
--- a/contrib/pg_prewarm/t/001_basic.pl
+++ b/contrib/pg_prewarm/t/001_basic.pl
@@ -25,8 +25,14 @@ $node->safe_psql("postgres",
 	  . "CREATE TABLE test(c1 int);\n"
 	  . "INSERT INTO test SELECT generate_series(1, 100);");
 
-# test read mode
+# test empty module info
 my $result =
+  $node->safe_psql("postgres", "SELECT substring(libname, 'pg_prewarm'),
+  						module_name,version FROM module_info();");
+like($result, qr/pg_prewarm\|\|/, 'Return null if module does not provide an info');
+
+# test read mode
+$result =
   $node->safe_psql("postgres", "SELECT pg_prewarm('test', 'read');");
 like($result, qr/^[1-9][0-9]*$/, 'read mode succeeded');
 
diff --git a/src/backend/utils/fmgr/dfmgr.c b/src/backend/utils/fmgr/dfmgr.c
index 8e81ecc749..20cca3e3d5 100644
--- a/src/backend/utils/fmgr/dfmgr.c
+++ b/src/backend/utils/fmgr/dfmgr.c
@@ -21,10 +21,12 @@
 #endif							/* !WIN32 */
 
 #include "fmgr.h"
+#include "funcapi.h"
 #include "lib/stringinfo.h"
 #include "miscadmin.h"
 #include "storage/fd.h"
 #include "storage/shmem.h"
+#include "utils/builtins.h"
 #include "utils/hsearch.h"
 
 
@@ -51,6 +53,7 @@ typedef struct df_files
 	ino_t		inode;			/* Inode number of file */
 #endif
 	void	   *handle;			/* a handle for pg_dl* functions */
+	const pg_module_info *minfo;
 	char		filename[FLEXIBLE_ARRAY_MEMBER];	/* Full pathname of file */
 } DynamicFileList;
 
@@ -75,7 +78,7 @@ static char *substitute_libpath_macro(const char *name);
 static char *find_in_dynamic_libpath(const char *basename);
 
 /* Magic structure that module needs to match to be accepted */
-static const Pg_magic_struct magic_data = PG_MODULE_MAGIC_DATA;
+static const Pg_magic_struct magic_data = PG_MODULE_MAGIC_DATA(0);
 
 
 /*
@@ -245,8 +248,14 @@ internal_load_library(const char *libname)
 		{
 			const Pg_magic_struct *magic_data_ptr = (*magic_func) ();
 
+			/*
+			 * Check magic field from loading library to be sure it  compiled
+			 * for the same Postgres code. Skip maintainer fields at the end
+			 * of the struct.
+			 */
 			if (magic_data_ptr->len != magic_data.len ||
-				memcmp(magic_data_ptr, &magic_data, magic_data.len) != 0)
+				memcmp(magic_data_ptr, &magic_data,
+					   offsetof(Pg_magic_struct, module_extra)) != 0)
 			{
 				/* copy data block before unlinking library */
 				Pg_magic_struct module_magic_data = *magic_data_ptr;
@@ -258,6 +267,9 @@ internal_load_library(const char *libname)
 				/* issue suitable complaint */
 				incompatible_module_error(libname, &module_magic_data);
 			}
+
+			/* Save link to the maintainer-provided info */
+			file_scanner->minfo = &magic_data_ptr->module_extra;
 		}
 		else
 		{
@@ -675,3 +687,49 @@ RestoreLibraryState(char *start_address)
 		start_address += strlen(start_address) + 1;
 	}
 }
+
+Datum
+module_info(PG_FUNCTION_ARGS)
+{
+	FuncCallContext	   *funcctx;
+	MemoryContext		oldcontext;
+	DynamicFileList	   *file_scanner = NULL;
+	TupleDesc			tupdesc;
+	Datum				result;
+	Datum				values[3];
+	bool				isnull[3] = {0,0,0};
+
+	if (SRF_IS_FIRSTCALL())
+	{
+		funcctx = SRF_FIRSTCALL_INIT();
+		oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
+
+		if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
+			elog(ERROR, "return type must be a row type");
+
+		file_scanner = file_list;
+
+		MemoryContextSwitchTo(oldcontext);
+	}
+
+	funcctx = SRF_PERCALL_SETUP();
+
+	if (file_scanner != NULL)
+	{
+		if (file_scanner->minfo->name == NULL)
+			isnull[0] = true;
+		else
+			values[0] = CStringGetTextDatum(file_scanner->minfo->name);
+		if (file_scanner->minfo->version == NULL)
+			isnull[1] = true;
+		else
+			values[1] = CStringGetTextDatum(file_scanner->minfo->version);
+
+		values[2] = CStringGetTextDatum(file_scanner->filename);
+		result = HeapTupleGetDatum(heap_form_tuple(tupdesc, values, isnull));
+		file_scanner = file_scanner->next;
+		SRF_RETURN_NEXT(funcctx, result);
+	}
+	else
+		SRF_RETURN_DONE(funcctx);
+}
diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat
index 0f22c21723..51cb23f827 100644
--- a/src/include/catalog/pg_proc.dat
+++ b/src/include/catalog/pg_proc.dat
@@ -311,6 +311,12 @@
   proname => 'scalargtjoinsel', provolatile => 's', prorettype => 'float8',
   proargtypes => 'internal oid internal int2 internal',
   prosrc => 'scalargtjoinsel' },
+{ oid => '111', descr => 'Module Info',
+  proname => 'module_info', provolatile => 's', prorettype => 'record',
+  proretset => 't', proargtypes => '', proallargtypes => '{text,text,text}',
+  proargmodes => '{o,o,o}', prorows => 10,
+  proargnames => '{module_name,version,libname}',
+  prosrc => 'module_info' },
 
 { oid => '336',
   descr => 'restriction selectivity of <= and related operators on scalar datatypes',
diff --git a/src/include/fmgr.h b/src/include/fmgr.h
index 1e3795de4a..ea21e15332 100644
--- a/src/include/fmgr.h
+++ b/src/include/fmgr.h
@@ -459,6 +459,12 @@ extern PGDLLEXPORT void _PG_init(void);
  *-------------------------------------------------------------------------
  */
 
+typedef struct pg_module_info
+{
+	const char *name;
+	const char *version;
+} pg_module_info;
+
 /* Definition of the magic block structure */
 typedef struct
 {
@@ -469,10 +475,15 @@ typedef struct
 	int			namedatalen;	/* NAMEDATALEN */
 	int			float8byval;	/* FLOAT8PASSBYVAL */
 	char		abi_extra[32];	/* see pg_config_manual.h */
+	pg_module_info module_extra;
 } Pg_magic_struct;
 
-/* The actual data block contents */
-#define PG_MODULE_MAGIC_DATA \
+/* The actual data block contents
+ *
+ * Fill the module info part with zeros by default. Zero-length module name
+ * indicates that it is not initialised.
+ */
+#define PG_MODULE_MAGIC_DATA(...) \
 { \
 	sizeof(Pg_magic_struct), \
 	PG_VERSION_NUM / 100, \
@@ -481,6 +492,7 @@ typedef struct
 	NAMEDATALEN, \
 	FLOAT8PASSBYVAL, \
 	FMGR_ABI_EXTRA, \
+	{__VA_ARGS__}, \
 }
 
 StaticAssertDecl(sizeof(FMGR_ABI_EXTRA) <= sizeof(((Pg_magic_struct *) 0)->abi_extra),
@@ -500,11 +512,21 @@ extern PGDLLEXPORT const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void); \
 const Pg_magic_struct * \
 PG_MAGIC_FUNCTION_NAME(void) \
 { \
-	static const Pg_magic_struct Pg_magic_data = PG_MODULE_MAGIC_DATA; \
+	static const Pg_magic_struct Pg_magic_data = PG_MODULE_MAGIC_DATA(0); \
 	return &Pg_magic_data; \
 } \
 extern int no_such_variable
 
+#define PG_MODULE_MAGIC_EXT(...) \
+extern PGDLLEXPORT const Pg_magic_struct *PG_MAGIC_FUNCTION_NAME(void); \
+const Pg_magic_struct * \
+PG_MAGIC_FUNCTION_NAME(void) \
+{ \
+	static const Pg_magic_struct Pg_magic_data = \
+		PG_MODULE_MAGIC_DATA(__VA_ARGS__); \
+	return &Pg_magic_data; \
+} \
+extern int no_such_variable
 
 /*-------------------------------------------------------------------------
  *		Support routines and macros for callers of fmgr-compatible functions
-- 
2.39.5

