diff options
author | David Rowley <drowley@postgresql.org> | 2024-10-17 14:25:08 +1300 |
---|---|---|
committer | David Rowley <drowley@postgresql.org> | 2024-10-17 14:25:08 +1300 |
commit | 9ca67658d19e6c258eb4021a326ed7d38b3ab75f (patch) | |
tree | 8034b0b5bfb372ebdf68970faa90c052bfa050bc /src/backend/executor/execExpr.c | |
parent | 089aac631b5ba53be0ecf8ea2e8d81388d69629c (diff) | |
download | postgresql-9ca67658d19e6c258eb4021a326ed7d38b3ab75f.tar.gz postgresql-9ca67658d19e6c258eb4021a326ed7d38b3ab75f.zip |
Don't store intermediate hash values in ExprState->resvalue
adf97c156 made it so ExprStates could support hashing and changed Hash
Join to use that instead of manually extracting Datums from tuples and
hashing them one column at a time.
When hashing multiple columns or expressions, the code added in that
commit stored the intermediate hash value in the ExprState's resvalue
field. That was a mistake as steps may be injected into the ExprState
between each hashing step that look at or overwrite the stored
intermediate hash value. EEOP_PARAM_SET is an example of such a step.
Here we fix this by adding a new dedicated field for storing
intermediate hash values and adjust the code so that all apart from the
final hashing step store their result in the intermediate field.
In passing, rename a variable so that it's more aligned to the
surrounding code and also so a few lines stay within the 80 char margin.
Reported-by: Andres Freund
Reviewed-by: Alena Rybakina <a.rybakina@postgrespro.ru>
Discussion: https://postgr.es/m/CAApHDvqo9eenEFXND5zZ9JxO_k4eTA4jKMGxSyjdTrsmYvnmZw@mail.gmail.com
Diffstat (limited to 'src/backend/executor/execExpr.c')
-rw-r--r-- | src/backend/executor/execExpr.c | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/src/backend/executor/execExpr.c b/src/backend/executor/execExpr.c index c8077aa57bd..a343d0bc6a2 100644 --- a/src/backend/executor/execExpr.c +++ b/src/backend/executor/execExpr.c @@ -3996,6 +3996,7 @@ ExecBuildHash32Expr(TupleDesc desc, const TupleTableSlotOps *ops, { ExprState *state = makeNode(ExprState); ExprEvalStep scratch = {0}; + NullableDatum *iresult = NULL; List *adjust_jumps = NIL; ListCell *lc; ListCell *lc2; @@ -4009,6 +4010,14 @@ ExecBuildHash32Expr(TupleDesc desc, const TupleTableSlotOps *ops, /* Insert setup steps as needed. */ ExecCreateExprSetupSteps(state, (Node *) hash_exprs); + /* + * When hashing more than 1 expression or if we have an init value, we + * need somewhere to store the intermediate hash value so that it's + * available to be combined with the result of subsequent hashing. + */ + if (list_length(hash_exprs) > 1 || init_value != 0) + iresult = palloc(sizeof(NullableDatum)); + if (init_value == 0) { /* @@ -4024,8 +4033,8 @@ ExecBuildHash32Expr(TupleDesc desc, const TupleTableSlotOps *ops, /* Set up operation to set the initial value. */ scratch.opcode = EEOP_HASHDATUM_SET_INITVAL; scratch.d.hashdatum_initvalue.init_value = UInt32GetDatum(init_value); - scratch.resvalue = &state->resvalue; - scratch.resnull = &state->resnull; + scratch.resvalue = &iresult->value; + scratch.resnull = &iresult->isnull; ExprEvalPushStep(state, &scratch); @@ -4063,8 +4072,26 @@ ExecBuildHash32Expr(TupleDesc desc, const TupleTableSlotOps *ops, &fcinfo->args[0].value, &fcinfo->args[0].isnull); - scratch.resvalue = &state->resvalue; - scratch.resnull = &state->resnull; + if (i == list_length(hash_exprs) - 1) + { + /* the result for hashing the final expr is stored in the state */ + scratch.resvalue = &state->resvalue; + scratch.resnull = &state->resnull; + } + else + { + Assert(iresult != NULL); + + /* intermediate values are stored in an intermediate result */ + scratch.resvalue = &iresult->value; + scratch.resnull = &iresult->isnull; + } + + /* + * NEXT32 opcodes need to look at the intermediate result. We might + * as well just set this for all ops. FIRSTs won't look at it. + */ + scratch.d.hashdatum.iresult = iresult; /* Initialize function call parameter structure too */ InitFunctionCallInfoData(*fcinfo, finfo, 1, inputcollid, NULL, NULL); |