Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit77aebe9

Browse files
committed
SQL/JSON: Avoid initializing unnecessary ON ERROR / ON EMPTY steps
When the ON ERROR / ON EMPTY behavior is to return NULL, returningNULL directly from ExecEvalJsonExprPath() suffices. Therefore, there'sno need to create separate steps to check the error/empty flag orthose to evaluate the the constant NULL expression. This speeds upcommon cases because the default ON ERROR / ON EMPTY behavior forJSON_QUERY() and JSON_VALUE() is to return NULL. However, these stepsare necessary if the RETURNING type is a domain, as constraints on thedomain 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.comBackpatch-through: 17
1 parentcd6b2ae commit77aebe9

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

‎src/backend/executor/execExpr.c‎

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,9 +4233,11 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
42334233
List*jumps_return_null=NIL;
42344234
List*jumps_to_end=NIL;
42354235
ListCell*lc;
4236-
ErrorSaveContext*escontext=
4237-
jsexpr->on_error->btype!=JSON_BEHAVIOR_ERROR ?
4238-
&jsestate->escontext :NULL;
4236+
ErrorSaveContext*escontext;
4237+
boolreturning_domain=
4238+
get_typtype(jsexpr->returning->typid)==TYPTYPE_DOMAIN;
4239+
4240+
Assert(jsexpr->on_error!=NULL);
42394241

42404242
jsestate->jsexpr=jsexpr;
42414243

@@ -4313,6 +4315,9 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
43134315
scratch->d.constval.isnull= true;
43144316
ExprEvalPushStep(state,scratch);
43154317

4318+
escontext=jsexpr->on_error->btype!=JSON_BEHAVIOR_ERROR ?
4319+
&jsestate->escontext :NULL;
4320+
43164321
/*
43174322
* To handle coercion errors softly, use the following ErrorSaveContext to
43184323
* pass to ExecInitExprRec() when initializing the coercion expressions
@@ -4384,9 +4389,18 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
43844389
* Step to check jsestate->error and return the ON ERROR expression if
43854390
* there is one. This handles both the errors that occur during jsonpath
43864391
* evaluation in EEOP_JSONEXPR_PATH and subsequent coercion evaluation.
4392+
*
4393+
* Speed up common cases by avoiding extra steps for a NULL-valued ON
4394+
* ERROR expression unless RETURNING a domain type, where constraints must
4395+
* be checked. ExecEvalJsonExprPath() already returns NULL on error,
4396+
* making additional steps unnecessary in typical scenarios. Note that the
4397+
* default ON ERROR behavior for JSON_VALUE() and JSON_QUERY() is to
4398+
* return NULL.
43874399
*/
4388-
if (jsexpr->on_error&&
4389-
jsexpr->on_error->btype!=JSON_BEHAVIOR_ERROR)
4400+
if (jsexpr->on_error->btype!=JSON_BEHAVIOR_ERROR&&
4401+
(!(IsA(jsexpr->on_error->expr,Const)&&
4402+
((Const*)jsexpr->on_error->expr)->constisnull)||
4403+
returning_domain))
43904404
{
43914405
ErrorSaveContext*saved_escontext;
43924406

@@ -4441,9 +4455,15 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
44414455
/*
44424456
* Step to check jsestate->empty and return the ON EMPTY expression if
44434457
* there is one.
4458+
*
4459+
* See the comment above for details on the optimization for NULL-valued
4460+
* expressions.
44444461
*/
44454462
if (jsexpr->on_empty!=NULL&&
4446-
jsexpr->on_empty->btype!=JSON_BEHAVIOR_ERROR)
4463+
jsexpr->on_empty->btype!=JSON_BEHAVIOR_ERROR&&
4464+
(!(IsA(jsexpr->on_empty->expr,Const)&&
4465+
((Const*)jsexpr->on_empty->expr)->constisnull)||
4466+
returning_domain))
44474467
{
44484468
ErrorSaveContext*saved_escontext;
44494469

‎src/backend/executor/execExprInterp.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4414,8 +4414,8 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
44144414
/* Set up to catch coercion errors of the ON EMPTY value. */
44154415
jsestate->escontext.error_occurred= false;
44164416
jsestate->escontext.details_wanted= true;
4417-
Assert(jsestate->jump_empty >=0);
4418-
returnjsestate->jump_empty;
4417+
/* Jump to end if the ON EMPTY behavior is to return NULL */
4418+
returnjsestate->jump_empty >=0 ?jsestate->jump_empty :jsestate->jump_end;
44194419
}
44204420
}
44214421
elseif (jsexpr->on_error->btype!=JSON_BEHAVIOR_ERROR)
@@ -4424,8 +4424,9 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
44244424
/* Set up to catch coercion errors of the ON ERROR value. */
44254425
jsestate->escontext.error_occurred= false;
44264426
jsestate->escontext.details_wanted= true;
4427-
Assert(!throw_error&&jsestate->jump_error >=0);
4428-
returnjsestate->jump_error;
4427+
Assert(!throw_error);
4428+
/* Jump to end if the ON ERROR behavior is to return NULL */
4429+
returnjsestate->jump_error >=0 ?jsestate->jump_error :jsestate->jump_end;
44294430
}
44304431

44314432
if (jsexpr->column_name)
@@ -4445,14 +4446,15 @@ ExecEvalJsonExprPath(ExprState *state, ExprEvalStep *op,
44454446
*/
44464447
if (error)
44474448
{
4448-
Assert(!throw_error&&jsestate->jump_error >=0);
4449+
Assert(!throw_error);
44494450
*op->resvalue= (Datum)0;
44504451
*op->resnull= true;
44514452
jsestate->error.value=BoolGetDatum(true);
44524453
/* Set up to catch coercion errors of the ON ERROR value. */
44534454
jsestate->escontext.error_occurred= false;
44544455
jsestate->escontext.details_wanted= true;
4455-
returnjsestate->jump_error;
4456+
/* Jump to end if the ON ERROR behavior is to return NULL */
4457+
returnjsestate->jump_error >=0 ?jsestate->jump_error :jsestate->jump_end;
44564458
}
44574459

44584460
returnjump_eval_coercion >=0 ?jump_eval_coercion :jsestate->jump_end;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp