From d41bf7711518b550fb5af9860ec4e2c3236d0b7d Mon Sep 17 00:00:00 2001 From: Fujii Masao Date: Tue, 24 Nov 2026 08:16:43 +0900 Subject: [PATCH v2 2/2] dblink: Discard accumulated rows after a remote error The documented behavior of dblink() with fail_on_error set to false is to report remote errors as NOTICEs and return no rows. However, it could return rows from an earlier statement in the command string or from the failing query itself. The cause was that dblink did not discard previously received rows when reporting a remote error as a NOTICE. Those rows remained available to be returned to the caller. Fix this by discarding any accumulated rows after a remote error is reported as a NOTICE. This ensures that, with fail_on_error set to false, dblink() returns no rows after a remote error, as documented, even if it received some before the error. Backpatch to all supported versions. Author: Fujii Masao Backpatch-through: 15 --- contrib/dblink/dblink.c | 9 ++++++++- contrib/dblink/expected/dblink.out | 18 ++++++++++++++++++ contrib/dblink/sql/dblink.sql | 7 +++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c index 207cab279ec..e74a07d43f5 100644 --- a/contrib/dblink/dblink.c +++ b/contrib/dblink/dblink.c @@ -1147,7 +1147,14 @@ materializeQueryResult(FunctionCallInfo fcinfo, { dblink_res_error(conn, conname, res, fail, "while executing query"); - /* if fail isn't set, we'll return an empty query result */ + + /* + * With fail = false, return no rows, even if we already stored + * some from the failed query or an earlier statement in the + * command string. + */ + if (sinfo.tuplestore) + tuplestore_clear(sinfo.tuplestore); } else if (PQresultStatus(res) == PGRES_COMMAND_OK) { diff --git a/contrib/dblink/expected/dblink.out b/contrib/dblink/expected/dblink.out index f4a980ed3e3..97489d50854 100644 --- a/contrib/dblink/expected/dblink.out +++ b/contrib/dblink/expected/dblink.out @@ -416,6 +416,24 @@ CONTEXT: while executing query on unnamed dblink connection ---+---+--- (0 rows) +-- Return no rows after a remote error, even if some rows were received. +SELECT * +FROM dblink('SELECT 42; SELECT 1 / 0', false) AS t(a int); +NOTICE: division by zero +CONTEXT: while executing query on unnamed dblink connection + a +--- +(0 rows) + +SELECT * +FROM dblink('SELECT 1 / (1 - i) FROM generate_series(0, 1) AS g(i)', false) + AS t(a int); +NOTICE: division by zero +CONTEXT: while executing query on unnamed dblink connection + a +--- +(0 rows) + -- change some data SELECT dblink_exec('UPDATE foo SET f3[2] = ''b99'' WHERE f1 = 11'); dblink_exec diff --git a/contrib/dblink/sql/dblink.sql b/contrib/dblink/sql/dblink.sql index 6a0f3dafe64..493ba09b667 100644 --- a/contrib/dblink/sql/dblink.sql +++ b/contrib/dblink/sql/dblink.sql @@ -257,6 +257,13 @@ FROM dblink('SELECT * FROM foo') AS t(a int, b text, c text[]); SELECT * FROM dblink('SELECT * FROM foobar',false) AS t(a int, b text, c text[]); +-- Return no rows after a remote error, even if some rows were received. +SELECT * +FROM dblink('SELECT 42; SELECT 1 / 0', false) AS t(a int); +SELECT * +FROM dblink('SELECT 1 / (1 - i) FROM generate_series(0, 1) AS g(i)', false) + AS t(a int); + -- change some data SELECT dblink_exec('UPDATE foo SET f3[2] = ''b99'' WHERE f1 = 11'); -- 2.56.1