postgres_fdw: Fix crash when estimating joins with functions

Started by Fujii Masao13 days ago3 messageshackers
Beta feature

Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.

won't retrysuccessCI history

This thread has been committed, so CI has stopped here. Anything below is the last result it produced.

You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:

docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t253729
psql -h localhost -U postgres

Built from patchset v1 (message #1), September 09, 2026 at 02:16 PM.

Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:

git clone --branch t253729_1 https://github.com/hackorum-dev/postgres.git

In a checkout you already have, add the fork once:

git remote add hackorum https://github.com/hackorum-dev/postgres.git

then, for this patchset and every later one:

git fetch hackorum t253729_1 && git checkout t253729_1

Patchset v1 (message #1) is on t253729_1

Jump to latest
#1Fujii Masao
masao.fujii@gmail.com

Hi,

On master, I found that the following causes a segmentation fault:

CREATE EXTENSION postgres_fdw;
CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw;
CREATE USER MAPPING FOR PUBLIC SERVER loopback;
CREATE TABLE t (i int);
CREATE FOREIGN TABLE ft (i int) SERVER loopback
OPTIONS (table_name 't', use_remote_estimate 'true');
EXPLAIN SELECT * FROM generate_series(1, 3) g JOIN ft ON ft.i = g;

This crash seems to occur because init_func_stub_fpinfo() forgets to copy
the user mapping to the function-side stub, leaving it NULL. The NULL
mapping is then passed to GetConnection() when requesting a remote
estimate, causing the segmentation fault.

This seems to have been introduced by commit 0ee83dd4a99.

The attached patch fixes this by also copying the user mapping from the
foreign side.

Regards,

--
Fujii Masao

Attachments:

t253729_1
v1-0001-postgres_fdw-Fix-crash-when-estimating-joins-with.patchapplication/octet-stream; name=v1-0001-postgres_fdw-Fix-crash-when-estimating-joins-with.patchDownload+2-2
#2Alexander Pyhalov
a.pyhalov@postgrespro.ru
In reply to: Fujii Masao (#1)
Re: postgres_fdw: Fix crash when estimating joins with functions

Fujii Masao писал(а) 2026-09-09 17:02:

Hi,

On master, I found that the following causes a segmentation fault:

CREATE EXTENSION postgres_fdw;
CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw;
CREATE USER MAPPING FOR PUBLIC SERVER loopback;
CREATE TABLE t (i int);
CREATE FOREIGN TABLE ft (i int) SERVER loopback
OPTIONS (table_name 't', use_remote_estimate 'true');
EXPLAIN SELECT * FROM generate_series(1, 3) g JOIN ft ON ft.i = g;

This crash seems to occur because init_func_stub_fpinfo() forgets to
copy
the user mapping to the function-side stub, leaving it NULL. The NULL
mapping is then passed to GetConnection() when requesting a remote
estimate, causing the segmentation fault.

This seems to have been introduced by commit 0ee83dd4a99.

The attached patch fixes this by also copying the user mapping from the
foreign side.

Regards,

Hi. Thanks. Looks good to me.

--
Best regards,
Alexander Pyhalov,
Postgres Professional

#3Fujii Masao
masao.fujii@gmail.com
In reply to: Alexander Pyhalov (#2)
Re: postgres_fdw: Fix crash when estimating joins with functions

On Thu, Sep 10, 2026 at 12:09 AM Alexander Pyhalov
<a.pyhalov@postgrespro.ru> wrote:

Hi. Thanks. Looks good to me.

Thanks for the review! I've pushed the patch.

Regards,

--
Fujii Masao