Skip to content

Commit 3c31594

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 39b1d19 commit 3c31594

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 33 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 */
@@ -2656,6 +2657,17 @@ postgresBeginDirectModify(ForeignScanState *node, int eflags)
26562657
dmstate = (PgFdwDirectModifyState *) palloc0(sizeof(PgFdwDirectModifyState));
26572658
node->fdw_state = (void *) dmstate;
26582659

2660+
/*
2661+
* We use a memory context callback to ensure that the dmstate's PGresult
2662+
* (if any) will be released, even if the query fails somewhere that's
2663+
* outside our control. The callback is always armed for the duration of
2664+
* the query; this relies on PQclear(NULL) being a no-op.
2665+
*/
2666+
dmstate->result_cb.func = (MemoryContextCallbackFunction) PQclear;
2667+
dmstate->result_cb.arg = NULL;
2668+
MemoryContextRegisterResetCallback(CurrentMemoryContext,
2669+
&dmstate->result_cb);
2670+
26592671
/*
26602672
* Identify which user to do the remote access as. This should match what
26612673
* ExecCheckRTEPerms() does.
@@ -2805,7 +2817,12 @@ postgresEndDirectModify(ForeignScanState *node)
28052817

28062818
/* Release PGresult */
28072819
if (dmstate->result)
2820+
{
28082821
PQclear(dmstate->result);
2822+
dmstate->result = NULL;
2823+
/* ... and don't forget to disable the callback */
2824+
dmstate->result_cb.arg = NULL;
2825+
}
28092826

28102827
/* Release remote connection */
28112828
ReleaseConnection(dmstate->conn);
@@ -4580,13 +4597,17 @@ execute_dml_stmt(ForeignScanState *node)
45804597
/*
45814598
* Get the result, and check for success.
45824599
*
4583-
* We don't use a PG_TRY block here, so be careful not to throw error
4584-
* without releasing the PGresult.
4600+
* We use a memory context callback to ensure that the PGresult will be
4601+
* released, even if the query fails somewhere that's outside our control.
4602+
* The callback is already registered, just need to fill in its arg.
45854603
*/
4604+
Assert(dmstate->result == NULL);
45864605
dmstate->result = pgfdw_get_result(dmstate->conn, dmstate->query);
4606+
dmstate->result_cb.arg = dmstate->result;
4607+
45874608
if (PQresultStatus(dmstate->result) !=
45884609
(dmstate->has_returning ? PGRES_TUPLES_OK : PGRES_COMMAND_OK))
4589-
pgfdw_report_error(ERROR, dmstate->result, dmstate->conn, true,
4610+
pgfdw_report_error(ERROR, dmstate->result, dmstate->conn, false,
45904611
dmstate->query);
45914612

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

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

0 commit comments

Comments
 (0)