Skip to content

Commit 294cdd7

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 e83e850 commit 294cdd7

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

contrib/postgres_fdw/connection.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -811,12 +811,14 @@ pgfdw_xact_callback(XactEvent event, void *arg)
811811
entry->xact_depth = 0;
812812

813813
/*
814-
* If the connection isn't in a good idle state, discard it to
815-
* recover. Next GetConnection will open a new connection.
814+
* If the connection isn't in a good idle state or it is marked as
815+
* invalid, then discard it to recover. Next GetConnection will open a
816+
* new connection.
816817
*/
817818
if (PQstatus(entry->conn) != CONNECTION_OK ||
818819
PQtransactionStatus(entry->conn) != PQTRANS_IDLE ||
819-
entry->changing_xact_state)
820+
entry->changing_xact_state ||
821+
entry->invalidated)
820822
{
821823
elog(DEBUG3, "discarding connection %p", entry->conn);
822824
disconnect_pg_server(entry);
@@ -940,9 +942,12 @@ pgfdw_subxact_callback(SubXactEvent event, SubTransactionId mySubid,
940942
* Connection invalidation callback function
941943
*
942944
* After a change to a pg_foreign_server or pg_user_mapping catalog entry,
943-
* mark connections depending on that entry as needing to be remade.
944-
* We can't immediately destroy them, since they might be in the midst of
945-
* a transaction, but we'll remake them at the next opportunity.
945+
* close connections depending on that entry immediately if current transaction
946+
* has not used those connections yet. Otherwise, mark those connections as
947+
* invalid and then make pgfdw_xact_callback() close them at the end of current
948+
* transaction, since they cannot be closed in the midst of the transaction
949+
* using them. Closed connections will be remade at the next opportunity if
950+
* necessary.
946951
*
947952
* Although most cache invalidation callbacks blow away all the related stuff
948953
* regardless of the given hashvalue, connections are expensive enough that
@@ -973,7 +978,21 @@ pgfdw_inval_callback(Datum arg, int cacheid, uint32 hashvalue)
973978
entry->server_hashvalue == hashvalue) ||
974979
(cacheid == USERMAPPINGOID &&
975980
entry->mapping_hashvalue == hashvalue))
976-
entry->invalidated = true;
981+
{
982+
/*
983+
* Close the connection immediately if it's not used yet in this
984+
* transaction. Otherwise mark it as invalid so that
985+
* pgfdw_xact_callback() can close it at the end of this
986+
* transaction.
987+
*/
988+
if (entry->xact_depth == 0)
989+
{
990+
elog(DEBUG3, "discarding connection %p", entry->conn);
991+
disconnect_pg_server(entry);
992+
}
993+
else
994+
entry->invalidated = true;
995+
}
977996
}
978997
}
979998

contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8832,3 +8832,21 @@ SELECT b, avg(a), max(a), count(*) FROM pagg_tab GROUP BY b HAVING sum(a) < 700
88328832

88338833
-- Clean-up
88348834
RESET enable_partitionwise_aggregate;
8835+
-- ===================================================================
8836+
-- test connection invalidation cases
8837+
-- ===================================================================
8838+
-- This test case is for closing the connection in pgfdw_xact_callback
8839+
BEGIN;
8840+
-- Connection xact depth becomes 1 i.e. the connection is in midst of the xact.
8841+
SELECT 1 FROM ft1 LIMIT 1;
8842+
?column?
8843+
----------
8844+
1
8845+
(1 row)
8846+
8847+
-- Connection is not closed at the end of the alter statement in
8848+
-- pgfdw_inval_callback. That's because the connection is in midst of this
8849+
-- xact, it is just marked as invalid.
8850+
ALTER SERVER loopback OPTIONS (ADD use_remote_estimate 'off');
8851+
-- The invalid connection gets closed in pgfdw_xact_callback during commit.
8852+
COMMIT;

contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2427,3 +2427,17 @@ SELECT b, avg(a), max(a), count(*) FROM pagg_tab GROUP BY b HAVING sum(a) < 700
24272427

24282428
-- Clean-up
24292429
RESET enable_partitionwise_aggregate;
2430+
2431+
-- ===================================================================
2432+
-- test connection invalidation cases
2433+
-- ===================================================================
2434+
-- This test case is for closing the connection in pgfdw_xact_callback
2435+
BEGIN;
2436+
-- Connection xact depth becomes 1 i.e. the connection is in midst of the xact.
2437+
SELECT 1 FROM ft1 LIMIT 1;
2438+
-- Connection is not closed at the end of the alter statement in
2439+
-- pgfdw_inval_callback. That's because the connection is in midst of this
2440+
-- xact, it is just marked as invalid.
2441+
ALTER SERVER loopback OPTIONS (ADD use_remote_estimate 'off');
2442+
-- The invalid connection gets closed in pgfdw_xact_callback during commit.
2443+
COMMIT;

0 commit comments

Comments
 (0)