Re: PG 15 (and to a smaller degree 14) regression due to ExprEvalStep size
Andres Freund <andres@anarazel.de>
Hi,
On 2022-08-02 12:05:55 +0900, Amit Langote wrote:
> On Tue, Aug 2, 2022 at 9:39 AM Andres Freund <andres@anarazel.de> wrote:
> > On 2022-07-27 17:01:13 +0900, Amit Langote wrote:
> > > Here's an updated version of the patch, with mostly cosmetic changes.
> > > In particular, I added comments describing the new llvm_compile_expr()
> > > blobs.
> >
> > - I've asked a couple times before: Why do we need space for every possible
> > datatype at once in JsonItemCoercions? Can there be multiple "concurrent"
> > coercions in process?
>
> This topic has been a head-scratcher for me too from the beginning,
> but I've since come to understand (convince myself) that we do need
> the coercions for all possible types, because we don't know the type
> of the JSON item that's going to pop out of the main JSON path
> expression until we've run it through the JSON path executor that
> ExecEvalJson() invokes. So, it's not possible to statically assign
> the coercion.
Sure. But that doesn't mean we have to have memory for every possible type *at
the same time*.
> I am not really sure if different coercions may be used
> in the same query over multiple evaluations of the same JSON path
> expression, but maybe that's also possible.
Even if the type can change, I don't think that means we need to have space
for multiple types at the same time - there can't be multiple coercions
happening at the same time, otherwise there could be two coercions of the same
type as well. So we don't need memory for every coercion type.
>
> > The whole coercion stuff just seems incredibly clunky (in a slightly
> > different shape before this patch). ExecEvalJsonExprItemCoercion() calls
> > ExecPrepareJsonItemCoercion(), which gets a pointer to one of the per-type
> > elements in JsonItemCoercionsState, dispatching on the type of the json
> > object. Then we later call ExecGetJsonItemCoercion() (via a convoluted
> > path), which again will dispatch on the type (extracting the json object
> > again afaics!), to then somehow eventually get the coerced value.
>
> I think it might be possible to make this a bit simpler, by not
> leaving anything coercion-related in ExecEvalJsonExpr().
Honestly, this code seems like it should just be rewritten from scratch.
> I left some pieces there, because I thought the error of not finding an
> appropriate coercion must be thrown right away as the code in
> ExecEvalJsonExpr() does after calling ExecGetJsonItemCoercion().
>
> ExecPrepareJsonItemCoercion() is called later when it's time to
> actually evaluate the coercion. If we move the error path to
> ExecPrepareJsonItemCoercion(), both ExecGetJsonItemCoercion() and the
> error path code in ExecEvalJsonExpr() will be unnecessary. I will
> give that a try.
Why do we need the separation of prepare and then evaluation? They're executed
straight after each other?
> > - Looks like there's still some recursive expression states, namely
> > JsonExprState->{result_coercion, coercions}?
>
> So, the problem with inlining coercion evaluation into the main parent
> JsonExpr's is that it needs to be wrapped in a sub-transaction to
> catch any errors and return NULL instead. I don't know a way to wrap
> ExprEvalStep evaluation in a sub-transaction to achieve that effect.
But we don't need to wrap arbitrary evaluation in a subtransaction - afaics
the coercion calls a single function, not an arbitrary expression?
> Ooh, thanks for letting me know. So maybe I am missing some
> llvmjist_emit.h/type.c infrastructure to read an int32 value
> (jumpdone) out of an int32 pointer (&jumpdone)?
No, you just need to replace l_ptr(TypeSizeT) with l_ptr(LLVMInt32Type()).
> > I first was confused why the code tries to load the jump target
> > dynamically. But then I saw that the interpreted code sets it dynamically -
> > why? That's completely unnecessary overhead afaics? There's just two
> > possible jump targets, no?
>
> Hmm, I looked at the code for other expressions that jump, especially
> CASE WHEN, but they use ready-made EEOP_JUMP_IF_* steps, which can be
> added statically. I thought we can't use them in this case, because
> the conditions are very ad-hoc, like if the JSON path computation
> returned an "empty" item or if the "error" flag was set during that
> computation, etc.
The minimal fix would be to return the jump target from the function, and then
jump to that. That at least avoids the roundtrip to memory you have right now.
Greetings,
Andres Freund
Commits
-
JSON_TABLE: Add support for NESTED paths and columns
- bb766cde63b4 17.0 landed
-
Add basic JSON_TABLE() functionality
- de3600452b61 17.0 landed
-
Add SQL/JSON query functions
- 6185c9737cf4 17.0 landed
-
Add soft error handling to some expression nodes
- aaaf9449ec6b 17.0 landed
-
Adjust populate_record_field() to handle errors softly
- 1edb3b491bee 17.0 landed
-
Refactor code used by jsonpath executor to fetch variables
- faa2b953ba3b 17.0 landed
-
Add more SQL/JSON constructor functions
- 03734a7fed7d 17.0 landed
-
SQL/JSON: support the IS JSON predicate
- 6ee30209a6f1 16.0 landed
-
SQL/JSON: add standard JSON constructor functions
- 7081ac46ace8 16.0 landed
-
Add static assertion ensuring sizeof(ExprEvalStep) <= 64 bytes
- 78be04e4c672 16.0 landed
-
Remove size increase in ExprEvalStep caused by hashed saops
- 30efc3b5a364 15.0 landed
- fe3caa143937 16.0 landed
-
pgstat: reduce timer overhead by leaving timer running.
- 4a37527fde3c 15.0 landed
- 056cc366fafa 16.0 landed
-
expression eval: Fix EEOP_JSON_CONSTRUCTOR and EEOP_JSONEXPR size.
- 5a1ab894f758 15.0 landed
- 67b26703b415 16.0 landed
-
SQL/JSON query functions
- 1a36bc9dba8e 15.0 cited
-
Speedup ScalarArrayOpExpr evaluation
- 50e17ad281b8 14.0 cited