Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e3ebcca

Browse files
committed
postgres_fdw: Fix connection leak.
In postgres_fdw, the cached connections to foreign servers will not be closed until the local session exits if the user mappings or foreign servers that those connections depend on are dropped. Those connections can be leaked. To fix that connection leak issue, after a change to a pg_foreign_server or pg_user_mapping catalog entry, this commit makes postgres_fdw close the connections depending on that entry immediately if current transaction has not used those connections yet. Otherwise, mark those connections as invalid and then close them at the end of current transaction, since they cannot be closed in the midst of the transaction using them. Closed connections will be remade at the next opportunity if necessary. Back-patch to all supported branches. Author: Bharath Rupireddy Reviewed-by: Zhihong Yu, Zhijie Hou, Fujii Masao Discussion: https://postgr.es/m/CALj2ACVNcGH_6qLY-4_tXz8JLvA+4yeBThRfxMz7Oxbk1aHcpQ@mail.gmail.com
1 parent 3187ef7 commit e3ebcca

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

contrib/postgres_fdw/connection.c

+26-7
Original file line numberDiff line numberDiff line change
@@ -940,12 +940,14 @@ pgfdw_xact_callback(XactEvent event, void *arg)
940940
entry->xact_depth = 0;
941941

942942
/*
943-
* If the connection isn't in a good idle state, discard it to
944-
* recover. Next GetConnection will open a new connection.
943+
* If the connection isn't in a good idle state or it is marked as
944+
* invalid, then discard it to recover. Next GetConnection will open a
945+
* new connection.
945946
*/
946947
if (PQstatus(entry->conn) != CONNECTION_OK ||
947948
PQtransactionStatus(entry->conn) != PQTRANS_IDLE ||
948-
entry->changing_xact_state)
949+
entry->changing_xact_state ||
950+
entry->invalidated)
949951
{
950952
elog(DEBUG3, "discarding connection %p", entry->conn);
951953
disconnect_pg_server(entry);
@@ -1069,9 +1071,12 @@ pgfdw_subxact_callback(SubXactEvent event, SubTransactionId mySubid,
10691071
* Connection invalidation callback function
10701072
*
10711073
* After a change to a pg_foreign_server or pg_user_mapping catalog entry,
1072-
* mark connections depending on that entry as needing to be remade.
1073-
* We can't immediately destroy them, since they might be in the midst of
1074-
* a transaction, but we'll remake them at the next opportunity.
1074+
* close connections depending on that entry immediately if current transaction
1075+
* has not used those connections yet. Otherwise, mark those connections as
1076+
* invalid and then make pgfdw_xact_callback() close them at the end of current
1077+
* transaction, since they cannot be closed in the midst of the transaction
1078+
* using them. Closed connections will be remade at the next opportunity if
1079+
* necessary.
10751080
*
10761081
* Although most cache invalidation callbacks blow away all the related stuff
10771082
* regardless of the given hashvalue, connections are expensive enough that
@@ -1102,7 +1107,21 @@ pgfdw_inval_callback(Datum arg, int cacheid, uint32 hashvalue)
11021107
entry->server_hashvalue == hashvalue) ||
11031108
(cacheid == USERMAPPINGOID &&
11041109
entry->mapping_hashvalue == hashvalue))
1105-
entry->invalidated = true;
1110+
{
1111+
/*
1112+
* Close the connection immediately if it's not used yet in this
1113+
* transaction. Otherwise mark it as invalid so that
1114+
* pgfdw_xact_callback() can close it at the end of this
1115+
* transaction.
1116+
*/
1117+
if (entry->xact_depth == 0)
1118+
{
1119+
elog(DEBUG3, "discarding connection %p", entry->conn);
1120+
disconnect_pg_server(entry);
1121+
}
1122+
else
1123+
entry->invalidated = true;
1124+
}
11061125
}
11071126
}
11081127

contrib/postgres_fdw/expected/postgres_fdw.out

+18
Original file line numberDiff line numberDiff line change
@@ -9035,3 +9035,21 @@ ERROR: 08006
90359035
COMMIT;
90369036
-- Clean up
90379037
DROP PROCEDURE terminate_backend_and_wait(text);
9038+
-- ===================================================================
9039+
-- test connection invalidation cases
9040+
-- ===================================================================
9041+
-- This test case is for closing the connection in pgfdw_xact_callback
9042+
BEGIN;
9043+
-- Connection xact depth becomes 1 i.e. the connection is in midst of the xact.
9044+
SELECT 1 FROM ft1 LIMIT 1;
9045+
?column?
9046+
----------
9047+
1
9048+
(1 row)
9049+
9050+
-- Connection is not closed at the end of the alter statement in
9051+
-- pgfdw_inval_callback. That's because the connection is in midst of this
9052+
-- xact, it is just marked as invalid.
9053+
ALTER SERVER loopback OPTIONS (ADD use_remote_estimate 'off');
9054+
-- The invalid connection gets closed in pgfdw_xact_callback during commit.
9055+
COMMIT;

contrib/postgres_fdw/sql/postgres_fdw.sql

+14
Original file line numberDiff line numberDiff line change
@@ -2697,3 +2697,17 @@ COMMIT;
26972697

26982698
-- Clean up
26992699
DROP PROCEDURE terminate_backend_and_wait(text);
2700+
2701+
-- ===================================================================
2702+
-- test connection invalidation cases
2703+
-- ===================================================================
2704+
-- This test case is for closing the connection in pgfdw_xact_callback
2705+
BEGIN;
2706+
-- Connection xact depth becomes 1 i.e. the connection is in midst of the xact.
2707+
SELECT 1 FROM ft1 LIMIT 1;
2708+
-- Connection is not closed at the end of the alter statement in
2709+
-- pgfdw_inval_callback. That's because the connection is in midst of this
2710+
-- xact, it is just marked as invalid.
2711+
ALTER SERVER loopback OPTIONS (ADD use_remote_estimate 'off');
2712+
-- The invalid connection gets closed in pgfdw_xact_callback during commit.
2713+
COMMIT;

0 commit comments

Comments
 (0)