Skip to content

Commit 18229bc

Browse files
authored
Fix transaction begin failure handling (#751)
When opening a transaction using `pgsql_begin` we would set the connection type to multi statement mode. However, if we failed to open a connection, we would not reset it to single statement mode. This changes it such that we only set the connection type to multi statement mode once we have successfully opened a transaction. See #746
1 parent f665321 commit 18229bc

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/bin/pg_autoctl/pgsql.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ log_connection_error(PGconn *connection, int logLevel)
478478
/*
479479
* pgsql_open_connection opens a PostgreSQL connection, given a PGSQL client
480480
* instance. If a connection is already open in the client (it's not NULL),
481-
* then pgsql_open_connection reuses it and returns it immediately.
481+
* then this errors, unless we are inside a transaction opened by pgsql_begin.
482482
*/
483483
static PGconn *
484484
pgsql_open_connection(PGSQL *pgsql)
@@ -873,17 +873,21 @@ pgAutoCtlDebugNoticeProcessor(void *arg, const char *message)
873873
bool
874874
pgsql_begin(PGSQL *pgsql)
875875
{
876-
PGconn *connection;
877-
878-
pgsql->connectionStatementType = PGSQL_CONNECTION_MULTI_STATEMENT;
879-
connection = pgsql_open_connection(pgsql);
880-
if (connection == NULL)
876+
if (!pgsql_execute(pgsql, "BEGIN"))
881877
{
882-
/* error message was logged in pgsql_open_connection */
878+
/*
879+
* connection is closed by pgsql_execute already, so no need for
880+
* further cleanup.
881+
*/
883882
return false;
884883
}
885884

886-
return pgsql_execute(pgsql, "BEGIN");
885+
/*
886+
* Indicate that we're in a transaction so that we can detect bugs easily.
887+
*/
888+
pgsql->connectionStatementType = PGSQL_CONNECTION_MULTI_STATEMENT;
889+
890+
return true;
887891
}
888892

889893

0 commit comments

Comments
 (0)