postgres_fdw: Minor cleanup for pgfdw_abort_cleanup().

Commit 85c696112 introduced this function to deduplicate code in the
transaction callback functions, but the SQL command passed as an
argument to it was useless when it returned before aborting a remote
transaction using the command.  Modify pgfdw_abort_cleanup() so that it
constructs the command when/if necessary, as before, removing the
argument from it.  Also update comments in pgfdw_abort_cleanup() and one
of the calling functions.

Etsuro Fujita, reviewed by David Zhang.

Discussion: https://postgr.es/m/CAPmGK158hrd%3DZfXmgkmNFHivgh18e4oE2Gz151C2Q4OBDjZ08A%40mail.gmail.com
This commit is contained in:
Etsuro Fujita 2022-03-25 15:30:00 +09:00
parent ad8759bea0
commit 5656683503
1 changed files with 15 additions and 14 deletions

View File

@ -110,8 +110,7 @@ static bool pgfdw_exec_cleanup_query(PGconn *conn, const char *query,
bool ignore_errors); bool ignore_errors);
static bool pgfdw_get_cleanup_result(PGconn *conn, TimestampTz endtime, static bool pgfdw_get_cleanup_result(PGconn *conn, TimestampTz endtime,
PGresult **result, bool *timed_out); PGresult **result, bool *timed_out);
static void pgfdw_abort_cleanup(ConnCacheEntry *entry, const char *sql, static void pgfdw_abort_cleanup(ConnCacheEntry *entry, bool toplevel);
bool toplevel);
static void pgfdw_finish_pre_commit_cleanup(List *pending_entries); static void pgfdw_finish_pre_commit_cleanup(List *pending_entries);
static void pgfdw_finish_pre_subcommit_cleanup(List *pending_entries, static void pgfdw_finish_pre_subcommit_cleanup(List *pending_entries,
int curlevel); int curlevel);
@ -1015,8 +1014,8 @@ pgfdw_xact_callback(XactEvent event, void *arg)
break; break;
case XACT_EVENT_PARALLEL_ABORT: case XACT_EVENT_PARALLEL_ABORT:
case XACT_EVENT_ABORT: case XACT_EVENT_ABORT:
/* Rollback all remote transactions during abort */
pgfdw_abort_cleanup(entry, "ABORT TRANSACTION", true); pgfdw_abort_cleanup(entry, true);
break; break;
} }
} }
@ -1109,10 +1108,7 @@ pgfdw_subxact_callback(SubXactEvent event, SubTransactionId mySubid,
else else
{ {
/* Rollback all remote subtransactions during abort */ /* Rollback all remote subtransactions during abort */
snprintf(sql, sizeof(sql), pgfdw_abort_cleanup(entry, false);
"ROLLBACK TO SAVEPOINT s%d; RELEASE SAVEPOINT s%d",
curlevel, curlevel);
pgfdw_abort_cleanup(entry, sql, false);
} }
/* OK, we're outta that level of subtransaction */ /* OK, we're outta that level of subtransaction */
@ -1465,10 +1461,7 @@ exit: ;
} }
/* /*
* Abort remote transaction. * Abort remote transaction or subtransaction.
*
* The statement specified in "sql" is sent to the remote server,
* in order to rollback the remote transaction.
* *
* "toplevel" should be set to true if toplevel (main) transaction is * "toplevel" should be set to true if toplevel (main) transaction is
* rollbacked, false otherwise. * rollbacked, false otherwise.
@ -1476,8 +1469,10 @@ exit: ;
* Set entry->changing_xact_state to false on success, true on failure. * Set entry->changing_xact_state to false on success, true on failure.
*/ */
static void static void
pgfdw_abort_cleanup(ConnCacheEntry *entry, const char *sql, bool toplevel) pgfdw_abort_cleanup(ConnCacheEntry *entry, bool toplevel)
{ {
char sql[100];
/* /*
* Don't try to clean up the connection if we're already in error * Don't try to clean up the connection if we're already in error
* recursion trouble. * recursion trouble.
@ -1509,8 +1504,14 @@ pgfdw_abort_cleanup(ConnCacheEntry *entry, const char *sql, bool toplevel)
!pgfdw_cancel_query(entry->conn)) !pgfdw_cancel_query(entry->conn))
return; /* Unable to cancel running query */ return; /* Unable to cancel running query */
if (toplevel)
snprintf(sql, sizeof(sql), "ABORT TRANSACTION");
else
snprintf(sql, sizeof(sql),
"ROLLBACK TO SAVEPOINT s%d; RELEASE SAVEPOINT s%d",
entry->xact_depth, entry->xact_depth);
if (!pgfdw_exec_cleanup_query(entry->conn, sql, false)) if (!pgfdw_exec_cleanup_query(entry->conn, sql, false))
return; /* Unable to abort remote transaction */ return; /* Unable to abort remote (sub)transaction */
if (toplevel) if (toplevel)
{ {