Registry Mirror
Skip to content

Conversation

@yjhjstz

@yjhjstz yjhjstz commented Dec 3, 2026

Copy link
Copy Markdown
Member

Fixes #2490

What does this PR do?

Fixes a QD crash in postgres_fdw when the remote server uses SCRAM authentication. The backend crashes whenever a cached connection is closed.

Reproduce. On the remote server, give the user-mapping role a SCRAM password and a scram-sha-256 rule in pg_hba.conf. Then:

CREATE EXTENSION postgres_fdw;
CREATE SERVER remote_server FOREIGN DATA WRAPPER postgres_fdw
  OPTIONS (host '127.0.0.1', port '5432', dbname 'remotedb');
CREATE USER MAPPING FOR CURRENT_USER SERVER remote_server
  OPTIONS (user 'fdw_scram', password 'secret');
CREATE FOREIGN TABLE ft (a int) SERVER remote_server OPTIONS (table_name 't');

SELECT count(*) FROM ft;               -- caches a SCRAM-authenticated connection
SELECT postgres_fdw_disconnect_all();  -- server closed the connection unexpectedly

Any path that closes the cached connection crashes in the same way:

  • postgres_fdw_disconnect() / postgres_fdw_disconnect_all()
  • ALTER SERVER / ALTER USER MAPPING
  • a cache reset processed at the start of the next statement. This is the case in terminated by signal 11 Segmentation fault #2490: StartTransactionCommand -> InvalidateSystemCachesExtended -> pgfdw_inval_callback -> PQfinish -> free, reported by CREATE FOREIGN TABLE.

With md5 authentication the same steps work.

Root cause. postgres_fdw linked libpq.a statically, with -Bsymbolic. libpq.a does not contain the src/common functions that libpq calls, so they were resolved at run time against the backend's versions, which allocate with palloc().

During SCRAM authentication, scram_init() stores the result of pg_saslprep() in state->password. That result was a pstrdup() in the current memory context, ExecutorState of the first query on the connection (postgresBeginForeignScan -> GetConnection). The context is deleted when that query ends, but libpq keeps the pointer for the life of the connection. When the connection is closed, scram_free() calls free() on it, and glibc aborts with free(): invalid pointer / munmap_chunk(): invalid pointer, or the process gets SIGSEGV if the page has already been unmapped. All of this was confirmed with gdb on main and on 2.0.0.

The static link came from GPDB 667f0c3, when the backend exported its own copy of the libpq functions. After the PG12 merge, libpq's own check reported that link as wrong (libpq is incorrectly linked to backend functions). Cloudberry then silenced the check by building a copy of link-canary.c with -DFRONTEND into postgres_fdw.

Fix. Do what Greenplum and upstream PostgreSQL do.

  1. Cherry-pick Greenplum ceda5994991 ("Revert "Fix postgres_fdw's libpq issue (#10617)"", Adam Lee). It links libpq.so dynamically and drops the FRONTEND wrapper around libpq-fe.h. This is safe since 32cde27 (Greenplum #16234), which hides the backend's copy of the libpq symbols with a linker version script. libpq.so uses its own frontend src/common, so pg_saslprep() allocates with malloc(). The only conflict was the extra -Bsymbolic and a GPDB_13_MERGE_FIXME comment; both were resolved to the Greenplum side.
  2. Remove the link-canary.o workaround. It is Cloudberry-only, and with libpq.so it is no longer used.

dblink and gp_inject_fault already link libpq.so the same way.

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

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

  • Crash repro, before / after: all three paths crash before the fix and pass after it: postgres_fdw_disconnect_all(), a sinval cache reset processed while idle, and a cache reset processed in start_xact_command() of CREATE FOREIGN TABLE. The last one is the stack in terminated by signal 11 Segmentation fault #2490. md5 passes in all cases.
  • gdb: after the fix, pg_saslprep() and scram_free() run in libpq.so.5, and state->password is a live malloc() chunk when it is freed.
  • Build: clean build passes, and postgres_fdw.so now has NEEDED libpq.so.5.
  • Regression: gp_postgres_fdw and mpp_postgres_fdw produce the same regression.diffs with and without this change. Both fail before it as well, because their expected files are stale; they are not run in CI. gp2pg_* needs separate PostgreSQL servers on 5432/5555 and was not run.
  • No regression test added: the crash needs a SCRAM rule in pg_hba.conf, which pg_regress cannot set up. With the workaround removed, libpq's link canary again rejects any future mislinking of libpq against backend src/common. Every postgres_fdw connection would fail, so existing tests would catch it.

Impact

User-facing changes: postgres_fdw no longer crashes the backend when it closes a connection to a SCRAM-authenticated server.

Dependencies: postgres_fdw.so now depends on libpq.so.5 from the same installation. It is found through the rpath, as for dblink.

Checklist

Additional Context

Backport. 2.1.0 and 2.2.0 already hide the libpq symbols in libpostgres.so, so this should apply there; that has not been tested yet. 2.0.0 does not: its libpostgres.so exports the PQ* symbols, which take precedence over libpq.so. Measured on 2.0.0:

  • With this change alone, PQfinish() / pg_fe_scram_free() still come from libpostgres.so and still crash.
  • dblink with a SCRAM-authenticated connection also crashes on dblink_disconnect().

A 2.0.x fix would also need the libpostgres.so part of 32cde27.

adam8157 and others added 2 commits December 4, 2026 00:48
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
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.

terminated by signal 11 Segmentation fault

2 participants