Skip to content

Commit 4a07c09

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 a7da791 commit 4a07c09

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 */
@@ -2646,6 +2647,17 @@ postgresBeginDirectModify(ForeignScanState *node, int eflags)
26462647
dmstate = (PgFdwDirectModifyState *) palloc0(sizeof(PgFdwDirectModifyState));
26472648
node->fdw_state = (void *) dmstate;
26482649

2650+
/*
2651+
* We use a memory context callback to ensure that the dmstate's PGresult
2652+
* (if any) will be released, even if the query fails somewhere that's
2653+
* outside our control. The callback is always armed for the duration of
2654+
* the query; this relies on PQclear(NULL) being a no-op.
2655+
*/
2656+
dmstate->result_cb.func = (MemoryContextCallbackFunction) PQclear;
2657+
dmstate->result_cb.arg = NULL;
2658+
MemoryContextRegisterResetCallback(CurrentMemoryContext,
2659+
&dmstate->result_cb);
2660+
26492661
/*
26502662
* Identify which user to do the remote access as. This should match what
26512663
* ExecCheckRTEPerms() does.
@@ -2795,7 +2807,12 @@ postgresEndDirectModify(ForeignScanState *node)
27952807

27962808
/* Release PGresult */
27972809
if (dmstate->result)
2810+
{
27982811
PQclear(dmstate->result);
2812+
dmstate->result = NULL;
2813+
/* ... and don't forget to disable the callback */
2814+
dmstate->result_cb.arg = NULL;
2815+
}
27992816

28002817
/* Release remote connection */
28012818
ReleaseConnection(dmstate->conn);
@@ -4570,13 +4587,17 @@ execute_dml_stmt(ForeignScanState *node)
45704587
/*
45714588
* Get the result, and check for success.
45724589
*
4573-
* We don't use a PG_TRY block here, so be careful not to throw error
4574-
* without releasing the PGresult.
4590+
* We use a memory context callback to ensure that the PGresult will be
4591+
* released, even if the query fails somewhere that's outside our control.
4592+
* The callback is already registered, just need to fill in its arg.
45754593
*/
4594+
Assert(dmstate->result == NULL);
45764595
dmstate->result = pgfdw_get_result(dmstate->conn, dmstate->query);
4596+
dmstate->result_cb.arg = dmstate->result;
4597+
45774598
if (PQresultStatus(dmstate->result) !=
45784599
(dmstate->has_returning ? PGRES_TUPLES_OK : PGRES_COMMAND_OK))
4579-
pgfdw_report_error(ERROR, dmstate->result, dmstate->conn, true,
4600+
pgfdw_report_error(ERROR, dmstate->result, dmstate->conn, false,
45804601
dmstate->query);
45814602

45824603
/* Get the number of rows affected. */
@@ -4620,31 +4641,16 @@ get_returning_data(ForeignScanState *node)
46204641
}
46214642
else
46224643
{
4623-
/*
4624-
* On error, be sure to release the PGresult on the way out. Callers
4625-
* do not have PG_TRY blocks to ensure this happens.
4626-
*/
4627-
PG_TRY();
4628-
{
4629-
HeapTuple newtup;
4630-
4631-
newtup = make_tuple_from_result_row(dmstate->result,
4632-
dmstate->next_tuple,
4633-
dmstate->rel,
4634-
dmstate->attinmeta,
4635-
dmstate->retrieved_attrs,
4636-
node,
4637-
dmstate->temp_cxt);
4638-
ExecStoreHeapTuple(newtup, slot, false);
4639-
}
4640-
PG_CATCH();
4641-
{
4642-
if (dmstate->result)
4643-
PQclear(dmstate->result);
4644-
PG_RE_THROW();
4645-
}
4646-
PG_END_TRY();
4644+
HeapTuple newtup;
46474645

4646+
newtup = make_tuple_from_result_row(dmstate->result,
4647+
dmstate->next_tuple,
4648+
dmstate->rel,
4649+
dmstate->attinmeta,
4650+
dmstate->retrieved_attrs,
4651+
node,
4652+
dmstate->temp_cxt);
4653+
ExecStoreHeapTuple(newtup, slot, false);
46484654
/* Get the updated/deleted tuple. */
46494655
if (dmstate->rel)
46504656
resultSlot = slot;

0 commit comments

Comments
 (0)