PoC. The saving of the compiled jit-code in the plan cache
Thread notes
1 note#performance#moderate#enhancement
hackorum-bot#performance#moderate#enhancement
@global #performance #moderate #enhancement
Dear colleagues,
I implemented the patch to store in the plan cache an already compiled
jit
code. If a query is executed many times, the query time can be decreased
by
20-30% for long queries with big share of expressions in the code.
This patch can be applied to the current master and passes tests, but it
is
only the Proof of the Concept, and I need to get opinions and advice for
nexts
steps. It is not ready for commitfest, does not cover all scenarios
(craches
in some sutuation that need special solution), and source code is not
clean.
The code source has removed old code marked by // comments to easier
compare,
what was changed, especially in llvmjit_expr.c and llvmjit_deform.c
Implementation details.
1. Changes in jit-code generation.
a) the load of the absolute address (as const) changed to the load of
this
address from a struct member:
old version:
v_resvaluep = l_ptr_const(op->resvalue, l_ptr(TypeSizeT));
new version
v_resvaluep = l_load_struct_gep( b, g->StructExprEvalStep, v_op,
FIELDNO_EXPREVALSTEP_RESVALUE, "v_resvaluep");
b) the load of the absolute address or the value from union changed to
the load of the union value using the offset of the union member
old version
fcinfo - the union member value
v_fcinfo = l_ptr_const(fcinfo, l_ptr(g->StructFunctionCallInfoData));
new version
v_fcinfo = l_load_member_value_by_offset(b,lc, v_op,
l_ptr(g->StructFunctionCallInfoData), offsetof(ExprEvalStep,
d.func.fcinfo_data ));
c) every cached plan has own LLVM-context and 1 module in it. As result,
some functions require context parameter to correctly address it instead
of the global llvm_context variable.
d) llvm_types moved from global variables to the struct LLVMJitTypes.
Not cached queries use old setup, but all types definitions are stored
in global struct LLVMJitTypes llvm_jit_types_struct, instead of many
global variables. Chached queries allocate struct LLVMJitTypes during
query
executon, and pfree it when cached plan is deallocated. llvm_types
module is
read for every cached statement (not 1 time during global context
creation).
e) llvm_function_reference still uses l_ptr_const with the absolute
address
of the function, it needs to be checked, as the function can change
address.
f) new GUC variabe jit_cached is added, off by default.
2. Changes in cached plan.
To store cached context I use struct PlannedStmt (in ExprState
state->parent->state->es_plannedstmt).
New member struct CachedJitContext *jit_context added to store
information
about jit context. In new plans this field in NULL. If plan is stored to
cache,
it filled by CACHED_JITCONTEXT_EMPTY and replaced by actual jit context
pointer
in llvmjit_expr.c .
When cached plan is deallocated, this member used to free jit resources
and
memory.
3. Current problems.
When module is compiled, it needs to connect ExprState and function
addresses.
In old implementation funcname is stored in ExprState.
In cached implementation the fuction will be called second and more
times,
and calling code does not have information about function name for
ExprState.
a) The current cached implementation uses the number of the call to
connect
f.e. 1st expresion and 1st function. It does not work for queries, that
generates an expression after module compilation (f.e. HashJoin) and
tries
to compile it.
b) Also query with aggregate functions generates an expresion every
time, when
executed. It work with current approach, but these expressions should be
considered, as they have different expression address every time.
One of the solution for a) and b) - generate jit-code for Expr pointers
only
in cached plan. Every new created expression either should be ignored by
the compiler and executed by the standard interpreter, or compiled in
the
standard (not cached) jit (it will run compilation every query run and
eliminate all gain from jit-cache).
c) new jit-code generation (use the stuct member instead of the direct
absolute address) slightly decreases the jit-code performance. It is
possible
to compile old version for not cached queries, and new code for cached
queries.
In this case two big 3000 lines funtions llvm_compile_expr() need to be
maintained in similar way, when new expresiions or features are added.
Attached files have
1) the patch (branched from 83ea6c54025bea67bcd4949a6d58d3fc11c3e21b
master),
2 and 3) benchmark files jitinit.sql to create jitbench database and
bash
script jitbench.sh (change to own user and password if you need) to run
banchmark.
4) chart for this benchmark and the query in the benchmark (comparison
with jit=off as 1 unit). It is easy to find query, where jit is higher
or
lower than jit-off. Here I demonstate the difference of standard and new
jit-code (the decrease of the performance with compilation without
optimization), and high gain of cached version with optimization and
high lost of not cached version with optimization due to the running
of the optimization for every the query.
--
Best regards,
Vladlen Popolitov.
Dear colleagues,
It is updated patch (compiled with current master, the same
description, as
below, no other features yet)
If you need to patch the same master version, you can:
git checkout 83ea6c54025bea67bcd4949a6d58d3fc11c3e21b
patch -p1 <v5-0001-jit-saved-for-cached-plans.patch
Vladlen Popolitov писал(а) 2025-02-13 16:01:
Dear colleagues,I implemented the patch to store in the plan cache an already compiled
jit
code. If a query is executed many times, the query time can be
decreased by
20-30% for long queries with big share of expressions in the code.This patch can be applied to the current master and passes tests, but
it is
only the Proof of the Concept, and I need to get opinions and advice
for nexts
steps. It is not ready for commitfest, does not cover all scenarios
(craches
in some sutuation that need special solution), and source code is not
clean.The code source has removed old code marked by // comments to easier
compare,
what was changed, especially in llvmjit_expr.c and llvmjit_deform.cImplementation details.
1. Changes in jit-code generation.
a) the load of the absolute address (as const) changed to the load of
this
address from a struct member:old version:
v_resvaluep = l_ptr_const(op->resvalue, l_ptr(TypeSizeT));
new version
v_resvaluep = l_load_struct_gep( b, g->StructExprEvalStep, v_op,
FIELDNO_EXPREVALSTEP_RESVALUE, "v_resvaluep");b) the load of the absolute address or the value from union changed to
the load of the union value using the offset of the union memberold version
fcinfo - the union member value
v_fcinfo = l_ptr_const(fcinfo, l_ptr(g->StructFunctionCallInfoData));
new version
v_fcinfo = l_load_member_value_by_offset(b,lc, v_op,
l_ptr(g->StructFunctionCallInfoData), offsetof(ExprEvalStep,
d.func.fcinfo_data ));c) every cached plan has own LLVM-context and 1 module in it. As
result,
some functions require context parameter to correctly address it
instead
of the global llvm_context variable.d) llvm_types moved from global variables to the struct LLVMJitTypes.
Not cached queries use old setup, but all types definitions are stored
in global struct LLVMJitTypes llvm_jit_types_struct, instead of many
global variables. Chached queries allocate struct LLVMJitTypes during
query
executon, and pfree it when cached plan is deallocated. llvm_types
module is
read for every cached statement (not 1 time during global context
creation).e) llvm_function_reference still uses l_ptr_const with the absolute
address
of the function, it needs to be checked, as the function can change
address.f) new GUC variabe jit_cached is added, off by default.
2. Changes in cached plan.
To store cached context I use struct PlannedStmt (in ExprState
state->parent->state->es_plannedstmt).
New member struct CachedJitContext *jit_context added to store
information
about jit context. In new plans this field in NULL. If plan is stored
to cache,
it filled by CACHED_JITCONTEXT_EMPTY and replaced by actual jit context
pointer
in llvmjit_expr.c .
When cached plan is deallocated, this member used to free jit resources
and
memory.3. Current problems.
When module is compiled, it needs to connect ExprState and function
addresses.
In old implementation funcname is stored in ExprState.
In cached implementation the fuction will be called second and more
times,
and calling code does not have information about function name for
ExprState.
a) The current cached implementation uses the number of the call to
connect
f.e. 1st expresion and 1st function. It does not work for queries, that
generates an expression after module compilation (f.e. HashJoin) and
tries
to compile it.
b) Also query with aggregate functions generates an expresion every
time, when
executed. It work with current approach, but these expressions should
be
considered, as they have different expression address every time.One of the solution for a) and b) - generate jit-code for Expr pointers
only
in cached plan. Every new created expression either should be ignored
by
the compiler and executed by the standard interpreter, or compiled in
the
standard (not cached) jit (it will run compilation every query run and
eliminate all gain from jit-cache).c) new jit-code generation (use the stuct member instead of the direct
absolute address) slightly decreases the jit-code performance. It is
possible
to compile old version for not cached queries, and new code for cached
queries.
In this case two big 3000 lines funtions llvm_compile_expr() need to be
maintained in similar way, when new expresiions or features are added.Attached files have
1) the patch (branched from 83ea6c54025bea67bcd4949a6d58d3fc11c3e21b
master),
2 and 3) benchmark files jitinit.sql to create jitbench database and
bash
script jitbench.sh (change to own user and password if you need) to run
banchmark.
4) chart for this benchmark and the query in the benchmark (comparison
with jit=off as 1 unit). It is easy to find query, where jit is higher
or
lower than jit-off. Here I demonstate the difference of standard and
new
jit-code (the decrease of the performance with compilation without
optimization), and high gain of cached version with optimization and
high lost of not cached version with optimization due to the running
of the optimization for every the query.
--
Best regards,
Vladlen Popolitov.
Attachments:
v5-0001-jit-saved-for-cached-plans.patchtext/x-diff; name=v5-0001-jit-saved-for-cached-plans.patchDownload+1035-557
Vladlen Popolitov писал(а) 2025-02-13 21:49:
Dear colleagues,
Updated patch rebased to the current master. Also I resolved the
problems
with the lookup of the compiled expressions.
Cached jit compiles only expressions from cached plan - they are
recognized by memory context - if Expr is not NULL and belong to the
same
memory context as cached plan, this expression is compiled to the
function
with expression address in the name (expression address casted to Size
type).
All other expression (generated later than plan, f.e. expressions in
aggregates, hash join, hash aggregates) are not compiled and are
executed
by standard expression interpreter.
This version is more stable comparing to previous, make check passed
with SET jit_cached=on.
P.S. this patch is not for commit fest, it is for discussion of the
cached
jit feature.
Dear colleagues,
It is updated patch (compiled with current master, the same
description, as
below, no other features yet)If you need to patch the same master version, you can:
git checkout 83ea6c54025bea67bcd4949a6d58d3fc11c3e21b
patch -p1 <v5-0001-jit-saved-for-cached-plans.patchVladlen Popolitov писал(а) 2025-02-13 16:01:
Dear colleagues,I implemented the patch to store in the plan cache an already compiled
jit
code. If a query is executed many times, the query time can be
decreased by
20-30% for long queries with big share of expressions in the code.This patch can be applied to the current master and passes tests, but
it is
only the Proof of the Concept, and I need to get opinions and advice
for nexts
steps. It is not ready for commitfest, does not cover all scenarios
(craches
in some sutuation that need special solution), and source code is not
clean.The code source has removed old code marked by // comments to easier
compare,
what was changed, especially in llvmjit_expr.c and llvmjit_deform.cImplementation details.
1. Changes in jit-code generation.
a) the load of the absolute address (as const) changed to the load of
this
address from a struct member:old version:
v_resvaluep = l_ptr_const(op->resvalue, l_ptr(TypeSizeT));
new version
v_resvaluep = l_load_struct_gep( b, g->StructExprEvalStep, v_op,
FIELDNO_EXPREVALSTEP_RESVALUE, "v_resvaluep");b) the load of the absolute address or the value from union changed to
the load of the union value using the offset of the union memberold version
fcinfo - the union member value
v_fcinfo = l_ptr_const(fcinfo, l_ptr(g->StructFunctionCallInfoData));
new version
v_fcinfo = l_load_member_value_by_offset(b,lc, v_op,
l_ptr(g->StructFunctionCallInfoData), offsetof(ExprEvalStep,
d.func.fcinfo_data ));c) every cached plan has own LLVM-context and 1 module in it. As
result,
some functions require context parameter to correctly address it
instead
of the global llvm_context variable.d) llvm_types moved from global variables to the struct LLVMJitTypes.
Not cached queries use old setup, but all types definitions are stored
in global struct LLVMJitTypes llvm_jit_types_struct, instead of many
global variables. Chached queries allocate struct LLVMJitTypes during
query
executon, and pfree it when cached plan is deallocated. llvm_types
module is
read for every cached statement (not 1 time during global context
creation).e) llvm_function_reference still uses l_ptr_const with the absolute
address
of the function, it needs to be checked, as the function can change
address.f) new GUC variabe jit_cached is added, off by default.
2. Changes in cached plan.
To store cached context I use struct PlannedStmt (in ExprState
state->parent->state->es_plannedstmt).
New member struct CachedJitContext *jit_context added to store
information
about jit context. In new plans this field in NULL. If plan is stored
to cache,
it filled by CACHED_JITCONTEXT_EMPTY and replaced by actual jit
context pointer
in llvmjit_expr.c .
When cached plan is deallocated, this member used to free jit
resources and
memory.3. Current problems.
When module is compiled, it needs to connect ExprState and function
addresses.
In old implementation funcname is stored in ExprState.
In cached implementation the fuction will be called second and more
times,
and calling code does not have information about function name for
ExprState.
a) The current cached implementation uses the number of the call to
connect
f.e. 1st expresion and 1st function. It does not work for queries,
that
generates an expression after module compilation (f.e. HashJoin) and
tries
to compile it.
b) Also query with aggregate functions generates an expresion every
time, when
executed. It work with current approach, but these expressions should
be
considered, as they have different expression address every time.One of the solution for a) and b) - generate jit-code for Expr
pointers only
in cached plan. Every new created expression either should be ignored
by
the compiler and executed by the standard interpreter, or compiled in
the
standard (not cached) jit (it will run compilation every query run and
eliminate all gain from jit-cache).c) new jit-code generation (use the stuct member instead of the direct
absolute address) slightly decreases the jit-code performance. It is
possible
to compile old version for not cached queries, and new code for cached
queries.
In this case two big 3000 lines funtions llvm_compile_expr() need to
be
maintained in similar way, when new expresiions or features are added.Attached files have
1) the patch (branched from 83ea6c54025bea67bcd4949a6d58d3fc11c3e21b
master),
2 and 3) benchmark files jitinit.sql to create jitbench database and
bash
script jitbench.sh (change to own user and password if you need) to
run
banchmark.
4) chart for this benchmark and the query in the benchmark (comparison
with jit=off as 1 unit). It is easy to find query, where jit is higher
or
lower than jit-off. Here I demonstate the difference of standard and
new
jit-code (the decrease of the performance with compilation without
optimization), and high gain of cached version with optimization and
high lost of not cached version with optimization due to the running
of the optimization for every the query.
--
Best regards,
Vladlen Popolitov.
Hi,
I've spent some time learning more about jit in the last few weeks and I
think that this patch could be very useful, thanks for working on this!
I'm new on this subject but I would like to share some thoughts about
it.
1. Changes in jit-code generation.
a) the load of the absolute address (as const) changed to the load of this
address from a struct member:
If I understood correctly, this change is required to avoid making the
jit code points to a memory address that was reallocated (making it
invalid)? If that's the case, would be possible to split this change
into a separated patch? It would help a lot to review.
It's not clear to me what's the difference between jit_context being set
to NULL or CACHED_JITCONTEXT_EMPTY and how it changes from NULL to
CACHED_JITCONTEXT_EMPTY. I think some code comments will help on this,
specially on llvm_compile_expr around if/else blocks.
Updated patch rebased to the current master. Also I resolved the problems
with the lookup of the compiled expressions.
Cached jit compiles only expressions from cached plan - they are
recognized by memory context - if Expr is not NULL and belong to the same
memory context as cached plan, this expression is compiled to the function
with expression address in the name (expression address casted to Size
type).
All other expression (generated later than plan, f.e. expressions in
aggregates, hash join, hash aggregates) are not compiled and are executed
by standard expression interpreter.
What's stopping us from going back to the current code generation
without caching on these scenarios? I'm a bit concerned for never jit
compile these kind of expressions and have some kind of performance
issue.
What's the relation of this exec time generated expressions (hash join,
hash agg) problem with the function name lookup issue? It's seems
different problems to me but I may be missing something.
If these problems is not fully correlated I think that it would be
better to have some kind of hash map or use the number of the call to
connect expressions with functions (as you did on v4) to lookup the
cached compiled function. IMHO it sounds a bit strange to me to add the
function pointer on the function name and have this memory context
validation.
I've also executed make check-world and it seems that
test_misc/003_check_guc is falling:
[11:29:42.995](1.153s) not ok 1 - no parameters missing from
postgresql.conf.sample
[11:29:42.995](0.000s) # Failed test 'no parameters missing from
postgresql.conf.sample'
# at src/test/modules/test_misc/t/003_check_guc.pl line 85.
[11:29:42.995](0.000s) # got: '1'
# expected: '0'
[11:29:42.995](0.000s) ok 2 - no parameters missing from guc_tables.c
[11:29:42.995](0.000s) ok 3 - no parameters marked as NOT_IN_SAMPLE in
postgresql.conf.sample
found GUC jit_cached in guc_tables.c, missing from postgresql.conf.sample
--
Matheus Alcantara
Matheus Alcantara писал(а) 2025-03-07 05:02:
Hi!
Thank for interest to the patch.
1. Changes in jit-code generation.
a) the load of the absolute address (as const) changed to the load of
this
address from a struct member:If I understood correctly, this change is required to avoid making the
jit code points to a memory address that was reallocated (making it
invalid)? If that's the case, would be possible to split this change
into a separated patch? It would help a lot to review.
Yes, you correct. In this patch the value of the pointers initialised
every time, when code called for new structure, to reffer to new
address.
I did not put it into separate patch, because now I see all changes
in the system are interconnected and require to change many files.
It is hard to maintain, at least now.
Also I did it to check the possibility to implement it and check, is it
really better. During implementation I understood, that the design must
be changed and clarified, it looks dirty now.
It's not clear to me what's the difference between jit_context being
set
to NULL or CACHED_JITCONTEXT_EMPTY and how it changes from NULL to
CACHED_JITCONTEXT_EMPTY. I think some code comments will help on this,
specially on llvm_compile_expr around if/else blocks.
When llvm_compile_expr is called, I need to know, is it compilation for
cache or usual compilation. I use jit_context for this. It is always
initialised by NULL in palloc0(). If plan is saved to cache,
jit_context assigned CACHED_JITCONTEXT_EMPTY - it means, jit also must
me saved in cache, but not saved yet.
Updated patch rebased to the current master. Also I resolved the
problems
with the lookup of the compiled expressions.
Cached jit compiles only expressions from cached plan - they are
recognized by memory context - if Expr is not NULL and belong to the
same
memory context as cached plan, this expression is compiled to the
function
with expression address in the name (expression address casted to Size
type).
All other expression (generated later than plan, f.e. expressions in
aggregates, hash join, hash aggregates) are not compiled and are
executed
by standard expression interpreter.What's stopping us from going back to the current code generation
without caching on these scenarios? I'm a bit concerned for never jit
compile these kind of expressions and have some kind of performance
issue.
I thought about this. The reason to store the jit in cache - avoid
compiling every time when query is executed. If we anyway compile
the code even for 1 function, we lose benefits of cached jit code.
We can choose the standard jit compilation for other functions.
What's the relation of this exec time generated expressions (hash join,
hash agg) problem with the function name lookup issue? It's seems
different problems to me but I may be missing something.
This question and next question are correlated.
If these problems is not fully correlated I think that it would be
better to have some kind of hash map or use the number of the call to
connect expressions with functions (as you did on v4) to lookup the
cached compiled function. IMHO it sounds a bit strange to me to add the
function pointer on the function name and have this memory context
validation.
This function lookup the biggest challenge in this patch, I see
it correct.
In current implementation: the function is generated for expression,
the name of the function is saved in the ExprState,
then function is compiled (when first expression is executed),
then the function is called,
lookup by saved address is done to find compiled code,
the compiled code address is saved in ExprState as new execution code,
the compiled code is called.
In cached implementation when expression is called the next time,
we need to find the link among the expression and the compiled code.
I tried to find reliable way to connect them, and see now only one
version - make compilation only for expressions saved with the plan -
they have the same address across the time and the same memory context
as the saved plan. Expressions generated during execution like
aggregate expression and Hash join expression are allocated in
query execution context and always have different addresses, and I did
not find any way to connect them with cached plan. The have "expr"
member, but it can be NULL, T_List or T_Expr (or anything, what
Custom node can assign to it).
Function name with expression address now looks as reliable way to find
compiled function, though not elegant. Standard implementation just
stores
generated name, uses it and pfree() it.
I've also executed make check-world and it seems that
test_misc/003_check_guc is falling:[11:29:42.995](1.153s) not ok 1 - no parameters missing from
postgresql.conf.sample
[11:29:42.995](0.000s) # Failed test 'no parameters missing from
postgresql.conf.sample'
# at src/test/modules/test_misc/t/003_check_guc.pl line 85.
[11:29:42.995](0.000s) # got: '1'
# expected: '0'
[11:29:42.995](0.000s) ok 2 - no parameters missing from guc_tables.c
[11:29:42.995](0.000s) ok 3 - no parameters marked as NOT_IN_SAMPLE in
postgresql.conf.sample
found GUC jit_cached in guc_tables.c, missing from
postgresql.conf.sample
I tested in with make check and make installcheck . In v8 I found bugs,
but
not published fix yet.
--
Best regards,
Vladlen Popolitov.
On Fri, Mar 7, 2025 at 7:43 AM Vladlen Popolitov
<v.popolitov@postgrespro.ru> wrote:
Updated patch rebased to the current master. Also I resolved the
problems
with the lookup of the compiled expressions.
Cached jit compiles only expressions from cached plan - they are
recognized by memory context - if Expr is not NULL and belong to the
same
memory context as cached plan, this expression is compiled to the
function
with expression address in the name (expression address casted to Size
type).
All other expression (generated later than plan, f.e. expressions in
aggregates, hash join, hash aggregates) are not compiled and are
executed
by standard expression interpreter.What's stopping us from going back to the current code generation
without caching on these scenarios? I'm a bit concerned for never jit
compile these kind of expressions and have some kind of performance
issue.I thought about this. The reason to store the jit in cache - avoid
compiling every time when query is executed. If we anyway compile
the code even for 1 function, we lose benefits of cached jit code.
We can choose the standard jit compilation for other functions.
I don't think that I get your point here - Why we would lose the
benefits? I think that I may not be clear with my question; My point
was, why we can't jit compile, without caching, these expressions that
are generated later on the plan? Like aggregates, hash join, as you
mention. I think that we may have scenarios that jit compile is worth
even if we will not cache the compiled code. I understand the limitation
of runtime generated ExprState being allocated in another memory
context, but why can't we follow the same path as we have today for
these scenarios?
What's the relation of this exec time generated expressions (hash join,
hash agg) problem with the function name lookup issue? It's seems
different problems to me but I may be missing something.This question and next question are correlated.
If these problems is not fully correlated I think that it would be
better to have some kind of hash map or use the number of the call to
connect expressions with functions (as you did on v4) to lookup the
cached compiled function. IMHO it sounds a bit strange to me to add the
function pointer on the function name and have this memory context
validation.This function lookup the biggest challenge in this patch, I see
it correct.
In current implementation: the function is generated for expression,
the name of the function is saved in the ExprState,
then function is compiled (when first expression is executed),
then the function is called,
lookup by saved address is done to find compiled code,
the compiled code address is saved in ExprState as new execution code,
the compiled code is called.In cached implementation when expression is called the next time,
we need to find the link among the expression and the compiled code.
I tried to find reliable way to connect them, and see now only one
version - make compilation only for expressions saved with the plan -
they have the same address across the time and the same memory context
as the saved plan. Expressions generated during execution like
aggregate expression and Hash join expression are allocated in
query execution context and always have different addresses, and I did
not find any way to connect them with cached plan. The have "expr"
member, but it can be NULL, T_List or T_Expr (or anything, what
Custom node can assign to it).
Function name with expression address now looks as reliable way to find
compiled function, though not elegant. Standard implementation just
stores
generated name, uses it and pfree() it.
Yeah, I see that this sounds a bit complicated. I'm wondering if this
logic of checking if the expr is already cached or not could be
implemented in another function or layer (maybe jit_compile_expr?). I
think that llvm_compile_expr is already complicated enough and it should
only care about emitting code, so we could have another layer that check
if the expr is already compiled or not and only call llvm_compile_expr
if it's not compiled yet, WYT?
I've also executed make check-world and it seems that
test_misc/003_check_guc is falling:[11:29:42.995](1.153s) not ok 1 - no parameters missing from
postgresql.conf.sample
[11:29:42.995](0.000s) # Failed test 'no parameters missing from
postgresql.conf.sample'
# at src/test/modules/test_misc/t/003_check_guc.pl line 85.
[11:29:42.995](0.000s) # got: '1'
# expected: '0'
[11:29:42.995](0.000s) ok 2 - no parameters missing from guc_tables.c
[11:29:42.995](0.000s) ok 3 - no parameters marked as NOT_IN_SAMPLE in
postgresql.conf.sample
found GUC jit_cached in guc_tables.c, missing from
postgresql.conf.sampleI tested in with make check and make installcheck . In v8 I found bugs,
but not published fix yet.
Do you have any intent to work on a new version for this?
--
Matheus Alcantara
Matheus Alcantara писал(а) 2025-03-18 21:56:
On Fri, Mar 7, 2025 at 7:43 AM Vladlen Popolitov
<v.popolitov@postgrespro.ru> wrote:Updated patch rebased to the current master. Also I resolved the
problems
with the lookup of the compiled expressions.
Cached jit compiles only expressions from cached plan - they are
recognized by memory context - if Expr is not NULL and belong to the
same
memory context as cached plan, this expression is compiled to the
function
with expression address in the name (expression address casted to Size
type).
All other expression (generated later than plan, f.e. expressions in
aggregates, hash join, hash aggregates) are not compiled and are
executed
by standard expression interpreter.What's stopping us from going back to the current code generation
without caching on these scenarios? I'm a bit concerned for never jit
compile these kind of expressions and have some kind of performance
issue.I thought about this. The reason to store the jit in cache - avoid
compiling every time when query is executed. If we anyway compile
the code even for 1 function, we lose benefits of cached jit code.
We can choose the standard jit compilation for other functions.I don't think that I get your point here - Why we would lose the
benefits? I think that I may not be clear with my question; My point
was, why we can't jit compile, without caching, these expressions that
are generated later on the plan? Like aggregates, hash join, as you
mention. I think that we may have scenarios that jit compile is worth
even if we will not cache the compiled code. I understand the
limitation
of runtime generated ExprState being allocated in another memory
context, but why can't we follow the same path as we have today for
these scenarios?
Yes, I understood. It is possible to make compilation of the newly
created expressions. I tried to avoid additional compilation and chose
the implementation without compilation.
What's the relation of this exec time generated expressions (hash join,
hash agg) problem with the function name lookup issue? It's seems
different problems to me but I may be missing something.This question and next question are correlated.
If these problems is not fully correlated I think that it would be
better to have some kind of hash map or use the number of the call to
connect expressions with functions (as you did on v4) to lookup the
cached compiled function. IMHO it sounds a bit strange to me to add the
function pointer on the function name and have this memory context
validation.This function lookup the biggest challenge in this patch, I see
it correct.
In current implementation: the function is generated for expression,
the name of the function is saved in the ExprState,
then function is compiled (when first expression is executed),
then the function is called,
lookup by saved address is done to find compiled code,
the compiled code address is saved in ExprState as new execution code,
the compiled code is called.In cached implementation when expression is called the next time,
we need to find the link among the expression and the compiled code.
I tried to find reliable way to connect them, and see now only one
version - make compilation only for expressions saved with the plan -
they have the same address across the time and the same memory context
as the saved plan. Expressions generated during execution like
aggregate expression and Hash join expression are allocated in
query execution context and always have different addresses, and I did
not find any way to connect them with cached plan. The have "expr"
member, but it can be NULL, T_List or T_Expr (or anything, what
Custom node can assign to it).
Function name with expression address now looks as reliable way to
find
compiled function, though not elegant. Standard implementation just
stores
generated name, uses it and pfree() it.Yeah, I see that this sounds a bit complicated. I'm wondering if this
logic of checking if the expr is already cached or not could be
implemented in another function or layer (maybe jit_compile_expr?). I
think that llvm_compile_expr is already complicated enough and it
should
only care about emitting code, so we could have another layer that
check
if the expr is already compiled or not and only call llvm_compile_expr
if it's not compiled yet, WYT?
I agree, it requires cleaning of the code. I am going to do it on the
next
step.
I've also executed make check-world and it seems that
test_misc/003_check_guc is falling:[11:29:42.995](1.153s) not ok 1 - no parameters missing from
postgresql.conf.sample
[11:29:42.995](0.000s) # Failed test 'no parameters missing from
postgresql.conf.sample'
# at src/test/modules/test_misc/t/003_check_guc.pl line 85.
[11:29:42.995](0.000s) # got: '1'
# expected: '0'
[11:29:42.995](0.000s) ok 2 - no parameters missing from guc_tables.c
[11:29:42.995](0.000s) ok 3 - no parameters marked as NOT_IN_SAMPLE in
postgresql.conf.sample
found GUC jit_cached in guc_tables.c, missing from
postgresql.conf.sampleI tested in with make check and make installcheck . In v8 I found
bugs,
but not published fix yet.
Do you have any intent to work on a new version for this?
I am preparing the new version. I got comments with bugs and memory
leaks,
I am fixing and testing the code.
I run TPC-H benchmark and found the degradation of performance with
jit_cached (and with regular jit also in some queries). As I see,
parallel execution of the query creates a parallel worker, that starts,
gets the plan, compile all expressions again. Even if query has cached
jit code, all parallel workers compile code again.
By the way, it is possible to send to the worker compiled jit code
together with plan (shared memory is used), but again arise the problem,
how to find the link between expression addresses in parallel worker
address space and jit code in shared memory.
--
Best regards,
Vladlen Popolitov.