Skip to content

[REL_2_STABLE] Link postgres_fdw against libpq.so to fix SCRAM disconnect crash - #2494

Merged
tuhaihe merged 2 commits into
apache:REL_2_STABLEfrom
yjhjstz:fix/postgres-fdw-dynamic-libpq-rel2
Dec 4, 2026
Merged

tuhaihe merged 2 commits into
apache:REL_2_STABLEfrom
yjhjstz:fix/postgres-fdw-dynamic-libpq-rel2

Conversation

@yjhjstz

@yjhjstz yjhjstz commented Dec 3, 2026

Copy link
Copy Markdown
Member

Backport of #2492 to REL_2_STABLE. The same two commits are applied with git cherry-pick -x:

  • Greenplum ceda5994991, which links postgres_fdw against libpq.so dynamically. It has the same conflict as on main (the extra -Bsymbolic and a GPDB_13_MERGE_FIXME comment), resolved to the Greenplum side.
  • The removal of the link-canary.o workaround. This one applied cleanly.

Fixes #2490 on the 2.x line. With a SCRAM-authenticated remote server, postgres_fdw crashes the coordinator whenever it closes a cached connection: postgres_fdw_disconnect*(), ALTER SERVER/ALTER USER MAPPING, or a cache reset processed at the start of the next statement. The cause is the statically linked libpq. It used the backend's pg_saslprep(), so it got a palloc'd password from ExecutorState and later called free() on it in scram_free(). See #2492 for the full analysis.

REL_2_STABLE already has 0b80005 (Greenplum #16234), which hides the backend's copy of the libpq symbols, so linking libpq.so is safe here. 2.1.0 and 2.2.0 include it as well. 2.0.0 does not. Its libpostgres.so still exports the PQ* symbols, so dblink with SCRAM crashes there too. Users on 2.0.x should upgrade to a 2.x release that contains this fix.

Testing on a demo cluster built from this branch, with the cluster itself as the remote server and a scram-sha-256 role:

  • Crash repro: before the patch, both cache-reset paths crash with free(): invalid pointer. One of them is the CREATE FOREIGN TABLE stack from terminated by signal 11 Segmentation fault #2490. With the patch, those paths and postgres_fdw_disconnect_all() all pass. md5 and dblink with SCRAM pass before and after.
  • Existing regression test: the GPDB #16219: validate scram-sha-256 in postgres_fdw section of gp_postgres_fdw already covers this bug. Without the patch, the backend crashes at its ALTER SERVER step. With the patch, the section passes. Other parts of gp_postgres_fdw and mpp_postgres_fdw still fail because their expected files are stale; that is unchanged by this PR, and these tests are not run in CI.

@yjhjstz yjhjstz added the cherry-pick cherry-pick upstream commts label Dec 3, 2026
@yjhjstz
yjhjstz force-pushed the fix/postgres-fdw-dynamic-libpq-rel2 branch from 6dadb83 to 731aaa0 Compare December 4, 2026 01:59
Comment thread contrib/postgres_fdw/postgres_fdw.h Resolved

@gfphoenix78 gfphoenix78 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

adam8157 and others added 2 commits December 3, 2026 19:38
This reverts commit 667f0c3.

It would also work without it, in the opposite way, it has two issues:

1, it failed to build on macOS, with an error "ld: unknown option:
--exclude-libs=libpq.a"
2, after the 12 merge, it triggers a PostgreSQL's error: "libpq is
incorrectly linked to backend functions"

(cherry picked from commit ceda5994991c17c57331d84d914478d123a2aa18)
Cloudberry linked libpq.a statically into postgres_fdw and added a copy
of src/common/link-canary.c compiled with -DFRONTEND, only to make
libpq's "libpq is incorrectly linked to backend functions" check pass.
That check was right: libpq was calling the backend's src/common
functions.  With a SCRAM-authenticated remote server, pg_saslprep()
returned a palloc'd password that libpq later passed to free() in
scram_free() when the connection was closed.  The memory context it came
from (typically ExecutorState) was gone by then, so any disconnect
crashed the backend: postgres_fdw_disconnect(), ALTER SERVER/USER
MAPPING, or a cache reset processed at the start of the next statement.

The previous commit links libpq.so dynamically, as Greenplum and upstream
do; that is safe since 32cde27 hides the backend's copy of the libpq
symbols.  libpq.so uses its own frontend src/common and its own link
canary, so the copy in postgres_fdw.so is no longer used.  Remove it.

Fixes apache#2490

(cherry picked from commit df22e82)
@yjhjstz
yjhjstz force-pushed the fix/postgres-fdw-dynamic-libpq-rel2 branch from 731aaa0 to 6f55487 Compare December 4, 2026 03:38
@tuhaihe
tuhaihe merged commit f8c8007 into apache:REL_2_STABLE Dec 4, 2026
137 of 138 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cherry-pick cherry-pick upstream commts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants