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

Commit481d7d1

Browse files
committed
Fix resource leak when a FDW's ForeignAsyncRequest function fails
If an error is thrown after calling CreateWaitEventSet(), the memoryof a WaitEventSet is free'd as it's allocated in the short-livedmemory context, but the file descriptor (on epoll- or kqueue-basedsystems) or handles (on Windows) that it contains are leaked.Use PG_TRY-FINALLY to ensure it gets freed. (On master, I will apply abetter fix, using ResourceOwners to track the WaitEventSet, but that'snot backpatchable.)The added test doesn't check for leaking resources, so it passed evenbefore this commit. But at least it covers the code path.In the passing, fix misleading comment on what the 'nevents' argumentto WaitEventSetWait means.Report by Alexander Lakhin, analysis and suggestion for the fix by TomLane. Fixes bug #17828. Backpatch to v14 where async execution wasintroduced, but master gets a different fix.Discussion:https://www.postgresql.org/message-id/17828-122da8cba23236be@postgresql.orgDiscussion:https://www.postgresql.org/message-id/472235.1678387869@sss.pgh.pa.us
1 parent57aae65 commit481d7d1

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

‎contrib/postgres_fdw/expected/postgres_fdw.out

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10576,6 +10576,13 @@ SELECT * FROM result_tbl ORDER BY a;
1057610576
(2 rows)
1057710577

1057810578
DELETE FROM result_tbl;
10579+
-- Test error handling, if accessing one of the foreign partitions errors out
10580+
CREATE FOREIGN TABLE async_p_broken PARTITION OF async_pt FOR VALUES FROM (10000) TO (10001)
10581+
SERVER loopback OPTIONS (table_name 'non_existent_table');
10582+
SELECT * FROM async_pt;
10583+
ERROR: relation "public.non_existent_table" does not exist
10584+
CONTEXT: remote SQL command: SELECT a, b, c FROM public.non_existent_table
10585+
DROP FOREIGN TABLE async_p_broken;
1057910586
-- Check case where multiple partitions use the same connection
1058010587
CREATE TABLE base_tbl3 (a int, b int, c text);
1058110588
CREATE FOREIGN TABLE async_p3 PARTITION OF async_pt FOR VALUES FROM (3000) TO (4000)

‎contrib/postgres_fdw/sql/postgres_fdw.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3404,6 +3404,12 @@ INSERT INTO result_tbl SELECT a, b, 'AAA' || c FROM async_pt WHERE b === 505;
34043404
SELECT*FROM result_tblORDER BY a;
34053405
DELETEFROM result_tbl;
34063406

3407+
-- Test error handling, if accessing one of the foreign partitions errors out
3408+
CREATE FOREIGN TABLE async_p_broken PARTITION OF async_pt FORVALUESFROM (10000) TO (10001)
3409+
SERVER loopback OPTIONS (table_name'non_existent_table');
3410+
SELECT*FROM async_pt;
3411+
DROP FOREIGN TABLE async_p_broken;
3412+
34073413
-- Check case where multiple partitions use the same connection
34083414
CREATETABLEbase_tbl3 (aint, bint, ctext);
34093415
CREATE FOREIGN TABLE async_p3 PARTITION OF async_pt FORVALUESFROM (3000) TO (4000)

‎src/backend/executor/nodeAppend.c

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,43 +1016,51 @@ ExecAppendAsyncEventWait(AppendState *node)
10161016
/* We should never be called when there are no valid async subplans. */
10171017
Assert(node->as_nasyncremain>0);
10181018

1019+
Assert(node->as_eventset==NULL);
10191020
node->as_eventset=CreateWaitEventSet(CurrentMemoryContext,nevents);
1020-
AddWaitEventToSet(node->as_eventset,WL_EXIT_ON_PM_DEATH,PGINVALID_SOCKET,
1021-
NULL,NULL);
1022-
1023-
/* Give each waiting subplan a chance to add an event. */
1024-
i=-1;
1025-
while ((i=bms_next_member(node->as_asyncplans,i)) >=0)
1021+
PG_TRY();
10261022
{
1027-
AsyncRequest*areq=node->as_asyncrequests[i];
1023+
AddWaitEventToSet(node->as_eventset,WL_EXIT_ON_PM_DEATH,PGINVALID_SOCKET,
1024+
NULL,NULL);
10281025

1029-
if (areq->callback_pending)
1030-
ExecAsyncConfigureWait(areq);
1031-
}
1026+
/* Give each waiting subplan a chance to add an event. */
1027+
i=-1;
1028+
while ((i=bms_next_member(node->as_asyncplans,i)) >=0)
1029+
{
1030+
AsyncRequest*areq=node->as_asyncrequests[i];
10321031

1033-
/*
1034-
* No need for further processing if there are no configured events other
1035-
* than the postmaster death event.
1036-
*/
1037-
if (GetNumRegisteredWaitEvents(node->as_eventset)==1)
1032+
if (areq->callback_pending)
1033+
ExecAsyncConfigureWait(areq);
1034+
}
1035+
1036+
/*
1037+
* No need for further processing if there are no configured events
1038+
* other than the postmaster death event.
1039+
*/
1040+
if (GetNumRegisteredWaitEvents(node->as_eventset)==1)
1041+
{
1042+
FreeWaitEventSet(node->as_eventset);
1043+
node->as_eventset=NULL;
1044+
return;
1045+
}
1046+
1047+
/* Return at most EVENT_BUFFER_SIZE events in one call. */
1048+
if (nevents>EVENT_BUFFER_SIZE)
1049+
nevents=EVENT_BUFFER_SIZE;
1050+
1051+
/*
1052+
* If the timeout is -1, wait until at least one event occurs. If the
1053+
* timeout is 0, poll for events, but do not wait at all.
1054+
*/
1055+
noccurred=WaitEventSetWait(node->as_eventset,timeout,occurred_event,
1056+
nevents,WAIT_EVENT_APPEND_READY);
1057+
}
1058+
PG_FINALLY();
10381059
{
10391060
FreeWaitEventSet(node->as_eventset);
10401061
node->as_eventset=NULL;
1041-
return;
10421062
}
1043-
1044-
/* We wait on at most EVENT_BUFFER_SIZE events. */
1045-
if (nevents>EVENT_BUFFER_SIZE)
1046-
nevents=EVENT_BUFFER_SIZE;
1047-
1048-
/*
1049-
* If the timeout is -1, wait until at least one event occurs. If the
1050-
* timeout is 0, poll for events, but do not wait at all.
1051-
*/
1052-
noccurred=WaitEventSetWait(node->as_eventset,timeout,occurred_event,
1053-
nevents,WAIT_EVENT_APPEND_READY);
1054-
FreeWaitEventSet(node->as_eventset);
1055-
node->as_eventset=NULL;
1063+
PG_END_TRY();
10561064
if (noccurred==0)
10571065
return;
10581066

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp