Skip to content

Commit c12693d

Browse files
committed
Introduce ExecQualAndReset() helper.
It's a common task to evaluate a qual and reset the corresponding expression context. Currently that requires storing the result of the qual eval, resetting the context, and then reacting on the result. As that's awkward several places only reset the context next time through a node. That's not great, so introduce a helper that evaluates and resets. It's a bit ugly that it currently uses MemoryContextReset() instead of ResetExprContext(), but that seems easier than reordering all of executor.h. Author: Andres Freund Discussion: https://postgr.es/m/20180109222544.f7loxrunqh3xjl5f@alap3.anarazel.de
1 parent 97d4445 commit c12693d

File tree

5 files changed

+25
-25
lines changed

5 files changed

+25
-25
lines changed

src/backend/executor/nodeBitmapHeapscan.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,7 @@ BitmapHeapNext(BitmapHeapScanState *node)
352352
if (tbmres->recheck)
353353
{
354354
econtext->ecxt_scantuple = slot;
355-
ResetExprContext(econtext);
356-
357-
if (!ExecQual(node->bitmapqualorig, econtext))
355+
if (!ExecQualAndReset(node->bitmapqualorig, econtext))
358356
{
359357
/* Fails recheck, so drop it and loop back for another */
360358
InstrCountFiltered2(node, 1);
@@ -717,10 +715,7 @@ BitmapHeapRecheck(BitmapHeapScanState *node, TupleTableSlot *slot)
717715

718716
/* Does the tuple meet the original qual conditions? */
719717
econtext->ecxt_scantuple = slot;
720-
721-
ResetExprContext(econtext);
722-
723-
return ExecQual(node->bitmapqualorig, econtext);
718+
return ExecQualAndReset(node->bitmapqualorig, econtext);
724719
}
725720

726721
/* ----------------------------------------------------------------

src/backend/executor/nodeHash.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,10 +1942,7 @@ ExecScanHashBucket(HashJoinState *hjstate,
19421942
false); /* do not pfree */
19431943
econtext->ecxt_innertuple = inntuple;
19441944

1945-
/* reset temp memory each time to avoid leaks from qual expr */
1946-
ResetExprContext(econtext);
1947-
1948-
if (ExecQual(hjclauses, econtext))
1945+
if (ExecQualAndReset(hjclauses, econtext))
19491946
{
19501947
hjstate->hj_CurTuple = hashTuple;
19511948
return true;
@@ -2002,10 +1999,7 @@ ExecParallelScanHashBucket(HashJoinState *hjstate,
20021999
false); /* do not pfree */
20032000
econtext->ecxt_innertuple = inntuple;
20042001

2005-
/* reset temp memory each time to avoid leaks from qual expr */
2006-
ResetExprContext(econtext);
2007-
2008-
if (ExecQual(hjclauses, econtext))
2002+
if (ExecQualAndReset(hjclauses, econtext))
20092003
{
20102004
hjstate->hj_CurTuple = hashTuple;
20112005
return true;

src/backend/executor/nodeIndexonlyscan.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,7 @@ IndexOnlyNext(IndexOnlyScanState *node)
214214
if (scandesc->xs_recheck)
215215
{
216216
econtext->ecxt_scantuple = slot;
217-
ResetExprContext(econtext);
218-
if (!ExecQual(node->indexqual, econtext))
217+
if (!ExecQualAndReset(node->indexqual, econtext))
219218
{
220219
/* Fails recheck, so drop it and loop back for another */
221220
InstrCountFiltered2(node, 1);

src/backend/executor/nodeIndexscan.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ IndexNext(IndexScanState *node)
152152
if (scandesc->xs_recheck)
153153
{
154154
econtext->ecxt_scantuple = slot;
155-
ResetExprContext(econtext);
156-
if (!ExecQual(node->indexqualorig, econtext))
155+
if (!ExecQualAndReset(node->indexqualorig, econtext))
157156
{
158157
/* Fails recheck, so drop it and loop back for another */
159158
InstrCountFiltered2(node, 1);
@@ -300,8 +299,7 @@ IndexNextWithReorder(IndexScanState *node)
300299
if (scandesc->xs_recheck)
301300
{
302301
econtext->ecxt_scantuple = slot;
303-
ResetExprContext(econtext);
304-
if (!ExecQual(node->indexqualorig, econtext))
302+
if (!ExecQualAndReset(node->indexqualorig, econtext))
305303
{
306304
/* Fails recheck, so drop it and loop back for another */
307305
InstrCountFiltered2(node, 1);
@@ -420,10 +418,7 @@ IndexRecheck(IndexScanState *node, TupleTableSlot *slot)
420418

421419
/* Does the tuple meet the indexqual condition? */
422420
econtext->ecxt_scantuple = slot;
423-
424-
ResetExprContext(econtext);
425-
426-
return ExecQual(node->indexqualorig, econtext);
421+
return ExecQualAndReset(node->indexqualorig, econtext);
427422
}
428423

429424

src/include/executor/executor.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "catalog/partition.h"
1818
#include "executor/execdesc.h"
1919
#include "nodes/parsenodes.h"
20+
#include "utils/memutils.h"
2021

2122

2223
/*
@@ -381,6 +382,22 @@ ExecQual(ExprState *state, ExprContext *econtext)
381382
}
382383
#endif
383384

385+
/*
386+
* ExecQualAndReset() - evaluate qual with ExecQual() and reset expression
387+
* context.
388+
*/
389+
#ifndef FRONTEND
390+
static inline bool
391+
ExecQualAndReset(ExprState *state, ExprContext *econtext)
392+
{
393+
bool ret = ExecQual(state, econtext);
394+
395+
/* inline ResetExprContext, to avoid ordering issue in this file */
396+
MemoryContextReset(econtext->ecxt_per_tuple_memory);
397+
return ret;
398+
}
399+
#endif
400+
384401
extern bool ExecCheck(ExprState *state, ExprContext *context);
385402

386403
/*

0 commit comments

Comments
 (0)