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

Commit555276f

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 parent4af8cf3 commit555276f

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
@@ -10403,6 +10403,13 @@ SELECT * FROM result_tbl ORDER BY a;
1040310403
(2 rows)
1040410404

1040510405
DELETE FROM result_tbl;
10406+
-- Test error handling, if accessing one of the foreign partitions errors out
10407+
CREATE FOREIGN TABLE async_p_broken PARTITION OF async_pt FOR VALUES FROM (10000) TO (10001)
10408+
SERVER loopback OPTIONS (table_name 'non_existent_table');
10409+
SELECT * FROM async_pt;
10410+
ERROR: relation "public.non_existent_table" does not exist
10411+
CONTEXT: remote SQL command: SELECT a, b, c FROM public.non_existent_table
10412+
DROP FOREIGN TABLE async_p_broken;
1040610413
-- Check case where multiple partitions use the same connection
1040710414
CREATE TABLE base_tbl3 (a int, b int, c text);
1040810415
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
@@ -3341,6 +3341,12 @@ INSERT INTO result_tbl SELECT * FROM async_pt WHERE b === 505;
33413341
SELECT*FROM result_tblORDER BY a;
33423342
DELETEFROM result_tbl;
33433343

3344+
-- Test error handling, if accessing one of the foreign partitions errors out
3345+
CREATE FOREIGN TABLE async_p_broken PARTITION OF async_pt FORVALUESFROM (10000) TO (10001)
3346+
SERVER loopback OPTIONS (table_name'non_existent_table');
3347+
SELECT*FROM async_pt;
3348+
DROP FOREIGN TABLE async_p_broken;
3349+
33443350
-- Check case where multiple partitions use the same connection
33453351
CREATETABLEbase_tbl3 (aint, bint, ctext);
33463352
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
@@ -1029,43 +1029,51 @@ ExecAppendAsyncEventWait(AppendState *node)
10291029
/* We should never be called when there are no valid async subplans. */
10301030
Assert(node->as_nasyncremain>0);
10311031

1032+
Assert(node->as_eventset==NULL);
10321033
node->as_eventset=CreateWaitEventSet(CurrentMemoryContext,nevents);
1033-
AddWaitEventToSet(node->as_eventset,WL_EXIT_ON_PM_DEATH,PGINVALID_SOCKET,
1034-
NULL,NULL);
1035-
1036-
/* Give each waiting subplan a chance to add an event. */
1037-
i=-1;
1038-
while ((i=bms_next_member(node->as_asyncplans,i)) >=0)
1034+
PG_TRY();
10391035
{
1040-
AsyncRequest*areq=node->as_asyncrequests[i];
1036+
AddWaitEventToSet(node->as_eventset,WL_EXIT_ON_PM_DEATH,PGINVALID_SOCKET,
1037+
NULL,NULL);
10411038

1042-
if (areq->callback_pending)
1043-
ExecAsyncConfigureWait(areq);
1044-
}
1039+
/* Give each waiting subplan a chance to add an event. */
1040+
i=-1;
1041+
while ((i=bms_next_member(node->as_asyncplans,i)) >=0)
1042+
{
1043+
AsyncRequest*areq=node->as_asyncrequests[i];
10451044

1046-
/*
1047-
* No need for further processing if there are no configured events other
1048-
* than the postmaster death event.
1049-
*/
1050-
if (GetNumRegisteredWaitEvents(node->as_eventset)==1)
1045+
if (areq->callback_pending)
1046+
ExecAsyncConfigureWait(areq);
1047+
}
1048+
1049+
/*
1050+
* No need for further processing if there are no configured events
1051+
* other than the postmaster death event.
1052+
*/
1053+
if (GetNumRegisteredWaitEvents(node->as_eventset)==1)
1054+
{
1055+
FreeWaitEventSet(node->as_eventset);
1056+
node->as_eventset=NULL;
1057+
return;
1058+
}
1059+
1060+
/* Return at most EVENT_BUFFER_SIZE events in one call. */
1061+
if (nevents>EVENT_BUFFER_SIZE)
1062+
nevents=EVENT_BUFFER_SIZE;
1063+
1064+
/*
1065+
* If the timeout is -1, wait until at least one event occurs. If the
1066+
* timeout is 0, poll for events, but do not wait at all.
1067+
*/
1068+
noccurred=WaitEventSetWait(node->as_eventset,timeout,occurred_event,
1069+
nevents,WAIT_EVENT_APPEND_READY);
1070+
}
1071+
PG_FINALLY();
10511072
{
10521073
FreeWaitEventSet(node->as_eventset);
10531074
node->as_eventset=NULL;
1054-
return;
10551075
}
1056-
1057-
/* We wait on at most EVENT_BUFFER_SIZE events. */
1058-
if (nevents>EVENT_BUFFER_SIZE)
1059-
nevents=EVENT_BUFFER_SIZE;
1060-
1061-
/*
1062-
* If the timeout is -1, wait until at least one event occurs. If the
1063-
* timeout is 0, poll for events, but do not wait at all.
1064-
*/
1065-
noccurred=WaitEventSetWait(node->as_eventset,timeout,occurred_event,
1066-
nevents,WAIT_EVENT_APPEND_READY);
1067-
FreeWaitEventSet(node->as_eventset);
1068-
node->as_eventset=NULL;
1076+
PG_END_TRY();
10691077
if (noccurred==0)
10701078
return;
10711079

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp