BUG #19518: Path resolution for loading a function fails, if postgres is somewhere in the path.

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: clemens(dot)ruck(at)t-online(dot)de
Subject: BUG #19518: Path resolution for loading a function fails, if postgres is somewhere in the path.
Date: 2026-06-12 19:19:31
Message-ID: 19518-902fabd6a225851f@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19518
Logged by: Clemens Ruck
Email address: clemens(dot)ruck(at)t-online(dot)de
PostgreSQL version: 17.4
Operating system: Ubuntu 24.04.04
Description:

I encountered an incorrect $libdir expansion when loading a C-language
function from a shared library.

My directory layout is:

/home/testuser/projects/hol-postgres/
install/bin/postgres
install/lib/

/home/testuser/projects/hol-lambdas/
src/ext/lambda_udfs.so

PostgreSQL was configured with:

./configure \
--prefix=/home/testuser/projects/hol-postgres/install \
--with-llvm

The following statement fails:

CREATE OR REPLACE FUNCTION test(integer)
RETURNS integer
AS '$libdir/../../../hol-lambdas/src/ext/l_udfs.so',
'l_test'
LANGUAGE C
STRICT;

The expected expansion is:

$libdir
= /home/testuser/projects/hol-postgres/install/lib

and therefore:

$libdir/../../../hol-lambdas/src/ext/lambda_udfs.so
= /home/testuser/projects/hol-lambdas/src/ext/lambda_udfs.so

That file exists and is readable by the PostgreSQL server process.

However, PostgreSQL resolves $libdir to an unexpected directory and reports
that the shared library cannot be found.

The issue depends on the surrounding directory names. With the PostgreSQL
repository located under a differently named directory, the same build and
SQL statement resolve the expected path.

The server binary and matching pg_config are used explicitly:

/home/testuser/projects/hol-postgres/install/bin/postgres
/home/testuser/projects/hol-postgres/install/bin/pg_config

and:

pg_config --pkglibdir

reports:

/home/testuser/projects/hol-postgres/install/lib

The relevant loading path appears to be:

src/backend/utils/fmgr/dfmgr.c
load_external_function()
expand_dynamic_library_name()
internal_load_library()

$libdir is expanded from pkglib_path. That path is initialized using:

get_pkglib_path(my_exec_path, pkglib_path)

which calls make_relative_path() in src/port/path.c.

I suspect that the relocatable-installation suffix matching in
make_relative_path() incorrectly recognizes part of the actual executable
path as the compiled-in PGBINDIR suffix, causing an incorrect relocated
PKGLIBDIR to be derived.

Expected behavior:

The running server should derive:

/home/testuser/projects/hol-postgres/install/lib

as pkglib_path, consistently with the matching pg_config --pkglibdir output.

Actual behavior:

The backend derives a different path, causing $libdir references in CREATE
FUNCTION ... LANGUAGE C to fail.

I can provide the exact compiled values of PGBINDIR and PKGLIBDIR, the
resulting runtime pkglib_path, and a patch or regression test if required.

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Baji Shaik 2026-06-12 22:34:59 Re: uuidv7 improperly accepts dates before 1970-01-01
Previous Message Hüseyin Demir 2026-06-12 16:22:03 Re: BUG #19483: pg_upgrade fails with orphan records in pg_init_priv catalog table