v2-0001-Replace-EEOP_DONE-with-special-steps-for-return-n.patch
application/octet-stream
Filename: v2-0001-Replace-EEOP_DONE-with-special-steps-for-return-n.patch
Type: application/octet-stream
Part: 1
Patch
Same data as JSON:
GET /api/v1/attachments/:id/patch
the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes.
API reference →
Format: format-patch
Series: patch v2-0001
Subject: Replace EEOP_DONE with special steps for return/no return
| File | + | − |
|---|---|---|
| src/backend/executor/execExpr.c | 10 | 10 |
| src/backend/executor/execExprInterp.c | 17 | 8 |
| src/backend/executor/nodeAgg.c | 2 | 5 |
| src/backend/executor/README | 8 | 3 |
| src/backend/jit/llvm/llvmjit_expr.c | 5 | 1 |
| src/include/executor/execExpr.h | 5 | 2 |
| src/include/executor/executor.h | 49 | 3 |
From 959e0b4f16efb75c63d946540b6a261899483fa6 Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <dgustafsson@postgresql.org>
Date: Thu, 4 Jul 2024 16:55:52 +0200
Subject: [PATCH v2 1/2] Replace EEOP_DONE with special steps for return/no
return
Knowing when the side-effects of an expression is the intended result
of the execution, rather than the returnvalue, is important for being
able generate more efficient JITed code. This replaces EEOP_DONE with
two new steps: EEOP_DONE_RETURN and EEOP_DONE_NO_RETURN. Expressions
which return a value should use the former step; expressions used for
their side-effects which don't return value should use the latter.
Author: Andres Freund, Daniel Gustafsson
Reviewed-by: Andreas Karlsson <andreas@proxel.se>
Discussion: https://postgr.es/m/415721CE-7D2E-4B74-B5D9-1950083BA03E@yesql.se
Discussion: https://postgr.es/m/20191023163849.sosqbfs5yenocez3@alap3.anarazel.de
---
src/backend/executor/README | 11 ++++--
src/backend/executor/execExpr.c | 20 +++++------
src/backend/executor/execExprInterp.c | 25 ++++++++-----
src/backend/executor/nodeAgg.c | 7 ++--
src/backend/jit/llvm/llvmjit_expr.c | 6 +++-
src/include/executor/execExpr.h | 7 ++--
src/include/executor/executor.h | 52 +++++++++++++++++++++++++--
7 files changed, 96 insertions(+), 32 deletions(-)
diff --git a/src/backend/executor/README b/src/backend/executor/README
index 642d63be61..54f4782f31 100644
--- a/src/backend/executor/README
+++ b/src/backend/executor/README
@@ -133,9 +133,14 @@ is used by the function evaluation step, thus avoiding extra work to copy
the result values around.
The last entry in a completed ExprState->steps array is always an
-EEOP_DONE step; this removes the need to test for end-of-array while
-iterating. Also, if the expression contains any variable references (to
-user columns of the ExprContext's INNER, OUTER, or SCAN tuples), the steps
+EEOP_DONE_RETURN or EEOP_DONE_NO_RETURN step; this removes the need to
+test for end-of-array while iterating. The former is used when the
+expression returns a value directly, the latter when side-effects of
+expression initialization are the goal (e.g. for projection or
+aggregate transition value computation).
+
+Also, if the expression contains any variable references (to user
+columns of the ExprContext's INNER, OUTER, or SCAN tuples), the steps
array begins with EEOP_*_FETCHSOME steps that ensure that the relevant
tuples have been deconstructed to make the required columns directly
available (cf. slot_getsomeattrs()). This allows individual Var-fetching
diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c
index ccd4863778..9ca6a3fea3 100644
--- a/src/backend/executor/execExpr.c
+++ b/src/backend/executor/execExpr.c
@@ -8,7 +8,7 @@
* using ExecInitExpr() et al. This converts the tree into a flat array
* of ExprEvalSteps, which may be thought of as instructions in a program.
* At runtime, we'll execute steps, starting with the first, until we reach
- * an EEOP_DONE opcode.
+ * an EEOP_DONE_{RETURN|NO_RETURN} opcode.
*
* This file contains the "compilation" logic. It is independent of the
* specific execution technology we use (switch statement, computed goto,
@@ -153,7 +153,7 @@ ExecInitExpr(Expr *node, PlanState *parent)
ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
/* Finally, append a DONE step */
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
@@ -190,7 +190,7 @@ ExecInitExprWithParams(Expr *node, ParamListInfo ext_params)
ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
/* Finally, append a DONE step */
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
@@ -282,7 +282,7 @@ ExecInitQual(List *qual, PlanState *parent)
* have yielded TRUE, and since its result is stored in the desired output
* location, we're done.
*/
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
@@ -477,7 +477,7 @@ ExecBuildProjectionInfo(List *targetList,
}
}
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_NO_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
@@ -716,7 +716,7 @@ ExecBuildUpdateProjection(List *targetList,
}
}
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_NO_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
@@ -1665,7 +1665,7 @@ ExecInitExprRec(Expr *node, ExprState *state,
else
{
/* Not trivial, so append a DONE step */
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(elemstate, &scratch);
/* and ready the subexpression */
ExecReadyExpr(elemstate);
@@ -3812,7 +3812,7 @@ ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
scratch.resvalue = NULL;
scratch.resnull = NULL;
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_NO_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
@@ -4076,7 +4076,7 @@ ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
scratch.resvalue = NULL;
scratch.resnull = NULL;
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
@@ -4210,7 +4210,7 @@ ExecBuildParamSetEqual(TupleDesc desc,
scratch.resvalue = NULL;
scratch.resnull = NULL;
- scratch.opcode = EEOP_DONE;
+ scratch.opcode = EEOP_DONE_RETURN;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
diff --git a/src/backend/executor/execExprInterp.c b/src/backend/executor/execExprInterp.c
index d8735286c4..0b20e2885d 100644
--- a/src/backend/executor/execExprInterp.c
+++ b/src/backend/executor/execExprInterp.c
@@ -240,7 +240,8 @@ ExecReadyInterpretedExpr(ExprState *state)
/* Simple validity checks on expression */
Assert(state->steps_len >= 1);
- Assert(state->steps[state->steps_len - 1].opcode == EEOP_DONE);
+ Assert(state->steps[state->steps_len - 1].opcode == EEOP_DONE_RETURN ||
+ state->steps[state->steps_len - 1].opcode == EEOP_DONE_NO_RETURN);
/*
* Don't perform redundant initialization. This is unreachable in current
@@ -406,7 +407,8 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
*/
#if defined(EEO_USE_COMPUTED_GOTO)
static const void *const dispatch_table[] = {
- &&CASE_EEOP_DONE,
+ &&CASE_EEOP_DONE_RETURN,
+ &&CASE_EEOP_DONE_NO_RETURN,
&&CASE_EEOP_INNER_FETCHSOME,
&&CASE_EEOP_OUTER_FETCHSOME,
&&CASE_EEOP_SCAN_FETCHSOME,
@@ -530,9 +532,16 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
EEO_SWITCH()
{
- EEO_CASE(EEOP_DONE)
+ EEO_CASE(EEOP_DONE_RETURN)
{
- goto out;
+ *isnull = state->resnull;
+ return state->resvalue;
+ }
+
+ EEO_CASE(EEOP_DONE_NO_RETURN)
+ {
+ Assert(isnull == NULL);
+ return 0;
}
EEO_CASE(EEOP_INNER_FETCHSOME)
@@ -1885,13 +1894,13 @@ ExecInterpExpr(ExprState *state, ExprContext *econtext, bool *isnull)
{
/* unreachable */
Assert(false);
- goto out;
+ goto out_error;
}
}
-out:
- *isnull = state->resnull;
- return state->resvalue;
+out_error:
+ pg_unreachable();
+ return (Datum) 0;
}
/*
diff --git a/src/backend/executor/nodeAgg.c b/src/backend/executor/nodeAgg.c
index 53ead77ece..c8257a23d1 100644
--- a/src/backend/executor/nodeAgg.c
+++ b/src/backend/executor/nodeAgg.c
@@ -815,11 +815,8 @@ advance_transition_function(AggState *aggstate,
static void
advance_aggregates(AggState *aggstate)
{
- bool dummynull;
-
- ExecEvalExprSwitchContext(aggstate->phase->evaltrans,
- aggstate->tmpcontext,
- &dummynull);
+ ExecEvalExprNoReturnSwitchContext(aggstate->phase->evaltrans,
+ aggstate->tmpcontext);
}
/*
diff --git a/src/backend/jit/llvm/llvmjit_expr.c b/src/backend/jit/llvm/llvmjit_expr.c
index 306aea82d3..4cbbdc72b2 100644
--- a/src/backend/jit/llvm/llvmjit_expr.c
+++ b/src/backend/jit/llvm/llvmjit_expr.c
@@ -285,7 +285,7 @@ llvm_compile_expr(ExprState *state)
switch (opcode)
{
- case EEOP_DONE:
+ case EEOP_DONE_RETURN:
{
LLVMValueRef v_tmpisnull;
LLVMValueRef v_tmpvalue;
@@ -299,6 +299,10 @@ llvm_compile_expr(ExprState *state)
break;
}
+ case EEOP_DONE_NO_RETURN:
+ LLVMBuildRet(b, l_sizet_const(0));
+ break;
+
case EEOP_INNER_FETCHSOME:
case EEOP_OUTER_FETCHSOME:
case EEOP_SCAN_FETCHSOME:
diff --git a/src/include/executor/execExpr.h b/src/include/executor/execExpr.h
index 55337d4916..4a7aca6a1a 100644
--- a/src/include/executor/execExpr.h
+++ b/src/include/executor/execExpr.h
@@ -65,8 +65,11 @@ typedef struct ExprEvalRowtypeCache
*/
typedef enum ExprEvalOp
{
- /* entire expression has been evaluated completely, return */
- EEOP_DONE,
+ /* entire expression has been evaluated, return value */
+ EEOP_DONE_RETURN,
+
+ /* entire expression has been evaluated, no return value */
+ EEOP_DONE_NO_RETURN,
/* apply slot_getsomeattrs on corresponding tuple slot */
EEOP_INNER_FETCHSOME,
diff --git a/src/include/executor/executor.h b/src/include/executor/executor.h
index 9770752ea3..39a082e9ad 100644
--- a/src/include/executor/executor.h
+++ b/src/include/executor/executor.h
@@ -338,6 +338,34 @@ ExecEvalExpr(ExprState *state,
}
#endif
+/*
+ * ExecEvalExprNoReturn
+ *
+ * Like ExecEvalExpr(), but for cases where no return value is expected,
+ * because the side-effects of expression evaluation are what's desired. This
+ * is e.g. used for projection and aggregate transition computation.
+
+ * Evaluate expression identified by "state" in the execution context
+ * given by "econtext".
+ *
+ * The caller should already have switched into the temporary memory context
+ * econtext->ecxt_per_tuple_memory. The convenience entry point
+ * ExecEvalExprNoReturnSwitchContext() is provided for callers who don't
+ * prefer to do the switch in an outer loop.
+ */
+#ifndef FRONTEND
+static inline void
+ExecEvalExprNoReturn(ExprState *state,
+ ExprContext *econtext)
+{
+ PG_USED_FOR_ASSERTS_ONLY Datum retDatum;
+
+ retDatum = state->evalfunc(state, econtext, NULL);
+
+ Assert(retDatum == (Datum) 0);
+}
+#endif
+
/*
* ExecEvalExprSwitchContext
*
@@ -359,6 +387,25 @@ ExecEvalExprSwitchContext(ExprState *state,
}
#endif
+/*
+ * ExecEvalExprNoReturnSwitchContext
+ *
+ * Same as ExecEvalExprNoReturn, but get into the right allocation context
+ * explicitly.
+ */
+#ifndef FRONTEND
+static inline void
+ExecEvalExprNoReturnSwitchContext(ExprState *state,
+ ExprContext *econtext)
+{
+ MemoryContext oldContext;
+
+ oldContext = MemoryContextSwitchTo(econtext->ecxt_per_tuple_memory);
+ ExecEvalExprNoReturn(state, econtext);
+ MemoryContextSwitchTo(oldContext);
+}
+#endif
+
/*
* ExecProject
*
@@ -378,7 +425,6 @@ ExecProject(ProjectionInfo *projInfo)
ExprContext *econtext = projInfo->pi_exprContext;
ExprState *state = &projInfo->pi_state;
TupleTableSlot *slot = state->resultslot;
- bool isnull;
/*
* Clear any former contents of the result slot. This makes it safe for
@@ -386,8 +432,8 @@ ExecProject(ProjectionInfo *projInfo)
*/
ExecClearTuple(slot);
- /* Run the expression, discarding scalar result from the last column. */
- (void) ExecEvalExprSwitchContext(state, econtext, &isnull);
+ /* Run the expression */
+ ExecEvalExprNoReturnSwitchContext(state, econtext);
/*
* Successfully formed a result row. Mark the result slot as containing a
--
2.39.3 (Apple Git-146)