Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb5c73ee

Browse files
committed
postgres_fdw: Fix connection leak.
In postgres_fdw, the cached connections to foreign servers will not beclosed until the local session exits if the user mappings or foreign serversthat those connections depend on are dropped. Those connections can beleaked.To fix that connection leak issue, after a change to a pg_foreign_serveror pg_user_mapping catalog entry, this commit makes postgres_fdw closethe connections depending on that entry immediately if currenttransaction has not used those connections yet. Otherwise, mark thoseconnections 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 RupireddyReviewed-by: Zhihong Yu, Zhijie Hou, Fujii MasaoDiscussion:https://postgr.es/m/CALj2ACVNcGH_6qLY-4_tXz8JLvA+4yeBThRfxMz7Oxbk1aHcpQ@mail.gmail.com
1 parent14649bf commitb5c73ee

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
@@ -833,12 +833,14 @@ pgfdw_xact_callback(XactEvent event, void *arg)
833833
entry->xact_depth=0;
834834

835835
/*
836-
* If the connection isn't in a good idle state, discard it to
837-
* recover. Next GetConnection will open a new connection.
836+
* If the connection isn't in a good idle state or it is marked as
837+
* invalid, then discard it to recover. Next GetConnection will open a
838+
* new connection.
838839
*/
839840
if (PQstatus(entry->conn)!=CONNECTION_OK||
840841
PQtransactionStatus(entry->conn)!=PQTRANS_IDLE||
841-
entry->changing_xact_state)
842+
entry->changing_xact_state||
843+
entry->invalidated)
842844
{
843845
elog(DEBUG3,"discarding connection %p",entry->conn);
844846
disconnect_pg_server(entry);
@@ -962,9 +964,12 @@ pgfdw_subxact_callback(SubXactEvent event, SubTransactionId mySubid,
962964
* Connection invalidation callback function
963965
*
964966
* After a change to a pg_foreign_server or pg_user_mapping catalog entry,
965-
* mark connections depending on that entry as needing to be remade.
966-
* We can't immediately destroy them, since they might be in the midst of
967-
* a transaction, but we'll remake them at the next opportunity.
967+
* close connections depending on that entry immediately if current transaction
968+
* has not used those connections yet. Otherwise, mark those connections as
969+
* invalid and then make pgfdw_xact_callback() close them at the end of current
970+
* transaction, since they cannot be closed in the midst of the transaction
971+
* using them. Closed connections will be remade at the next opportunity if
972+
* necessary.
968973
*
969974
* Although most cache invalidation callbacks blow away all the related stuff
970975
* regardless of the given hashvalue, connections are expensive enough that
@@ -995,7 +1000,21 @@ pgfdw_inval_callback(Datum arg, int cacheid, uint32 hashvalue)
9951000
entry->server_hashvalue==hashvalue)||
9961001
(cacheid==USERMAPPINGOID&&
9971002
entry->mapping_hashvalue==hashvalue))
998-
entry->invalidated= true;
1003+
{
1004+
/*
1005+
* Close the connection immediately if it's not used yet in this
1006+
* transaction. Otherwise mark it as invalid so that
1007+
* pgfdw_xact_callback() can close it at the end of this
1008+
* transaction.
1009+
*/
1010+
if (entry->xact_depth==0)
1011+
{
1012+
elog(DEBUG3,"discarding connection %p",entry->conn);
1013+
disconnect_pg_server(entry);
1014+
}
1015+
else
1016+
entry->invalidated= true;
1017+
}
9991018
}
10001019
}
10011020

‎contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3968,3 +3968,21 @@ QUERY: CREATE FOREIGN TABLE t5 (
39683968
OPTIONS (schema_name 'import_source', table_name 't5');
39693969
CONTEXT: importing foreign table "t5"
39703970
ROLLBACK;
3971+
-- ===================================================================
3972+
-- test connection invalidation cases
3973+
-- ===================================================================
3974+
-- This test case is for closing the connection in pgfdw_xact_callback
3975+
BEGIN;
3976+
-- Connection xact depth becomes 1 i.e. the connection is in midst of the xact.
3977+
SELECT 1 FROM ft1 LIMIT 1;
3978+
?column?
3979+
----------
3980+
1
3981+
(1 row)
3982+
3983+
-- Connection is not closed at the end of the alter statement in
3984+
-- pgfdw_inval_callback. That's because the connection is in midst of this
3985+
-- xact, it is just marked as invalid.
3986+
ALTER SERVER loopback OPTIONS (ADD use_remote_estimate 'off');
3987+
-- The invalid connection gets closed in pgfdw_xact_callback during commit.
3988+
COMMIT;

‎contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,3 +935,17 @@ DROP TYPE "Colors" CASCADE;
935935
IMPORT FOREIGN SCHEMA import_sourceLIMIT TO (t5)
936936
FROM SERVER loopback INTO import_dest5;-- ERROR
937937
ROLLBACK;
938+
939+
-- ===================================================================
940+
-- test connection invalidation cases
941+
-- ===================================================================
942+
-- This test case is for closing the connection in pgfdw_xact_callback
943+
BEGIN;
944+
-- Connection xact depth becomes 1 i.e. the connection is in midst of the xact.
945+
SELECT1FROM ft1LIMIT1;
946+
-- Connection is not closed at the end of the alter statement in
947+
-- pgfdw_inval_callback. That's because the connection is in midst of this
948+
-- xact, it is just marked as invalid.
949+
ALTER SERVER loopback OPTIONS (ADD use_remote_estimate'off');
950+
-- The invalid connection gets closed in pgfdw_xact_callback during commit.
951+
COMMIT;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp