postgres.git / summary / log / commit / refs

commit    7d3d2db7d5e533fa42a4202c7d308cbb0cbf9333
Author:   Etsuro Fujita <efujita@postgresql.org>
Date:     Mon Oct 05 07:15:01 2026 +0000
First released in: 19.0

    postgres_fdw: Fix corner cases in transaction mode propagation.
    
    This commit addresses the following issues in commit de28140de:
    
    * When the local transaction was DEFERRABLE, begin_remote_xact() started
      a remote transaction in the same mode without checking the remote
      server version.  Since DEFERRABLE is supported only in v9.1 and later,
      this would cause a syntax error on older remote servers.  Fix this by
      propagating the mode only to remote servers that support it.
    * pgfdw_xact_callback() committed remote transactions without
      synchronizing the read-only mode.  If the local transaction had become
      READ ONLY since the last remote operation, this would cause deferred
      triggers on remote servers to still run in READ WRITE mode.  Fix this
      by synchronizing the mode at commit time.
    * The documentation did not match the actual implementation.  Update the
      documentation to reflect the implementation.
    
    Reported-by: Fujii Masao <masao.fujii@gmail.com>
    Author: Etsuro Fujita <etsuro.fujita@gmail.com>
    Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
    Discussion: https://postgr.es/m/CAHGQGwE4UMaWGOZq1Z7YUTUSg-CgL4yAzO5YaGwTZe1-6Pjg2g%40mail.gmail.com
    Backpatch-through: 19


contrib/postgres_fdw/connection.c | 54 +++++++++++++++++++++++--- contrib/postgres_fdw/expected/postgres_fdw.out | 30 ++++++++++++-- contrib/postgres_fdw/sql/postgres_fdw.sql | 30 ++++++++++++-- doc/src/sgml/postgres-fdw.sgml | 15 +++---- 4 files changed, 106 insertions(+), 23 deletions(-) diff --git a/contrib/postgres_fdw/connection.c b/contrib/postgres_fdw/connection.c index 094eac2f343..4c7df57e4aa 100644 --- a/contrib/postgres_fdw/connection.c +++ b/contrib/postgres_fdw/connection.c @@ -112,6 +112,20 @@ static uint32 pgfdw_we_get_result = 0; */ #define RETRY_CANCEL_TIMEOUT 1000 +/* + * Macro for constructing commit command to be sent + * + * We synchronize the read/write mode before committing remote transactions + * so deferred triggers on remote servers can run in the right mode. + */ +#define CONSTRUCT_COMMIT_COMMAND(sql, entry) \ + do { \ + if ((read_only_level > 0) && !(entry)->xact_read_only) \ + strcpy((sql), "SET TRANSACTION READ ONLY; COMMIT TRANSACTION"); \ + else \ + strcpy((sql), "COMMIT TRANSACTION"); \ + } while(0) + /* Macro for constructing abort command to be sent */ #define CONSTRUCT_ABORT_COMMAND(sql, entry, toplevel) \ do { \ @@ -938,7 +952,7 @@ begin_remote_xact(ConnCacheEntry *entry) appendStringInfoString(&sql, "REPEATABLE READ"); if (ro) appendStringInfoString(&sql, " READ ONLY"); - if (XactDeferrable) + if (XactDeferrable && PQserverVersion(entry->conn) >= 90100) appendStringInfoString(&sql, " DEFERRABLE"); entry->changing_xact_state = true; do_sql_command(entry->conn, sql.data); @@ -968,7 +982,7 @@ begin_remote_xact(ConnCacheEntry *entry) if (entry->xact_depth == read_only_level) { entry->changing_xact_state = true; - do_sql_command(entry->conn, "SET transaction_read_only = on"); + do_sql_command(entry->conn, "SET TRANSACTION READ ONLY"); entry->xact_read_only = true; entry->changing_xact_state = false; } @@ -1001,7 +1015,7 @@ begin_remote_xact(ConnCacheEntry *entry) initStringInfo(&sql); appendStringInfo(&sql, "SAVEPOINT s%d", entry->xact_depth + 1); if (ro) - appendStringInfoString(&sql, "; SET transaction_read_only = on"); + appendStringInfoString(&sql, "; SET TRANSACTION READ ONLY"); entry->changing_xact_state = true; do_sql_command(entry->conn, sql.data); entry->xact_depth++; @@ -1181,6 +1195,24 @@ pgfdw_xact_callback(XactEvent event, void *arg) if (!xact_got_connection) return; + /* + * If we are called for pre-commit cleanup, ensure read_only_level is set + * for later processing. Note that we need to do this because the local + * transaction may have become read-only since the last remote operation. + */ + if (event == XACT_EVENT_PARALLEL_PRE_COMMIT || + event == XACT_EVENT_PRE_COMMIT) + { + if (XactReadOnly) + { + if (read_only_level == 0) + read_only_level = 1; + Assert(read_only_level == 1); + } + else + Assert(read_only_level == 0); + } + /* * Scan all connection cache entries to find open remote transactions, and * close them. @@ -1197,6 +1229,8 @@ pgfdw_xact_callback(XactEvent event, void *arg) /* If it has an open remote transaction, try to close it */ if (entry->xact_depth > 0) { + char sql[100]; + elog(DEBUG3, "closing remote transaction on connection %p", entry->conn); @@ -1212,14 +1246,17 @@ pgfdw_xact_callback(XactEvent event, void *arg) pgfdw_reject_incomplete_xact_state_change(entry); /* Commit all remote transactions during pre-commit */ + CONSTRUCT_COMMIT_COMMAND(sql, entry); entry->changing_xact_state = true; if (entry->parallel_commit) { - do_sql_command_begin(entry->conn, "COMMIT TRANSACTION"); + do_sql_command_begin(entry->conn, sql); pending_entries = lappend(pending_entries, entry); continue; } - do_sql_command(entry->conn, "COMMIT TRANSACTION"); + do_sql_command(entry->conn, sql); + if ((read_only_level > 0) && !entry->xact_read_only) + entry->xact_read_only = true; entry->changing_xact_state = false; /* @@ -2016,6 +2053,8 @@ pgfdw_finish_pre_commit_cleanup(List *pending_entries) */ foreach(lc, pending_entries) { + char sql[100]; + entry = (ConnCacheEntry *) lfirst(lc); Assert(entry->changing_xact_state); @@ -2024,7 +2063,10 @@ pgfdw_finish_pre_commit_cleanup(List *pending_entries) * We might already have received the result on the socket, so pass * consume_input=true to try to consume it first */ - do_sql_command_end(entry->conn, "COMMIT TRANSACTION", true); + CONSTRUCT_COMMIT_COMMAND(sql, entry); + do_sql_command_end(entry->conn, sql, true); + if ((read_only_level > 0) && !(entry)->xact_read_only) + entry->xact_read_only = true; entry->changing_xact_state = false; /* Do a DEALLOCATE ALL in parallel if needed */ diff --git a/contrib/postgres_fdw/expected/postgres_fdw.out b/contrib/postgres_fdw/expected/postgres_fdw.out index 3a17e026337..fb4ea2c5e97 100644 --- a/contrib/postgres_fdw/expected/postgres_fdw.out +++ b/contrib/postgres_fdw/expected/postgres_fdw.out @@ -12740,6 +12740,7 @@ DROP VIEW my_application_name; -- test read-only and/or deferrable transactions -- =================================================================== CREATE TABLE loct (f1 int, f2 text); +INSERT INTO loct VALUES (1, 'foo'), (2, 'bar'); CREATE FUNCTION locf() RETURNS SETOF loct LANGUAGE SQL AS 'UPDATE public.loct SET f2 = f2 || f2 RETURNING *'; CREATE VIEW locv AS SELECT t.* FROM locf() t; @@ -12747,7 +12748,6 @@ CREATE FOREIGN TABLE remt (f1 int, f2 text) SERVER loopback OPTIONS (table_name 'locv'); CREATE FOREIGN TABLE remt2 (f1 int, f2 text) SERVER loopback2 OPTIONS (table_name 'locv'); -INSERT INTO loct VALUES (1, 'foo'), (2, 'bar'); START TRANSACTION READ ONLY; SAVEPOINT s; SELECT * FROM remt; -- should fail @@ -12836,9 +12836,32 @@ ERROR: cannot execute UPDATE in a read-only transaction CONTEXT: SQL function "locf" statement 1 remote SQL command: SELECT f1, f2 FROM public.locv ROLLBACK; +-- Clean up DROP FOREIGN TABLE remt; +DROP FOREIGN TABLE remt2; +DROP VIEW locv; +DROP FUNCTION locf(); CREATE FOREIGN TABLE remt (f1 int, f2 text) SERVER loopback OPTIONS (table_name 'loct'); +CREATE FUNCTION defer_trig_func() RETURNS TRIGGER LANGUAGE plpgsql AS $$ +BEGIN + IF NEW.f2 IS NOT NULL THEN + UPDATE public.loct SET f2 = f2 || f2 WHERE f1 = NEW.f1; + END IF; + RETURN NULL; +END; +$$; +CREATE CONSTRAINT TRIGGER defer_trig AFTER INSERT ON loct + DEFERRABLE INITIALLY DEFERRED + FOR EACH ROW EXECUTE PROCEDURE defer_trig_func(); +START TRANSACTION; +INSERT INTO remt VALUES (3, 'baz'); +SET TRANSACTION READ ONLY; +COMMIT; +ERROR: cannot execute UPDATE in a read-only transaction +CONTEXT: SQL statement "UPDATE public.loct SET f2 = f2 || f2 WHERE f1 = NEW.f1" +PL/pgSQL function public.defer_trig_func() line 4 at SQL statement +remote SQL command: SET TRANSACTION READ ONLY; COMMIT TRANSACTION START TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY; SELECT * FROM remt; f1 | f2 @@ -12868,9 +12891,8 @@ SELECT * FROM remt; COMMIT; -- Clean up DROP FOREIGN TABLE remt; -DROP FOREIGN TABLE remt2; -DROP VIEW locv; -DROP FUNCTION locf(); +DROP TRIGGER defer_trig ON loct; +DROP FUNCTION defer_trig_func; DROP TABLE loct; -- =================================================================== -- test parallel commit and parallel abort diff --git a/contrib/postgres_fdw/sql/postgres_fdw.sql b/contrib/postgres_fdw/sql/postgres_fdw.sql index 4b19c50542f..d4280c569b3 100644 --- a/contrib/postgres_fdw/sql/postgres_fdw.sql +++ b/contrib/postgres_fdw/sql/postgres_fdw.sql @@ -4377,6 +4377,8 @@ DROP VIEW my_application_name; -- test read-only and/or deferrable transactions -- =================================================================== CREATE TABLE loct (f1 int, f2 text); +INSERT INTO loct VALUES (1, 'foo'), (2, 'bar'); + CREATE FUNCTION locf() RETURNS SETOF loct LANGUAGE SQL AS 'UPDATE public.loct SET f2 = f2 || f2 RETURNING *'; CREATE VIEW locv AS SELECT t.* FROM locf() t; @@ -4384,7 +4386,6 @@ CREATE FOREIGN TABLE remt (f1 int, f2 text) SERVER loopback OPTIONS (table_name 'locv'); CREATE FOREIGN TABLE remt2 (f1 int, f2 text) SERVER loopback2 OPTIONS (table_name 'locv'); -INSERT INTO loct VALUES (1, 'foo'), (2, 'bar'); START TRANSACTION READ ONLY; SAVEPOINT s; @@ -4430,10 +4431,32 @@ SET transaction_read_only = on; SELECT * FROM remt2; -- should fail ROLLBACK; +-- Clean up DROP FOREIGN TABLE remt; +DROP FOREIGN TABLE remt2; +DROP VIEW locv; +DROP FUNCTION locf(); + CREATE FOREIGN TABLE remt (f1 int, f2 text) SERVER loopback OPTIONS (table_name 'loct'); +CREATE FUNCTION defer_trig_func() RETURNS TRIGGER LANGUAGE plpgsql AS $$ +BEGIN + IF NEW.f2 IS NOT NULL THEN + UPDATE public.loct SET f2 = f2 || f2 WHERE f1 = NEW.f1; + END IF; + RETURN NULL; +END; +$$; +CREATE CONSTRAINT TRIGGER defer_trig AFTER INSERT ON loct + DEFERRABLE INITIALLY DEFERRED + FOR EACH ROW EXECUTE PROCEDURE defer_trig_func(); + +START TRANSACTION; +INSERT INTO remt VALUES (3, 'baz'); +SET TRANSACTION READ ONLY; +COMMIT; + START TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY; SELECT * FROM remt; COMMIT; @@ -4448,9 +4471,8 @@ COMMIT; -- Clean up DROP FOREIGN TABLE remt; -DROP FOREIGN TABLE remt2; -DROP VIEW locv; -DROP FUNCTION locf(); +DROP TRIGGER defer_trig ON loct; +DROP FUNCTION defer_trig_func; DROP TABLE loct; -- =================================================================== diff --git a/doc/src/sgml/postgres-fdw.sgml b/doc/src/sgml/postgres-fdw.sgml index a71acc9b610..33d55f87a24 100644 --- a/doc/src/sgml/postgres-fdw.sgml +++ b/doc/src/sgml/postgres-fdw.sgml @@ -1148,20 +1148,17 @@ CREATE SUBSCRIPTION my_subscription SERVER subscription_server PUBLICATION testp </para> <para> - The remote transaction is opened in the same read/write mode as the local - transaction: if the local transaction is <literal>READ ONLY</literal>, - the remote transaction is opened in <literal>READ ONLY</literal> mode, - otherwise it is opened in <literal>READ WRITE</literal> mode. - (This rule is also applied to remote and local subtransactions.) + Local <literal>READ ONLY</literal> transactions propagate their read-only + mode to remote sessions. + (This rule is also applied to local subtransactions.) Note that this does not prevent login triggers executed on the remote server from writing. </para> <para> - The remote transaction is also opened in the same deferrable mode as the - local transaction: if the local transaction is <literal>DEFERRABLE</literal>, - the remote transaction is opened in <literal>DEFERRABLE</literal> mode, - otherwise it is opened in <literal>NOT DEFERRABLE</literal> mode. + Also, local <literal>DEFERRABLE</literal> transactions propagate their + deferrable mode to remote sessions. + (This rule is only applied to remote servers 9.1 and newer.) </para> <para> [parent: 46875a4c7464]