On Thu Nov 12, 2026 at 12:05 AM -03, Chao Li wrote:
> Hi Matheus,
>
> Thank you so much for discussing this with Peter and bringing the
> message back.
>
> For 0001, we can revert to v1’s approach and silently discard empty
> path elements from extension_control_path.
>
> One thing to note is that a completely empty extension_control_path
> ('') will still be interpreted as $system, preserving the existing
> behavior. A value containing only separators, such as ':', will
> provide no search directories, so no extensions will be found.
>
> For 0002, I revisited the example Surya provided:
> ```
>     SET extension_control_path = '$system:rel';
>     SELECT name, location FROM pg_available_extensions WHERE name = 'relext';
>       name  |   location
>     --------+---------------
>      relext | rel/extension
>
>     CREATE EXTENSION relext;
>     ERROR:  component in parameter "extension_control_path" is not an 
> absolute path
> ```
>
> I think the key problem is that pg_available_extensions lists
> extensions found through relative paths, but CREATE EXTENSION fails
> when it reaches a relative path, before searching that directory.
>
> So I think we can also silently discard relative path elements. In the
> example above, pg_available_extensions will return no row for relext,
> and CREATE EXTENSION will fail with extension "relext" is not
> available, assuming relext is available only through rel. This
> approach also eliminates the inconsistency.
>
> This introduces a behavior change for CREATE EXTENSION. For example,
> with extension_control_path = '/path1:relative:/path2', CREATE
> EXTENSION currently fails if the search reaches 'relative'. With
> v5-0002, it will skip that element and proceed if the extension is
> found in /path2; otherwise, it will report that the extension is not
> available. I think that should be fine.
>

Thanks for the new version!

I tested v5 on master and REL_19_STABLE, where it applies cleanly and
the TAP tests pass. With '$system:', ':$system', '$system:rel',
'rel:$system' and '$system/foo:$system', pg_available_extensions,
CREATE EXTENSION and the unknown-language hint now behave the same, and
'' still means $system.

Just a note about the 0001 tests: they also pass on master without the
patch. On master an empty element is expanded to /extension, which
usually doesn't exist, so it's just skipped and the extension is still
found on the remaining directories. The difference is only visible if
/extension exists, which we can't create on a TAP test, so I don't think
that there is anything we can do here, but the tests are still useful to
prevent regressions. The 0002 tests fail on master as expected.

v5 doesn't apply on REL_18_STABLE, since get_extension_control_directories()
there returns a List of plain char * paths instead of ExtensionLocation,
and pg_available_extensions doesn't have the location column. The
attached patches are a backport of v5 to REL_18_STABLE. The changes are:

- The empty and relative element checks are the same, but applied to
  the List of char * paths, using the "$system" string directly since
  there is no EXTENSION_SYSTEM_MACRO on 18.
- find_in_paths() is made static and simplified in the same way.
- The tests that check the location column were changed to check
  whether the extension is listed with count(*), and use $ext_dir since
  $ext_dir_canonicalized doesn't exist on the 18 test.

The patches have the .nocfbot suffix so they don't break the CFBot.

I'm attaching as v6, but 0001 and 0002 are unchanged.

--
Matheus Alcantara
EDB: https://www.enterprisedb.com
From c68b3b795430d0fffb806248f6d0c631090bd1d4 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <[email protected]>
Date: Tue, 3 Nov 2026 09:55:18 +0800
Subject: [PATCH v6 1/2] Ignore empty elements in extension_control_path

Empty path elements were expanded to /extension by the automatic suffix
addition, causing an unintended search of that directory.

Silently discard empty elements before appending the suffix, while
continuing to search the remaining directories. This avoids introducing
errors for existing settings with leading or trailing separators.

Preserve the documented $system default for an entirely empty setting.
A nonempty setting containing only separators provides no search
directories.

Document the behavior and add TAP coverage for empty elements,
separator-only settings, and unknown-language errors and hints.

Author: Chao Li <[email protected]>
Reviewed-by: Prashant Chinnam <[email protected]>
Reviewed-by: Surya Poondla <[email protected]>
Reviewed-by: Matheus Alcantara <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
 doc/src/sgml/config.sgml                      |  1 +
 src/backend/commands/extension.c              | 54 ++++++++++---------
 .../t/001_extension_control_path.pl           | 37 +++++++++++++
 3 files changed, 68 insertions(+), 24 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 7018ca1e3ea..0c0510d306c 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -11783,6 +11783,7 @@ extension_control_path = 
'C:\tools\postgresql;H:\my_project\share;$system'
         The default value for this parameter is
         <literal>'$system'</literal>. If the value is set to an empty
         string, the default <literal>'$system'</literal> is also assumed.
+        Empty elements within a nonempty value are ignored.
        </para>
 
        <para>
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 35443dabecf..6fdaf6aa963 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -511,6 +511,7 @@ is_extension_script_filename(const char *filename)
 
 /*
  * Return a list of directories declared in the extension_control_path GUC.
+ * Empty elements are ignored.
  */
 static List *
 get_extension_control_directories(void)
@@ -541,9 +542,7 @@ get_extension_control_directories(void)
                for (;;)
                {
                        int                     len;
-                       char       *mangled;
                        char       *piece = first_path_var_separator(ecp);
-                       ExtensionLocation *location = 
palloc_object(ExtensionLocation);
 
                        /* Get the length of the next path on ecp */
                        if (piece == NULL)
@@ -551,30 +550,37 @@ get_extension_control_directories(void)
                        else
                                len = piece - ecp;
 
-                       /* Copy the next path found on ecp */
-                       piece = palloc(len + 1);
-                       strlcpy(piece, ecp, len + 1);
-
-                       /*
-                        * Substitute the path macro if needed or append 
"extension"
-                        * suffix if it is a custom extension control path.
-                        */
-                       if (strcmp(piece, EXTENSION_SYSTEM_MACRO) == 0)
+                       /* Ignore empty elements instead of expanding them to 
/extension */
+                       if (len > 0)
                        {
-                               location->macro = pstrdup(piece);
-                               mangled = substitute_path_macro(piece, 
EXTENSION_SYSTEM_MACRO, system_dir);
-                       }
-                       else
-                       {
-                               location->macro = NULL;
-                               mangled = psprintf("%s/extension", piece);
-                       }
-                       pfree(piece);
+                               char       *mangled;
+                               ExtensionLocation *location = 
palloc_object(ExtensionLocation);
 
-                       /* Canonicalize the path based on the OS and add to the 
list */
-                       canonicalize_path(mangled);
-                       location->loc = mangled;
-                       paths = lappend(paths, location);
+                               /* Copy the next path found on ecp */
+                               piece = palloc(len + 1);
+                               strlcpy(piece, ecp, len + 1);
+
+                               /*
+                                * Substitute the path macro if needed or 
append "extension"
+                                * suffix if it is a custom extension control 
path.
+                                */
+                               if (strcmp(piece, EXTENSION_SYSTEM_MACRO) == 0)
+                               {
+                                       location->macro = pstrdup(piece);
+                                       mangled = substitute_path_macro(piece, 
EXTENSION_SYSTEM_MACRO, system_dir);
+                               }
+                               else
+                               {
+                                       location->macro = NULL;
+                                       mangled = psprintf("%s/extension", 
piece);
+                               }
+                               pfree(piece);
+
+                               /* Canonicalize the path based on the OS and 
add to the list */
+                               canonicalize_path(mangled);
+                               location->loc = mangled;
+                               paths = lappend(paths, location);
+                       }
 
                        /* Break if ecp is empty or move to the next path on 
ecp */
                        if (ecp[len] == '\0')
diff --git a/src/test/modules/test_extensions/t/001_extension_control_path.pl 
b/src/test/modules/test_extensions/t/001_extension_control_path.pl
index 4a013a7da4b..5fb811b8e8d 100644
--- a/src/test/modules/test_extensions/t/001_extension_control_path.pl
+++ b/src/test/modules/test_extensions/t/001_extension_control_path.pl
@@ -115,6 +115,43 @@ is($ret, "\$system",
        "\$system location is shown correctly in pg_available_extensions with 
empty extension_control_path"
 );
 
+# Empty path elements do not prevent searching the remaining directories.
+for my $path (
+       "$sep$ext_dir_canonicalized",
+       "$ext_dir_canonicalized$sep")
+{
+       $ret = $node->safe_psql(
+               'postgres',
+               "set extension_control_path = '$path';
+                select location from pg_available_extensions where name = 
'$ext_name'");
+       is($ret, "$ext_dir_canonicalized/extension",
+               "extension is found with an empty element in path '$path'");
+}
+
+# A separator-only value provides no directories, unlike an empty value.
+$ret = $node->safe_psql(
+       'postgres',
+       "set extension_control_path = '$sep';
+        select count(*) from pg_available_extensions");
+is($ret, '0', 'separator-only path lists no extensions');
+
+# Empty elements must not replace an unknown-language error or prevent a hint
+# from being found using the remaining directories.
+for my $sql (
+       "create function test_invalid_language() returns int language $ext_name 
as 'x'",
+       "do language $ext_name 'x'")
+{
+       my ($code, $stdout, $stderr) = $node->psql(
+               'postgres',
+               "set extension_control_path = '$sep$ext_dir_canonicalized$sep'; 
$sql");
+       is($code, 3, "unknown language is rejected by $sql");
+       like($stderr, qr/ERROR:  language "\Q$ext_name\E" does not exist/,
+               "invalid path does not replace the unknown-language error");
+       like($stderr,
+               qr/HINT:  Use CREATE EXTENSION to load the language into the 
database\./,
+               "remaining directory provides the extension hint");
+}
+
 # Test with an extension that does not exists
 my ($code, $stdout, $stderr) =
   $node->psql('postgres', "CREATE EXTENSION invalid");
-- 
2.50.1 (Apple Git-155)

From 72a0a08aa4275549bbaea88c1d30d3129b7c049c Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <[email protected]>
Date: Mon, 9 Nov 2026 16:15:01 +0800
Subject: [PATCH v6 2/2] Ignore relative elements in extension_control_path

The available-extension views could list extensions found through
relative paths, while CREATE EXTENSION rejected those paths when its
search reached them.

Discard relative elements in get_extension_control_directories() so
all callers search only absolute paths. Extensions found only through
relative elements are neither listed nor loaded, while valid absolute
elements remain usable.

Canonicalize system_dir in get_extension_control_directories() so all
returned paths are canonicalized. Make find_in_paths() static and remove
its redundant path copying and canonicalization, retaining an assertion
that paths are absolute.

Document the behavior and add TAP coverage using an actual relative
directory, including checks that absolute directories remain usable
with relative elements before or after them.

Reported-by: Surya Poondla <[email protected]>
Author: Chao Li <[email protected]>
Reviewed-by: Surya Poondla <[email protected]>
Reviewed-by: Matheus Alcantara <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
 doc/src/sgml/config.sgml                      |  1 +
 src/backend/commands/extension.c              | 51 ++++++--------
 .../t/001_extension_control_path.pl           | 69 ++++++++++++++++---
 3 files changed, 82 insertions(+), 39 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 0c0510d306c..d3319710b1d 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -11784,6 +11784,7 @@ extension_control_path = 
'C:\tools\postgresql;H:\my_project\share;$system'
         <literal>'$system'</literal>. If the value is set to an empty
         string, the default <literal>'$system'</literal> is also assumed.
         Empty elements within a nonempty value are ignored.
+        Relative path elements are also ignored.
        </para>
 
        <para>
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 6fdaf6aa963..05b3e486278 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -195,8 +195,7 @@ static void 
ExecAlterExtensionContentsRecurse(AlterExtensionContentsStmt *stmt,
                                                                                
          ObjectAddress object);
 static char *read_whole_file(const char *filename, int *length);
 static ExtensionControlFile *new_ExtensionControlFile(const char *extname);
-
-char      *find_in_paths(const char *basename, List *paths);
+static char *find_in_paths(const char *basename, List *paths);
 
 /*
  *  Return the extension location. If the current user doesn't have sufficient
@@ -511,7 +510,8 @@ is_extension_script_filename(const char *filename)
 
 /*
  * Return a list of directories declared in the extension_control_path GUC.
- * Empty elements are ignored.
+ * All returned directory paths are absolute and canonicalized.
+ * Empty and relative elements are ignored.
  */
 static List *
 get_extension_control_directories(void)
@@ -525,6 +525,7 @@ get_extension_control_directories(void)
        get_share_path(my_exec_path, sharepath);
 
        system_dir = psprintf("%s/extension", sharepath);
+       canonicalize_path(system_dir);
 
        if (strlen(Extension_control_path) == 0)
        {
@@ -554,7 +555,7 @@ get_extension_control_directories(void)
                        if (len > 0)
                        {
                                char       *mangled;
-                               ExtensionLocation *location = 
palloc_object(ExtensionLocation);
+                               bool            is_system;
 
                                /* Copy the next path found on ecp */
                                piece = palloc(len + 1);
@@ -564,22 +565,25 @@ get_extension_control_directories(void)
                                 * Substitute the path macro if needed or 
append "extension"
                                 * suffix if it is a custom extension control 
path.
                                 */
-                               if (strcmp(piece, EXTENSION_SYSTEM_MACRO) == 0)
-                               {
-                                       location->macro = pstrdup(piece);
+                               is_system = strcmp(piece, 
EXTENSION_SYSTEM_MACRO) == 0;
+                               if (is_system)
                                        mangled = substitute_path_macro(piece, 
EXTENSION_SYSTEM_MACRO, system_dir);
-                               }
                                else
-                               {
-                                       location->macro = NULL;
                                        mangled = psprintf("%s/extension", 
piece);
-                               }
                                pfree(piece);
 
                                /* Canonicalize the path based on the OS and 
add to the list */
                                canonicalize_path(mangled);
-                               location->loc = mangled;
-                               paths = lappend(paths, location);
+                               if (!is_absolute_path(mangled))
+                                       pfree(mangled);
+                               else
+                               {
+                                       ExtensionLocation *location = 
palloc_object(ExtensionLocation);
+
+                                       location->macro = is_system ? 
pstrdup(EXTENSION_SYSTEM_MACRO) : NULL;
+                                       location->loc = mangled;
+                                       paths = lappend(paths, location);
+                               }
                        }
 
                        /* Break if ecp is empty or move to the next path on 
ecp */
@@ -4229,12 +4233,10 @@ new_ExtensionControlFile(const char *extname)
 }
 
 /*
- * Search for the basename in the list of paths.
- *
- * Similar to find_in_path but for simplicity does not support custom error
- * messages and expects that paths already have all macros replaced.
+ * Search for the basename in the list of absolute, canonicalized paths
+ * returned by get_extension_control_directories().
  */
-char *
+static char *
 find_in_paths(const char *basename, List *paths)
 {
        ListCell   *cell;
@@ -4242,26 +4244,17 @@ find_in_paths(const char *basename, List *paths)
        foreach(cell, paths)
        {
                ExtensionLocation *location = lfirst(cell);
-               char       *path = location->loc;
+               const char *path = location->loc;
                char       *full;
 
                Assert(path != NULL);
-
-               path = pstrdup(path);
-               canonicalize_path(path);
-
-               /* only absolute paths */
-               if (!is_absolute_path(path))
-                       ereport(ERROR,
-                                       errcode(ERRCODE_INVALID_NAME),
-                                       errmsg("component in parameter \"%s\" 
is not an absolute path", "extension_control_path"));
+               Assert(is_absolute_path(path));
 
                full = psprintf("%s/%s", path, basename);
 
                if (pg_file_exists(full))
                        return full;
 
-               pfree(path);
                pfree(full);
        }
 
diff --git a/src/test/modules/test_extensions/t/001_extension_control_path.pl 
b/src/test/modules/test_extensions/t/001_extension_control_path.pl
index 5fb811b8e8d..4fc29b72678 100644
--- a/src/test/modules/test_extensions/t/001_extension_control_path.pl
+++ b/src/test/modules/test_extensions/t/001_extension_control_path.pl
@@ -135,21 +135,70 @@ $ret = $node->safe_psql(
         select count(*) from pg_available_extensions");
 is($ret, '0', 'separator-only path lists no extensions');
 
-# Empty elements must not replace an unknown-language error or prevent a hint
+# Invalid elements must not replace an unknown-language error or prevent a hint
 # from being found using the remaining directories.
-for my $sql (
-       "create function test_invalid_language() returns int language $ext_name 
as 'x'",
-       "do language $ext_name 'x'")
+for my $path (
+       "$sep$ext_dir_canonicalized$sep",
+       "relative$sep$ext_dir_canonicalized${sep}relative")
+{
+       for my $sql (
+               "create function test_invalid_language() returns int language 
$ext_name as 'x'",
+               "do language $ext_name 'x'")
+       {
+               my ($code, $stdout, $stderr) = $node->psql(
+                       'postgres',
+                       "set extension_control_path = '$path'; $sql");
+               is($code, 3, "unknown language is rejected by $sql");
+               like($stderr, qr/ERROR:  language "\Q$ext_name\E" does not 
exist/,
+                       "invalid path does not replace the unknown-language 
error");
+               like($stderr,
+                       qr/HINT:  Use CREATE EXTENSION to load the language 
into the database\./,
+                       "remaining directory provides the extension hint");
+       }
+}
+
+# An extension found only through a relative path must not be listed or loaded.
+my $relative_dir = $node->data_dir . '/relative';
+mkpath("$relative_dir/extension");
+my $relative_ext = 'test_relative_path';
+create_extension($relative_ext, $relative_dir);
+$ret = $node->safe_psql(
+       'postgres',
+       "set extension_control_path = '\$system${sep}relative';
+        select count(*) from pg_available_extensions where name = 
'$relative_ext'");
+is($ret, '0', 'extension in a relative directory is not listed');
+
 {
        my ($code, $stdout, $stderr) = $node->psql(
                'postgres',
-               "set extension_control_path = '$sep$ext_dir_canonicalized$sep'; 
$sql");
-       is($code, 3, "unknown language is rejected by $sql");
-       like($stderr, qr/ERROR:  language "\Q$ext_name\E" does not exist/,
-               "invalid path does not replace the unknown-language error");
+               "set extension_control_path = '\$system${sep}relative';
+                create extension $relative_ext");
+       is($code, 3, 'extension in a relative directory cannot be loaded');
        like($stderr,
-               qr/HINT:  Use CREATE EXTENSION to load the language into the 
database\./,
-               "remaining directory provides the extension hint");
+               qr/ERROR:  extension "\Q$relative_ext\E" is not available/,
+               'relative directory is excluded from the extension search');
+}
+
+# The same extension can be found and loaded through an absolute path, even
+# with relative elements before or after that path.
+create_extension($relative_ext, $ext_dir);
+for my $path (
+       "relative$sep$ext_dir_canonicalized",
+       "$ext_dir_canonicalized${sep}relative")
+{
+       $ret = $node->safe_psql(
+               'postgres',
+               "set extension_control_path = '$path';
+                select location from pg_available_extensions where name = 
'$relative_ext'");
+       is($ret, "$ext_dir_canonicalized/extension",
+               "extension is listed from the absolute directory in path 
'$path'");
+       $ret = $node->safe_psql(
+               'postgres',
+               "set extension_control_path = '$path';
+                create extension $relative_ext;
+                select count(*) from pg_extension where extname = 
'$relative_ext';
+                drop extension $relative_ext");
+       is($ret, '1', "extension can be loaded using path '$path'");
 }
 
 # Test with an extension that does not exists
-- 
2.50.1 (Apple Git-155)

From 3cfb811beb69e4f418af0c4334c2e59d70ddcb66 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <[email protected]>
Date: Tue, 3 Nov 2026 09:55:18 +0800
Subject: [PATCH v6 REL_18 1/2] Ignore empty elements in extension_control_path

Empty path elements were expanded to /extension by the automatic suffix
addition, causing an unintended search of that directory.

Silently discard empty elements before appending the suffix, while
continuing to search the remaining directories. This avoids introducing
errors for existing settings with leading or trailing separators.

Preserve the documented $system default for an entirely empty setting.
A nonempty setting containing only separators provides no search
directories.

Document the behavior and add TAP coverage for empty elements,
separator-only settings, and unknown-language errors and hints.

Author: Chao Li <[email protected]>
Reviewed-by: Prashant Chinnam <[email protected]>
Reviewed-by: Surya Poondla <[email protected]>
Reviewed-by: Matheus Alcantara <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
 doc/src/sgml/config.sgml                      |  1 +
 src/backend/commands/extension.c              | 38 +++++++++++--------
 .../t/001_extension_control_path.pl           | 35 +++++++++++++++++
 3 files changed, 58 insertions(+), 16 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index ee151b15dc5..9a20dcf5422 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -11220,6 +11220,7 @@ extension_control_path = 
'C:\tools\postgresql;H:\my_project\share;$system'
         The default value for this parameter is
         <literal>'$system'</literal>. If the value is set to an empty
         string, the default <literal>'$system'</literal> is also assumed.
+        Empty elements within a nonempty value are ignored.
        </para>
 
        <para>
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 94383d6473f..7bd5b3ab181 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -470,6 +470,7 @@ is_extension_script_filename(const char *filename)
 
 /*
  * Return a list of directories declared on extension_control_path GUC.
+ * Empty elements are ignored.
  */
 static List *
 get_extension_control_directories(void)
@@ -495,7 +496,6 @@ get_extension_control_directories(void)
                for (;;)
                {
                        int                     len;
-                       char       *mangled;
                        char       *piece = first_path_var_separator(ecp);
 
                        /* Get the length of the next path on ecp */
@@ -504,24 +504,30 @@ get_extension_control_directories(void)
                        else
                                len = piece - ecp;
 
-                       /* Copy the next path found on ecp */
-                       piece = palloc(len + 1);
-                       strlcpy(piece, ecp, len + 1);
+                       /* Ignore empty elements instead of expanding them to 
/extension */
+                       if (len > 0)
+                       {
+                               char       *mangled;
 
-                       /*
-                        * Substitute the path macro if needed or append 
"extension"
-                        * suffix if it is a custom extension control path.
-                        */
-                       if (strcmp(piece, "$system") == 0)
-                               mangled = substitute_path_macro(piece, 
"$system", system_dir);
-                       else
-                               mangled = psprintf("%s/extension", piece);
+                               /* Copy the next path found on ecp */
+                               piece = palloc(len + 1);
+                               strlcpy(piece, ecp, len + 1);
 
-                       pfree(piece);
+                               /*
+                                * Substitute the path macro if needed or 
append "extension"
+                                * suffix if it is a custom extension control 
path.
+                                */
+                               if (strcmp(piece, "$system") == 0)
+                                       mangled = substitute_path_macro(piece, 
"$system", system_dir);
+                               else
+                                       mangled = psprintf("%s/extension", 
piece);
 
-                       /* Canonicalize the path based on the OS and add to the 
list */
-                       canonicalize_path(mangled);
-                       paths = lappend(paths, mangled);
+                               pfree(piece);
+
+                               /* Canonicalize the path based on the OS and 
add to the list */
+                               canonicalize_path(mangled);
+                               paths = lappend(paths, mangled);
+                       }
 
                        /* Break if ecp is empty or move to the next path on 
ecp */
                        if (ecp[len] == '\0')
diff --git a/src/test/modules/test_extensions/t/001_extension_control_path.pl 
b/src/test/modules/test_extensions/t/001_extension_control_path.pl
index 7fbe5bde332..8a2e3222059 100644
--- a/src/test/modules/test_extensions/t/001_extension_control_path.pl
+++ b/src/test/modules/test_extensions/t/001_extension_control_path.pl
@@ -84,6 +84,41 @@ is($ret, "t",
        "\$system extension is installed correctly on pg_available_extensions 
with empty extension_control_path"
 );
 
+# Empty path elements do not prevent searching the remaining directories.
+for my $path ("$sep$ext_dir", "$ext_dir$sep")
+{
+       $ret = $node->safe_psql(
+               'postgres',
+               "set extension_control_path = '$path';
+                select count(*) from pg_available_extensions where name = 
'$ext_name'");
+       is($ret, '1',
+               "extension is found with an empty element in path '$path'");
+}
+
+# A separator-only value provides no directories, unlike an empty value.
+$ret = $node->safe_psql(
+       'postgres',
+       "set extension_control_path = '$sep';
+        select count(*) from pg_available_extensions");
+is($ret, '0', 'separator-only path lists no extensions');
+
+# Empty elements must not replace an unknown-language error or prevent a hint
+# from being found using the remaining directories.
+for my $sql (
+       "create function test_invalid_language() returns int language $ext_name 
as 'x'",
+       "do language $ext_name 'x'")
+{
+       my ($code, $stdout, $stderr) = $node->psql(
+               'postgres',
+               "set extension_control_path = '$sep$ext_dir$sep'; $sql");
+       is($code, 3, "unknown language is rejected by $sql");
+       like($stderr, qr/ERROR:  language "\Q$ext_name\E" does not exist/,
+               "invalid path does not replace the unknown-language error");
+       like($stderr,
+               qr/HINT:  Use CREATE EXTENSION to load the language into the 
database\./,
+               "remaining directory provides the extension hint");
+}
+
 # Test with an extension that does not exists
 my ($code, $stdout, $stderr) =
   $node->psql('postgres', "CREATE EXTENSION invalid");
-- 
2.50.1 (Apple Git-155)

From 2435d8b0bc35fb73f1a4f7fd23417421ebf50f62 Mon Sep 17 00:00:00 2001
From: "Chao Li (Evan)" <[email protected]>
Date: Mon, 9 Nov 2026 16:15:01 +0800
Subject: [PATCH v6 REL_18 2/2] Ignore relative elements in
 extension_control_path

The available-extension views could list extensions found through
relative paths, while CREATE EXTENSION rejected those paths when its
search reached them.

Discard relative elements in get_extension_control_directories() so
all callers search only absolute paths. Extensions found only through
relative elements are neither listed nor loaded, while valid absolute
elements remain usable.

Canonicalize system_dir in get_extension_control_directories() so all
returned paths are canonicalized. Make find_in_paths() static and remove
its redundant path copying and canonicalization, retaining an assertion
that paths are absolute.

Document the behavior and add TAP coverage using an actual relative
directory, including checks that absolute directories remain usable
with relative elements before or after them.

Reported-by: Surya Poondla <[email protected]>
Author: Chao Li <[email protected]>
Reviewed-by: Surya Poondla <[email protected]>
Reviewed-by: Matheus Alcantara <[email protected]>
Discussion: https://postgr.es/m/[email protected]
---
 doc/src/sgml/config.sgml                      |  1 +
 src/backend/commands/extension.c              | 35 ++++------
 .../t/001_extension_control_path.pl           | 65 ++++++++++++++++---
 3 files changed, 69 insertions(+), 32 deletions(-)

diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml
index 9a20dcf5422..e4f0e9dd662 100644
--- a/doc/src/sgml/config.sgml
+++ b/doc/src/sgml/config.sgml
@@ -11221,6 +11221,7 @@ extension_control_path = 
'C:\tools\postgresql;H:\my_project\share;$system'
         <literal>'$system'</literal>. If the value is set to an empty
         string, the default <literal>'$system'</literal> is also assumed.
         Empty elements within a nonempty value are ignored.
+        Relative path elements are also ignored.
        </para>
 
        <para>
diff --git a/src/backend/commands/extension.c b/src/backend/commands/extension.c
index 7bd5b3ab181..e8ff017fe48 100644
--- a/src/backend/commands/extension.c
+++ b/src/backend/commands/extension.c
@@ -177,8 +177,7 @@ static void 
ExecAlterExtensionContentsRecurse(AlterExtensionContentsStmt *stmt,
                                                                                
          ObjectAddress object);
 static char *read_whole_file(const char *filename, int *length);
 static ExtensionControlFile *new_ExtensionControlFile(const char *extname);
-
-char      *find_in_paths(const char *basename, List *paths);
+static char *find_in_paths(const char *basename, List *paths);
 
 /*
  * get_extension_oid - given an extension name, look up the OID
@@ -470,7 +469,8 @@ is_extension_script_filename(const char *filename)
 
 /*
  * Return a list of directories declared on extension_control_path GUC.
- * Empty elements are ignored.
+ * All returned directory paths are absolute and canonicalized.
+ * Empty and relative elements are ignored.
  */
 static List *
 get_extension_control_directories(void)
@@ -483,6 +483,7 @@ get_extension_control_directories(void)
        get_share_path(my_exec_path, sharepath);
 
        system_dir = psprintf("%s/extension", sharepath);
+       canonicalize_path(system_dir);
 
        if (strlen(Extension_control_path) == 0)
        {
@@ -526,7 +527,10 @@ get_extension_control_directories(void)
 
                                /* Canonicalize the path based on the OS and 
add to the list */
                                canonicalize_path(mangled);
-                               paths = lappend(paths, mangled);
+                               if (!is_absolute_path(mangled))
+                                       pfree(mangled);
+                               else
+                                       paths = lappend(paths, mangled);
                        }
 
                        /* Break if ecp is empty or move to the next path on 
ecp */
@@ -4164,40 +4168,27 @@ new_ExtensionControlFile(const char *extname)
 }
 
 /*
- * Work in a very similar way with find_in_path but it receives an already
- * parsed List of paths to search the basename and it do not support macro
- * replacement or custom error messages (for simplicity).
- *
- * By "already parsed List of paths" this function expected that paths already
- * have all macros replaced.
+ * Search for the basename in the list of absolute, canonicalized paths
+ * returned by get_extension_control_directories().
  */
-char *
+static char *
 find_in_paths(const char *basename, List *paths)
 {
        ListCell   *cell;
 
        foreach(cell, paths)
        {
-               char       *path = lfirst(cell);
+               const char *path = lfirst(cell);
                char       *full;
 
                Assert(path != NULL);
-
-               path = pstrdup(path);
-               canonicalize_path(path);
-
-               /* only absolute paths */
-               if (!is_absolute_path(path))
-                       ereport(ERROR,
-                                       errcode(ERRCODE_INVALID_NAME),
-                                       errmsg("component in parameter \"%s\" 
is not an absolute path", "extension_control_path"));
+               Assert(is_absolute_path(path));
 
                full = psprintf("%s/%s", path, basename);
 
                if (pg_file_exists(full))
                        return full;
 
-               pfree(path);
                pfree(full);
        }
 
diff --git a/src/test/modules/test_extensions/t/001_extension_control_path.pl 
b/src/test/modules/test_extensions/t/001_extension_control_path.pl
index 8a2e3222059..4fef262ac6f 100644
--- a/src/test/modules/test_extensions/t/001_extension_control_path.pl
+++ b/src/test/modules/test_extensions/t/001_extension_control_path.pl
@@ -102,21 +102,66 @@ $ret = $node->safe_psql(
         select count(*) from pg_available_extensions");
 is($ret, '0', 'separator-only path lists no extensions');
 
-# Empty elements must not replace an unknown-language error or prevent a hint
+# Invalid elements must not replace an unknown-language error or prevent a hint
 # from being found using the remaining directories.
-for my $sql (
-       "create function test_invalid_language() returns int language $ext_name 
as 'x'",
-       "do language $ext_name 'x'")
+for my $path ("$sep$ext_dir$sep", "relative$sep$ext_dir${sep}relative")
+{
+       for my $sql (
+               "create function test_invalid_language() returns int language 
$ext_name as 'x'",
+               "do language $ext_name 'x'")
+       {
+               my ($code, $stdout, $stderr) = $node->psql(
+                       'postgres',
+                       "set extension_control_path = '$path'; $sql");
+               is($code, 3, "unknown language is rejected by $sql");
+               like($stderr, qr/ERROR:  language "\Q$ext_name\E" does not 
exist/,
+                       "invalid path does not replace the unknown-language 
error");
+               like($stderr,
+                       qr/HINT:  Use CREATE EXTENSION to load the language 
into the database\./,
+                       "remaining directory provides the extension hint");
+       }
+}
+
+# An extension found only through a relative path must not be listed or loaded.
+my $relative_dir = $node->data_dir . '/relative';
+mkpath("$relative_dir/extension");
+my $relative_ext = 'test_relative_path';
+create_extension($relative_ext, $relative_dir);
+$ret = $node->safe_psql(
+       'postgres',
+       "set extension_control_path = '\$system${sep}relative';
+        select count(*) from pg_available_extensions where name = 
'$relative_ext'");
+is($ret, '0', 'extension in a relative directory is not listed');
+
 {
        my ($code, $stdout, $stderr) = $node->psql(
                'postgres',
-               "set extension_control_path = '$sep$ext_dir$sep'; $sql");
-       is($code, 3, "unknown language is rejected by $sql");
-       like($stderr, qr/ERROR:  language "\Q$ext_name\E" does not exist/,
-               "invalid path does not replace the unknown-language error");
+               "set extension_control_path = '\$system${sep}relative';
+                create extension $relative_ext");
+       is($code, 3, 'extension in a relative directory cannot be loaded');
        like($stderr,
-               qr/HINT:  Use CREATE EXTENSION to load the language into the 
database\./,
-               "remaining directory provides the extension hint");
+               qr/ERROR:  extension "\Q$relative_ext\E" is not available/,
+               'relative directory is excluded from the extension search');
+}
+
+# The same extension can be found and loaded through an absolute path, even
+# with relative elements before or after that path.
+create_extension($relative_ext, $ext_dir);
+for my $path ("relative$sep$ext_dir", "$ext_dir${sep}relative")
+{
+       $ret = $node->safe_psql(
+               'postgres',
+               "set extension_control_path = '$path';
+                select count(*) from pg_available_extensions where name = 
'$relative_ext'");
+       is($ret, '1',
+               "extension is listed from the absolute directory in path 
'$path'");
+       $ret = $node->safe_psql(
+               'postgres',
+               "set extension_control_path = '$path';
+                create extension $relative_ext;
+                select count(*) from pg_extension where extname = 
'$relative_ext';
+                drop extension $relative_ext");
+       is($ret, '1', "extension can be loaded using path '$path'");
 }
 
 # Test with an extension that does not exists
-- 
2.50.1 (Apple Git-155)

Reply via email to