Content-Length: 424159 | pFad | http://github.com/postgres/postgres/commit/9339c85afc913752884858b28cca99b8f42a389c

0F Fix memory leakage in postgres_fdw's DirectModify code path. · postgres/postgres@9339c85 · GitHub
Skip to content

Commit 9339c85

Browse files
committed
Fix memory leakage in postgres_fdw's DirectModify code path.
postgres_fdw tries to use PG_TRY blocks to ensure that it will eventually free the PGresult created by the remote modify command. However, it's fundamentally impossible for this scheme to work reliably when there's RETURNING data, because the query could fail in between invocations of postgres_fdw's DirectModify methods. There is at least one instance of exactly this situation in the regression tests, and the ensuing session-lifespan leak is visible under Valgrind. We can improve matters by using a memory context reset callback attached to the ExecutorState context. That ensures that the PGresult will be freed when the ExecutorState context is torn down, even if control never reaches postgresEndDirectModify. I have little faith that there aren't other potential PGresult leakages in the backend modules that use libpq. So I think it'd be a good idea to apply this concept universally by creating infrastructure that attaches a reset callback to every PGresult generated in the backend. However, that seems too invasive for v18 at this point, let alone the back branches. So for the moment, apply this narrow fix that just makes DirectModify safe. I have a patch in the queue for the more general idea, but it will have to wait for v19. Author: Tom Lane <tgl@sss.pgh.pa.us> Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com> Discussion: https://postgr.es/m/2976982.1748049023@sss.pgh.pa.us Backpatch-through: 13
1 parent 8b0aa7a commit 9339c85

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ typedef struct PgFdwDirectModifyState
239239
PGresult *result; /* result for query */
240240
int num_tuples; /* # of result tuples */
241241
int next_tuple; /* index of next one to return */
242+
MemoryContextCallback result_cb; /* ensures result will get freed */
242243
Relation resultRel; /* relcache entry for the target relation */
243244
AttrNumber *attnoMap; /* array of attnums of input user columns */
244245
AttrNumber ctidAttno; /* attnum of input ctid column */
@@ -2662,6 +2663,17 @@ postgresBeginDirectModify(ForeignScanState *node, int eflags)
26622663
dmstate = (PgFdwDirectModifyState *) palloc0(sizeof(PgFdwDirectModifyState));
26632664
node->fdw_state = (void *) dmstate;
26642665

2666+
/*
2667+
* We use a memory context callback to ensure that the dmstate's PGresult
2668+
* (if any) will be released, even if the query fails somewhere that's
2669+
* outside our control. The callback is always armed for the duration of
2670+
* the query; this relies on PQclear(NULL) being a no-op.
2671+
*/
2672+
dmstate->result_cb.func = (MemoryContextCallbackFunction) PQclear;
2673+
dmstate->result_cb.arg = NULL;
2674+
MemoryContextRegisterResetCallback(CurrentMemoryContext,
2675+
&dmstate->result_cb);
2676+
26652677
/*
26662678
* Identify which user to do the remote access as. This should match what
26672679
* ExecCheckPermissions() does.
@@ -2809,7 +2821,13 @@ postgresEndDirectModify(ForeignScanState *node)
28092821
return;
28102822

28112823
/* Release PGresult */
2812-
PQclear(dmstate->result);
2824+
if (dmstate->result)
2825+
{
2826+
PQclear(dmstate->result);
2827+
dmstate->result = NULL;
2828+
/* ... and don't forget to disable the callback */
2829+
dmstate->result_cb.arg = NULL;
2830+
}
28132831

28142832
/* Release remote connection */
28152833
ReleaseConnection(dmstate->conn);
@@ -4578,13 +4596,17 @@ execute_dml_stmt(ForeignScanState *node)
45784596
/*
45794597
* Get the result, and check for success.
45804598
*
4581-
* We don't use a PG_TRY block here, so be careful not to throw error
4582-
* without releasing the PGresult.
4599+
* We use a memory context callback to ensure that the PGresult will be
4600+
* released, even if the query fails somewhere that's outside our control.
4601+
* The callback is already registered, just need to fill in its arg.
45834602
*/
4603+
Assert(dmstate->result == NULL);
45844604
dmstate->result = pgfdw_get_result(dmstate->conn);
4605+
dmstate->result_cb.arg = dmstate->result;
4606+
45854607
if (PQresultStatus(dmstate->result) !=
45864608
(dmstate->has_returning ? PGRES_TUPLES_OK : PGRES_COMMAND_OK))
4587-
pgfdw_report_error(ERROR, dmstate->result, dmstate->conn, true,
4609+
pgfdw_report_error(ERROR, dmstate->result, dmstate->conn, false,
45884610
dmstate->query);
45894611

45904612
/* Get the number of rows affected. */
@@ -4628,30 +4650,16 @@ get_returning_data(ForeignScanState *node)
46284650
}
46294651
else
46304652
{
4631-
/*
4632-
* On error, be sure to release the PGresult on the way out. Callers
4633-
* do not have PG_TRY blocks to ensure this happens.
4634-
*/
4635-
PG_TRY();
4636-
{
4637-
HeapTuple newtup;
4638-
4639-
newtup = make_tuple_from_result_row(dmstate->result,
4640-
dmstate->next_tuple,
4641-
dmstate->rel,
4642-
dmstate->attinmeta,
4643-
dmstate->retrieved_attrs,
4644-
node,
4645-
dmstate->temp_cxt);
4646-
ExecStoreHeapTuple(newtup, slot, false);
4647-
}
4648-
PG_CATCH();
4649-
{
4650-
PQclear(dmstate->result);
4651-
PG_RE_THROW();
4652-
}
4653-
PG_END_TRY();
4653+
HeapTuple newtup;
46544654

4655+
newtup = make_tuple_from_result_row(dmstate->result,
4656+
dmstate->next_tuple,
4657+
dmstate->rel,
4658+
dmstate->attinmeta,
4659+
dmstate->retrieved_attrs,
4660+
node,
4661+
dmstate->temp_cxt);
4662+
ExecStoreHeapTuple(newtup, slot, false);
46554663
/* Get the updated/deleted tuple. */
46564664
if (dmstate->rel)
46574665
resultSlot = slot;

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/postgres/postgres/commit/9339c85afc913752884858b28cca99b8f42a389c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy