Skip to content

Commit

Permalink
Fix transaction begin failure handling (#751)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
JelteF committed Jul 2, 2021
1 parent f665321 commit 18229bc
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/bin/pg_autoctl/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ log_connection_error(PGconn *connection, int logLevel)
/*
* pgsql_open_connection opens a PostgreSQL connection, given a PGSQL client
* instance. If a connection is already open in the client (it's not NULL),
* then pgsql_open_connection reuses it and returns it immediately.
* then this errors, unless we are inside a transaction opened by pgsql_begin.
*/
static PGconn *
pgsql_open_connection(PGSQL *pgsql)
Expand Down Expand Up @@ -873,17 +873,21 @@ pgAutoCtlDebugNoticeProcessor(void *arg, const char *message)
bool
pgsql_begin(PGSQL *pgsql)
{
PGconn *connection;

pgsql->connectionStatementType = PGSQL_CONNECTION_MULTI_STATEMENT;
connection = pgsql_open_connection(pgsql);
if (connection == NULL)
if (!pgsql_execute(pgsql, "BEGIN"))
{
/* error message was logged in pgsql_open_connection */
/*
* connection is closed by pgsql_execute already, so no need for
* further cleanup.
*/
return false;
}

return pgsql_execute(pgsql, "BEGIN");
/*
* Indicate that we're in a transaction so that we can detect bugs easily.
*/
pgsql->connectionStatementType = PGSQL_CONNECTION_MULTI_STATEMENT;

return true;
}


Expand Down

0 comments on commit 18229bc

Please sign in to comment.