From 2688096e81d415261cb65429550f80c201642acb Mon Sep 17 00:00:00 2001
From: Justin Pryzby <pryzbyj@telsasoft.com>
Date: Wed, 22 Dec 2021 19:24:43 -0600
Subject: [PATCH 3/3] wip: allow dlopen to error() rather than stat()

TODO: test warning message:
./
/dev/null
postmaster.pid
/bin/false
non library
---
 contrib/test_decoding/expected/slot.out       |  2 +-
 src/backend/utils/fmgr/dfmgr.c                | 20 ++++++++-----------
 .../regress/expected/create_function_0.out    |  2 +-
 3 files changed, 10 insertions(+), 14 deletions(-)

diff --git a/contrib/test_decoding/expected/slot.out b/contrib/test_decoding/expected/slot.out
index 63a9940f73a..48085ad4ef5 100644
--- a/contrib/test_decoding/expected/slot.out
+++ b/contrib/test_decoding/expected/slot.out
@@ -31,7 +31,7 @@ SELECT 'init' FROM pg_create_logical_replication_slot('regression_slot_t2', 'tes
 (1 row)
 
 SELECT pg_create_logical_replication_slot('foo', 'nonexistent');
-ERROR:  could not access file "nonexistent": No such file or directory
+ERROR:  could not load library: nonexistent: cannot open shared object file: No such file or directory
 -- here we want to start a new session and wait till old one is gone
 select pg_backend_pid() as oldpid \gset
 \c -
diff --git a/src/backend/utils/fmgr/dfmgr.c b/src/backend/utils/fmgr/dfmgr.c
index a9e2ec5aafa..c68037a3a8f 100644
--- a/src/backend/utils/fmgr/dfmgr.c
+++ b/src/backend/utils/fmgr/dfmgr.c
@@ -266,18 +266,14 @@ internal_load_library(const char *libname, const char *gucname)
 		/*
 		 * Check for same files - different paths (ie, symlink or link)
 		 */
-		if (stat(libname, &stat_buf) == -1)
-			ereport(ERROR,
-					(errcode_for_file_access(),
-					 gucname ? errcontext("guc \"%s\"", gucname) : 0,
-					 errmsg("could not access file \"%s\": %m",
-							libname)));
-
-		for (file_scanner = file_list;
-			 file_scanner != NULL &&
-			 !SAME_INODE(stat_buf, *file_scanner);
-			 file_scanner = file_scanner->next)
-			;
+		if (stat(libname, &stat_buf) != -1)
+		{
+			for (file_scanner = file_list;
+				 file_scanner != NULL &&
+				 !SAME_INODE(stat_buf, *file_scanner);
+				 file_scanner = file_scanner->next)
+				;
+		}
 	}
 
 	if (file_scanner == NULL)
diff --git a/src/test/regress/expected/create_function_0.out b/src/test/regress/expected/create_function_0.out
index 6e96d6c5d66..476be3d0544 100644
--- a/src/test/regress/expected/create_function_0.out
+++ b/src/test/regress/expected/create_function_0.out
@@ -85,7 +85,7 @@ CREATE FUNCTION test1 (int) RETURNS int LANGUAGE SQL
 ERROR:  only one AS item needed for language "sql"
 CREATE FUNCTION test1 (int) RETURNS int LANGUAGE C
     AS 'nosuchfile';
-ERROR:  could not access file "nosuchfile": No such file or directory
+ERROR:  could not load library: nosuchfile: cannot open shared object file: No such file or directory
 -- To produce stable regression test output, we have to filter the name
 -- of the regresslib file out of the error message in this test.
 \set VERBOSITY sqlstate
-- 
2.17.1

