Skip to content

Commit 0d4899e

Browse files
committed
Do a conditional SPI_push/SPI_pop when replanning a query in
RevalidateCachedPlan. This is to avoid a "SPI_ERROR_CONNECT" failure when the planner calls a SPI-using function and we are already inside one. The alternative fix is to expect callers of RevalidateCachedPlan to do this, which seems likely to result in additional hard-to-detect bugs of omission. Per reports from Frank van Vugt and Marek Lewczuk. Back-patch to 8.3. It's much harder to trigger the bug in 8.3, due to a smaller set of cases in which plans can be invalidated, but it could happen. (I think perhaps only a SI reset event could make 8.3 fail here, but that's certainly within the realm of possibility.)
1 parent 6fafa6a commit 0d4899e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/backend/utils/cache/plancache.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
* Portions Copyright (c) 1994, Regents of the University of California
3636
*
3737
* IDENTIFICATION
38-
* $PostgreSQL: pgsql/src/backend/utils/cache/plancache.c,v 1.27 2009/06/11 14:49:05 momjian Exp $
38+
* $PostgreSQL: pgsql/src/backend/utils/cache/plancache.c,v 1.28 2009/07/14 15:37:50 tgl Exp $
3939
*
4040
*-------------------------------------------------------------------------
4141
*/
@@ -45,6 +45,7 @@
4545
#include "access/transam.h"
4646
#include "catalog/namespace.h"
4747
#include "executor/executor.h"
48+
#include "executor/spi.h"
4849
#include "nodes/nodeFuncs.h"
4950
#include "optimizer/planmain.h"
5051
#include "storage/lmgr.h"
@@ -502,8 +503,19 @@ RevalidateCachedPlan(CachedPlanSource *plansource, bool useResOwner)
502503
{
503504
/*
504505
* Generate plans for queries.
506+
*
507+
* The planner may try to call SPI-using functions, which causes
508+
* a problem if we're already inside one. Rather than expect
509+
* all SPI-using code to do SPI_push whenever a replan could
510+
* happen, it seems best to take care of the case here.
505511
*/
512+
bool pushed;
513+
514+
pushed = SPI_push_conditional();
515+
506516
slist = pg_plan_queries(slist, plansource->cursor_options, NULL);
517+
518+
SPI_pop_conditional(pushed);
507519
}
508520

509521
/*

0 commit comments

Comments
 (0)