Skip to content

Commit 3a97460

Browse files
committed
SQL/JSON: Avoid initializing unnecessary ON ERROR / ON EMPTY steps
When the ON ERROR / ON EMPTY behavior is to return NULL, returning NULL directly from ExecEvalJsonExprPath() suffices. Therefore, there's no need to create separate steps to check the error/empty flag or those to evaluate the the constant NULL expression. This speeds up common cases because the default ON ERROR / ON EMPTY behavior for JSON_QUERY() and JSON_VALUE() is to return NULL. However, these steps are necessary if the RETURNING type is a domain, as constraints on the domain may need to be checked. Reported-by: Jian He <jian.universality@gmail.com> Author: Jian He <jian.universality@gmail.com> Author: Amit Langote <amitlangote09@gmail.com> Discussion: https://postgr.es/m/CACJufxEo4sUjKCYtda0_qt9tazqqKPmF1cqhW9KBOUeJFqQd2g@mail.gmail.com Backpatch-through: 17
1 parent 565caaa commit 3a97460

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/backend/executor/execExpr.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4414,6 +4414,8 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
44144414
ErrorSaveContext *escontext =
44154415
jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR ?
44164416
&jsestate->escontext : NULL;
4417+
bool returning_domain =
4418+
get_typtype(jsexpr->returning->typid) == TYPTYPE_DOMAIN;
44174419

44184420
jsestate->jsexpr = jsexpr;
44194421

@@ -4556,20 +4558,27 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
45564558
ExprEvalPushStep(state, scratch);
45574559
}
45584560

4559-
jsestate->jump_empty = jsestate->jump_error = -1;
4560-
45614561
/*
45624562
* Step to check jsestate->error and return the ON ERROR expression if
45634563
* there is one. This handles both the errors that occur during jsonpath
45644564
* evaluation in EEOP_JSONEXPR_PATH and subsequent coercion evaluation.
4565+
*
4566+
* Speed up common cases by avoiding extra steps for a NULL-valued ON
4567+
* ERROR expression unless RETURNING a domain type, where constraints must
4568+
* be checked. ExecEvalJsonExprPath() already returns NULL on error,
4569+
* making additional steps unnecessary in typical scenarios. Note that the
4570+
* default ON ERROR behavior for JSON_VALUE() and JSON_QUERY() is to
4571+
* return NULL.
45654572
*/
4573+
jsestate->jump_error = state->steps_len;
45664574
if (jsexpr->on_error &&
4567-
jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR)
4575+
jsexpr->on_error->btype != JSON_BEHAVIOR_ERROR &&
4576+
(!(IsA(jsexpr->on_error->expr, Const) &&
4577+
((Const *) jsexpr->on_error->expr)->constisnull) ||
4578+
returning_domain))
45684579
{
45694580
ErrorSaveContext *saved_escontext;
45704581

4571-
jsestate->jump_error = state->steps_len;
4572-
45734582
/* JUMP to end if false, that is, skip the ON ERROR expression. */
45744583
jumps_to_end = lappend_int(jumps_to_end, state->steps_len);
45754584
scratch->opcode = EEOP_JUMP_IF_NOT_TRUE;
@@ -4619,14 +4628,19 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
46194628
/*
46204629
* Step to check jsestate->empty and return the ON EMPTY expression if
46214630
* there is one.
4631+
*
4632+
* See the comment above for details on the optimization for NULL-valued
4633+
* expressions.
46224634
*/
4635+
jsestate->jump_empty = state->steps_len;
46234636
if (jsexpr->on_empty != NULL &&
4624-
jsexpr->on_empty->btype != JSON_BEHAVIOR_ERROR)
4637+
jsexpr->on_empty->btype != JSON_BEHAVIOR_ERROR &&
4638+
(!(IsA(jsexpr->on_empty->expr, Const) &&
4639+
((Const *) jsexpr->on_empty->expr)->constisnull) ||
4640+
returning_domain))
46254641
{
46264642
ErrorSaveContext *saved_escontext;
46274643

4628-
jsestate->jump_empty = state->steps_len;
4629-
46304644
/* JUMP to end if false, that is, skip the ON EMPTY expression. */
46314645
jumps_to_end = lappend_int(jumps_to_end, state->steps_len);
46324646
scratch->opcode = EEOP_JUMP_IF_NOT_TRUE;

0 commit comments

Comments
 (0)