Skip to content

Fix jit - #2403

Open
secwall wants to merge 33 commits into
apache:mainfrom
secwall:fix-jit
Open

Fix jit#2403
secwall wants to merge 33 commits into
apache:mainfrom
secwall:fix-jit

Conversation

@secwall

@secwall secwall commented Nov 23, 2026

Copy link
Copy Markdown

What does this PR do?

This is a follow up for the PR #2278. That PR targeted REL_2_STABLE branch. This one targets main.

Type of Change

  • Bug fix (non-breaking change)
  • New feature (non-breaking change)
  • Breaking change (fix or feature with breaking changes)
  • Documentation update

Test Plan

  • Unit tests added/updated
  • Integration tests added/updated
  • Passed make installcheck
  • Passed make -C src/test installcheck-cbdb-parallel

Impact

Performance:
Default build path doesn't seem to be impacted.

User-facing changes:
Build with --with-llvm becomes possible.

Checklist

branches: [main]
pull_request:
branches: [main]
types: [opened, synchronize, reopened, edited]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
types: [opened, synchronize, reopened, edited]
types: [opened, synchronize, reopened]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I've applied the suggested change.


on:
push:
branches: [main]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
branches: [main]
branches: [REL_2_STABLE]

The way is like #2384. We can run this test only for the PRs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied.

Comment on lines +32 to +34
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: false
concurrency:
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && format('pr-{0}', github.event.pull_request.number) || format('run-{0}', github.run_id) }}
cancel-in-progress: ${{ github.event_name == 'pull_request' }}

Also like the way in #2352 to save the CI runner resources.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I've applied the suggested change.

Comment on lines +103 to +122
- name: Free Disk Space
run: |
echo "=== Disk space before cleanup ==="
df -h /
# Remove pre-installed tools from host to free disk space
rm -rf /host_opt/hostedtoolcache || true
rm -rf /host_usr_local/lib/android || true
rm -rf /host_usr_share/dotnet || true
rm -rf /host_opt/ghc || true
rm -rf /host_usr_local/.ghcup || true
rm -rf /host_usr_share/swift || true
rm -rf /host_usr_local/share/powershell || true
rm -rf /host_usr_local/share/chromium || true
rm -rf /host_usr_share/miniconda || true
rm -rf /host_opt/az || true
rm -rf /host_usr_share/sbt || true
echo "=== Disk space after cleanup ==="
df -h /

@tuhaihe tuhaihe Nov 24, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We plan to remove this step from the general workflow. I believe that we will have sufficient disk space to run this workflow. So please remove this step too.

We also want to save more CI runner resources.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I've dropped this step.

@tuhaihe tuhaihe left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for your work!

Please resolve the comments. Let's work together to save more CI runner resources. You don't need to change the workflow file for REL_2_STABLE.

secwall and others added 23 commits November 24, 2026 13:15
Cloudberry with --with-llvm build fails.

Three independent problems:

* Six arms branch to opblocks[i + 1].  PostgreSQL 12 renamed the loop
  variable to opno, there is no `i` in scope, and every stock arm
  around them uses opblocks[opno + 1].

* Both EEOP_SCALARARRAYOP_FAST_* arms call

      build_EvalXFunc(b, mod, fn, v_state, v_econtext, op)

  but the macro is build_EvalXFunc(b, mod, funcname, v_state, op, ...),
  so v_econtext lands in the op parameter and op is passed as a
  variadic LLVMValueRef.  ExecEvalScalarArrayOpFastInt() and
  ExecEvalScalarArrayOpFastStr() take (ExprState *, ExprEvalStep *), so
  the extra argument is dropped, as in the adjacent
  EEOP_HASHED_SCALARARRAYOP arm.

* The arms predate the LLVM 16-18 support taken for 2.2.0 (PostgreSQL
  82d9a782a29 and the JIT commits after it, through 14.11).  Since then
  l_int64_const() takes an LLVMContextRef, and LLVMBuildLoad(), the
  typed-pointer call, is gone from LLVM 18's C API.  EEOP_ROWIDEXPR
  also built its pointer type with LLVMInt64Type(), from LLVM's global
  context, while the module is built in the JIT's own.  Use l_load()
  with the loaded type, l_int64_const(lc, ...) and
  LLVMInt64TypeInContext(lc), as the stock arms do.

Before the fix, with LLVM 18:

  error: 'i' undeclared (first use in this function)
  error: passing argument 5 of 'build_EvalXFuncInt' from incompatible pointer type
  error: implicit declaration of function 'LLVMBuildLoad'
  error: too few arguments to function 'l_int64_const'

(cherry picked from commit 0b17420)
llvm_pg_func() finds the signature of a backend function called from
generated code in llvmjit_types.bc, which is built from the
referenced_functions[] array in llvmjit_types.c.
ExecEvalScalarArrayOpFastInt() and ExecEvalScalarArrayOpFastStr() are
not listed, so once the JIT compiles, every expression that takes the
ScalarArrayOp fast path fails at run time with

  ERROR:  function ExecEvalScalarArrayOpFastInt not in llvmjit_types.c

ExecInitScalarArrayOpFastPath() chooses that path for "x IN (constant
list)" with int2, int4, int8, date, text or bpchar equality, so with
JIT enabled a large share of IN-list queries would fail.

(cherry picked from commit 67f745e)
EEOP_GROUP_ID, EEOP_GROUPING_SET_ID and EEOP_AGGEXPR_ID copy
AggState.group_id, AggState.gset_id and TupleSplitState.currentExprId
into the step's result. All three fields are 32 bits wide (int, int
and Index), but the JIT arms load them as a whole Datum, so the upper
half of the result holds whatever follows the field in memory -- for
group_id, that is gset_id. The interpreter returns Int32GetDatum() of
the field.

DatumGetInt32() reads the low half, so on little-endian machines the
SQL-visible int4 comes out right; but the Datum differs from the one
the interpreter produces, and anything that compares or hashes whole
Datums (datum_image_eq(), datum_image_hash(), by-value datumIsEqual())
sees two different values for the same group. On a big-endian machine
the value itself would be wrong.

Load 32 bits and sign-extend, which is what Int32GetDatum() does.

(cherry picked from commit f45f7eb)
Greenplum's merge with PostgreSQL 12 gave ExecEvalCurrentOfExpr() a
third parameter, the ExprContext, which it needs to find the cursor
position dispatched with the query; the interpreter has called it as
ExecEvalCurrentOfExpr(state, op, econtext) ever since. The JIT's
EEOP_CURRENTOFEXPR arm still emits the stock two-argument call, and
build_EvalXFunc() checks the arity against the function's real type, so
compiling any WHERE CURRENT OF qualifier fails with

    ERROR:  parameter mismatch: ExecEvalCurrentOfExpr expects 3 passed 2

With jit_above_cost=0 the regression suite's portals_updatable test
shows it on four statements: two DELETEs that should fail with a
different error, and two UPDATEs that should succeed.

Pass v_econtext, as the interpreter does.

(cherry picked from commit bd51870)
The backported SectionMemoryManager makes jit/llvmjit.h include
<llvm-c/OrcEE.h> on aarch64. That includes <llvm-c/TargetMachine.h>,
which declares

    void LLVMAddAnalysisPasses(LLVMTargetMachineRef T, LLVMPassManagerRef PM);

Unlike PostgreSQL's, Cloudberry's access/xact.h still includes
utils/datetime.h, which defines PM as 1, and llvmjit_expr.c includes
xact.h (via access/nbtree.h and access/tableam.h) before llvmjit.h.  So
on aarch64 with --with-llvm the build stops at:

    utils/datetime.h:73:25: error: expected ';', ',' or ')' before numeric constant
    llvm-c/TargetMachine.h:236:71: note: in expansion of macro 'PM'

Undefine PM before including OrcEE.h, as llvmjit_wrap.cpp and
llvmjit_inline.cpp already undefine Min before LLVM's C++ headers.

(cherry picked from commit 40dac8c)
configure derives PG_MAJORVERSION from PG_PACKAGE_VERSION, the version
of PostgreSQL that Cloudberry is based on, but PG_MINORVERSION from
PACKAGE_VERSION, which is Cloudberry's own. The two lines came from
different places: the PostgreSQL 12 merge moved the major to
PG_PACKAGE_VERSION, and the minor line arrived from upstream unchanged.

So the minor half of PG_VERSION_NUM, server_version_num and
PG_MINORVERSION_NUM is the last component of Cloudberry's version.
2.2.0-incubating, based on PostgreSQL 14.10, reports PG_VERSION_NUM
140000; a 2.2.1 would report 140001. Meanwhile server_version says
14.10, and clients that parse it (libpq's PQserverVersion()) compute
140010, so the server and its clients disagree about its own version.

That matters to extensions, which gate on PG_VERSION_NUM for behaviour
PostgreSQL changed within a major branch. For example, the LLVM
opaque-pointer support Cloudberry 2.2.0 took from 14.10 is gated in
extensions as PG_VERSION_NUM >= 140010; built against 2.2.0, such an
extension takes its pre-14.10 branch and fails to compile against
LLVM 18.

(cherry picked from commit b0b2ed0)
standard_planner() returns GPORCA's plan as soon as it has one, before the
code that sets PlannedStmt.jitFlags, which only the PostgreSQL planner path
reaches.  So under the default optimizer=on nothing is ever JIT-compiled, at
any jit_above_cost: with jit_above_cost=0, a two-stage aggregate compiles 14
functions on the coordinator when the planner plans it and none when GPORCA
does.

Greenplum fixed this after Cloudberry forked. Port its final state:

  - compute_jit_flags(), the planner's code moved into a function that
    both paths call;
  - GPORCA's costs are on a different scale from the planner's, so its
    plans are compared against their own thresholds,
    optimizer_jit_above_cost, optimizer_jit_inline_above_cost and
    optimizer_jit_optimize_above_cost; "jit" still switches both.

cherry-pick from Greenplum Database, greenplum-db/gpdb-archive @ d9d12fb3465
(Tao Tang), e9ccaf33122 and e575d85c369 (Nishant Sharma):
  src/backend/optimizer/plan/planner.c
  src/backend/utils/misc/guc_gp.c
  src/backend/utils/misc/postgresql.conf.sample
  src/include/utils/guc.h

Three differences:

First, the three thresholds default to -1, where
Greenplum has 7500, 37500 and 37500, chosen there from TPC-H runs.
We didn't measure this properly. So let's avoid enabling this by
default.

Second, the thresholds are listed in unsync_guc_name.h, with every other
optimizer_* setting.  Greenplum moved them to the synced list because an
intermediate version read one on the segments, in provider_init(), and
that read is gone: GPORCA only plans on the coordinator, and segments get
the flags inside the plan.

Third, Greenplum's tests do not apply here: they extend an explain_format
JIT section and gp_explain_jit, neither of which Cloudberry has. So
gp_jit_cost is new. Under either optimizer it checks that only that
optimizer's thresholds decide whether, and how hard, the coordinator
compiles, and that "jit=off" stops both. It reads true in a build
without --with-llvm as well.

(cherry picked from commit 46c36fc)
For many optional libraries, we extract the -L and -l switches needed
to link the library from a helper program such as llvm-config.  In
some cases we put the resulting -L switches into LDFLAGS ahead of
-L switches specified via --with-libraries.  That risks breaking
the user's intention for --with-libraries.

It's not such a problem if the library's -L switch points to a
directory containing only that library, but on some platforms a
library helper may "helpfully" offer a switch such as -L/usr/lib
that points to a directory holding all standard libraries.  If the
user specified --with-libraries in hopes of overriding the standard
build of some library, the -L/usr/lib switch prevents that from
happening since it will come before the user-specified directory.

To fix, avoid inserting these switches directly into LDFLAGS during
configure, instead adding them to LIBDIRS or SHLIB_LINK.  They will
still eventually get added to LDFLAGS, but only after the switches
coming from --with-libraries.

The same problem exists for -I switches: those coming from
--with-includes should appear before any coming from helper programs
such as llvm-config.  We have not heard field complaints about this
case, but it seems certain that a user attempting to override a
standard library could have issues.

The changes for this go well beyond configure itself, however,
because many Makefiles have occasion to manipulate CPPFLAGS to
insert locally-desirable -I switches, and some of them got it wrong.
The correct ordering is any -I switches pointing at within-the-
source-tree-or-build-tree directories, then those from the tree-wide
CPPFLAGS, then those from helper programs.  There were several places
that risked pulling in a system-supplied copy of libpq headers, for
example, instead of the in-tree files.  (Commit cb36f8e fixed one
instance of that a few months ago, but this exercise found more.)

The Meson build scripts may or may not have any comparable problems,
but I'll leave it to someone else to investigate that.

Reported-by: Charles Samborski <demurgos@demurgos.net>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/70f2155f-27ca-4534-b33d-7750e20633d7@demurgos.net
Backpatch-through: 13

Apply the LLVM build hunks to Cloudberry, including the generated configure.

(cherry picked from commit bbc20c8a9ffdefcabd70ea0c325827fe2b8f5960)
LLVM-21 renamed llvm::GlobalValue::getGUID() to
getGUIDAssumingExternalLinkage(), so add a version guard.

Author: Holger Hoffstätte <holger@applied-asynchrony.com>
Discussion: https://www.postgresql.org/message-id/flat/d25e6e4a-d1b4-84d3-2f8a-6c45b975f53d%40applied-asynchrony.com
(cherry picked from commit 2670881afd9175b8946530646eb4fb76aaebd340)
When JIT deformed tuples (controlled via the jit_tuple_deforming GUC),
types narrower than sizeof(Datum) would be zero-extended up to Datum
width.  This wasn't the same as what fetch_att() does in the standard
tuple deforming code.  Logically the values are the same when fetching
via the DatumGet*() marcos, but negative numbers are not the same in
binary form.

In the report, the problem was manifesting itself with:

ERROR: could not find memoization table entry

in a query which had a "Cache Mode: binary" Memoize node. However, it's
currently unclear what else is affected.  Anything that uses
datum_image_eq() or datum_image_hash() on a Datum from a tuple deformed by
JIT could be affected, but it may not be limited to that.

The fix for this is simple: use signed extension instead of zero
extension.

Many thanks to Emmanuel Touzery for reporting this issue and providing
steps and backup which allowed the problem to easily be recreated.

Reported-by: Emmanuel Touzery <emmanuel.touzery@plandela.si>
Author: David Rowley <dgrowleyml@gmail.com>
Discussion: https://postgr.es/m/DB8P194MB08532256D5BAF894F241C06393F3A@DB8P194MB0853.EURP194.PROD.OUTLOOK.COM
Backpatch-through: 13
(cherry picked from commit 3398b0d027852ff1ef4a3be28612a5731f6e52f3)
In 2a0faed, which added JIT compilation support for expressions, I
accidentally used sizeof(LLVMBasicBlockRef *) instead of
sizeof(LLVMBasicBlockRef) as part of computing the size of an allocation. That
turns out to have no real negative consequences due to LLVMBasicBlockRef being
a pointer itself (and thus having the same size). It still is wrong and
confusing, so fix it.

Reported by coverity.

Backpatch-through: 13
(cherry picked from commit 0909a2971ced9a894ecbb4753bc770aa0923c044)
LLVM 21 changed the arguments of RTDyldObjectLinkingLayer's
constructor, breaking compilation with the backported
SectionMemoryManager from commit 9044fc1.

llvm/llvm-project@cd58586

Backpatch-through: 14
Author: Holger Hoffstätte <holger@applied-asynchrony.com>
Reviewed-by: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
Discussion: https://postgr.es/m/d25e6e4a-d1b4-84d3-2f8a-6c45b975f53d%40applied-asynchrony.com
(cherry picked from commit 600acd34b09a7a06c236e503c130ab01c0fb1f5c)
An array of LLVMBasicBlockRef is allocated with the size used for an
element being "LLVMBasicBlockRef *" rather than "LLVMBasicBlockRef".
LLVMBasicBlockRef is a type that refers to a pointer, so this did not
directly cause a problem because both should have the same size, still
it is incorrect.

This issue has been spotted while reviewing a different patch, and
exists since 2a0faed, so backpatch all the way down.

Discussion: https://postgr.es/m/CA+hUKGLngd9cKHtTUuUdEo2eWEgUcZ_EQRbP55MigV2t_zTReg@mail.gmail.com
Backpatch-through: 14
(cherry picked from commit 5a4dc4aabd0345a194d27219f2424eb3dd3bf8fb)
REL_18_STABLE and master have commit ee48591, so they always use the
newer LLVM opaque pointer functions.  Drop -Wno-deprecated-declarations
(commit a56e7b6) for code under jit/llvm in those branches, to catch
any new deprecation warnings that arrive in future version of LLVM.

Older branches continued to use functions marked deprecated in LLVM 14
and 15 (ie switched to the newer functions only for LLVM 16+), as a
precaution against unforeseen compatibility problems with bitcode
already shipped.  In those branches, the comment about warning
suppression is updated to explain that situation better.  In theory we
could suppress warnings only for LLVM 14 and 15 specifically, but that
isn't done here.

Backpatch-through: 14
Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/1407185.1766682319%40sss.pgh.pa.us
(cherry picked from commit dfb9ff5904074976b2a27bc44735a2680303de6d)
jit_profiling_support=true captures profile data for Linux perf.  On
other platforms, LLVMCreatePerfJITEventListener() returns NULL and the
attempt to register the listener would crash.

Fix by ignoring the setting in that case.  The documentation already
says that it only has an effect if perf support is present, and we
already did the same for older LLVM versions that lacked support.

No field reports, unsurprisingly for an obscure developer-oriented
setting.  Noticed in passing while working on commit 1a28b4b4.

Backpatch-through: 14
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
Reviewed-by: Andres Freund <andres@anarazel.de>
Discussion: https://postgr.es/m/CA%2BhUKGJgB6gvrdDohgwLfCwzVQm%3DVMtb9m0vzQn%3DCwWn-kwG9w%40mail.gmail.com
(cherry picked from commit 130b001c15232305531b206d3f14d2ba01105979)
With LLVM >= 17, transform passes are provided as a string to
LLVMRunPasses. Only two strings were used: "default<O3>" and
"default<O0>,mem2reg".

With previous LLVM versions, an additional inline pass was added when
JIT inlining was enabled without optimization. With LLVM >= 17, the code
would go through llvm_inline, prepare the functions for inlining, but
the generated bitcode would be the same due to the missing inline pass.

This patch restores the previous behavior by adding an inline pass when
inlining is enabled but no optimization is done.

This fixes an oversight introduced by 76200e5 when support for LLVM 17
was added.

Backpatch-through: 14
Author: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com>
Reviewed-by: Thomas Munro <thomas.munro@gmail.com>
Reviewed-by: Andreas Karlsson <andreas@proxel.se>
Reviewed-by: Andres Freund <andres@anarazel.de>
Reviewed-by: Álvaro Herrera <alvherre@kurilemu.de>
Reviewed-by: Pierre Ducroquet <p.psql@pinaraf.info>
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com>
Discussion: https://postgr.es/m/CAO6_XqrNjJnbn15ctPv7o4yEAT9fWa-dK15RSyun6QNw9YDtKg%40mail.gmail.com
(cherry picked from commit 7600dc79c231e7fd88172dde6c3a3ba701144298)
Otherwise, this would break if using C and C++ compilers from
different families and they understand different options.  It already
used the right flags for compiling, this is only for linking.  Also,
the meson setup already did this correctly.

Back-patch of v18 commit 365b5a3 into older supported branches.
At the time we were only aware of trouble in v18, but as shown
by buildfarm member siren, older branches can hit the problem too.

Reported-by: Tom Lane <tgl@sss.pgh.pa.us>
Author: Peter Eisentraut <peter@eisentraut.org>
Discussion: https://www.postgresql.org/message-id/228700.1722717983@sss.pgh.pa.us
Discussion: https://postgr.es/m/3109540.1771698685@sss.pgh.pa.us
Backpatch-through: 14-17
(cherry picked from commit b9f7ceabd820369280d4ca6bfaab43cf8345f1bd)
The lifetime.end intrinsic can now only be used for stack memory
allocated with alloca[1][2][3].  We use it to tell LLVM about the
lifetime of function arguments/isnull values that we keep in palloc'd
memory, so that it can avoid spilling registers to memory.

We might need to rearrange things and put them on the stack, but that'll
take some research.  In the meantime, unbreak the build on LLVM 22.

[1] llvm/llvm-project#149310
[2] https://llvm.org/docs/LangRef.html#llvm-lifetime-end-intrinsic
[3] https://llvm.org/docs/LangRef.html#i-alloca

Backpatch-through: 14
Reviewed-by: Matheus Alcantara <matheusssilv97@gmail.com> (earlier attempt)
Reviewed-by: Anthonin Bonnefoy <anthonin.bonnefoy@datadoghq.com> (earlier attempt)
Reviewed-by: Andres Freund <andres@anarazel.de> (earlier attempt)
Discussion: https://postgr.es/m/CA%2BhUKGJTumad75o8Zao-LFseEbt%3DenbUFCM7LZVV%3Dc8yg2i7dg%40mail.gmail.com
(cherry picked from commit 26f27c592c5da7f40efe733204396b15ea905f82)
LLVM 22 has the fix that we copied into our tree in commit 9044fc1 and
a new function to reach it[1][2], so we only need to use our copy for
Aarch64 + LLVM < 22.  The only change to the final version that our copy
didn't get is a new LLVM_ABI macro, but that isn't appropriate for us.
Our copy is hopefully now frozen and would only need maintenance if bugs
are found in the upstream code.

Non-Aarch64 systems now also use the new API with LLVM 22.  It allocates
all sections with one contiguous mmap() instead of one per
section.  We could have done that earlier, but commit 9044fc1 wanted to
limit the blast radius to the affected systems.  We might as well
benefit from that small improvement everywhere now that it is available
out of the box.

We can't delete our copy until LLVM 22 is our minimum supported version,
or we switch to the newer JITLink API for at least Aarch64.

[1] llvm/llvm-project#71968
[2] llvm/llvm-project#174307

Backpatch-through: 14
Discussion: https://postgr.es/m/CA%2BhUKGJTumad75o8Zao-LFseEbt%3DenbUFCM7LZVV%3Dc8yg2i7dg%40mail.gmail.com
(cherry picked from commit 8354cd7ff5752035336f34149f97674c2399d905)
Like 8f1791c61, this fixes a case of implicitly casting away
const by not treating the result of strrchr() on a const pointer
as const.  This was missed at the time because the machines
reporting those warnings weren't building with --with-llvm.

While here, clean up another infelicity: in the probably-
impossible case that the input string contains only one dot,
this function would call pnstrdup() with a length of -1
and thereby emit a module name equal to the function name.
It seems to me we should emit modname = NULL instead.

Also remove a useless Assert and two redundant assignments.

Back-patch, as 8f1791c61 was, so that users of back branches
don't see this warning when building with late-model gcc.

Reported-by: hubert depesz lubaczewski <depesz@depesz.com>
Author: Tom Lane <tgl@sss.pgh.pa.us>
Discussion: https://postgr.es/m/aiGNJ89PBqvq2Yyz@depesz.com
Backpatch-through: 14
(cherry picked from commit 92f26d582632da349a565d340b3c8b8cb6b722be)
The documentation for jit_debugging_support and jit_profiling_support
previously stated that these parameters can only be set at server start.

However, both parameters use the PGC_SU_BACKEND context, meaning they
can be set at session start by superusers or users granted the appropriate
SET privilege, but cannot be changed within an active session.

This commit updates the documentation to reflect the actual behavior.

Backpatch to all supported versions.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Discussion: https://postgr.es/m/CAHGQGwEpMDpB-K8SSUVRRHg6L6z3pLAkekd9aviOS=ns0EC=+Q@mail.gmail.com
Backpatch-through: 14
(cherry picked from commit 8916a4bcc73f289f1ff6ef748a0d70e99cbdc8b6)
When configured --with-llvm, PGXS compiles every object of a module to LLVM
bitcode too, with clang and BITCODE_CXXFLAGS. PG_CXXFLAGS is not passed
there, so neither is this module's -std=c++17, and clang older than 16
defaults to C++14. With the clang 14 of Ubuntu 22.04 the build stops:

  src/log/LogSchema.h:56:23: error: no type named 'string_view' in namespace 'std'
  make[2]: *** [../../src/Makefile.global:1183: src/log/LogOps.bc] Error 1

The bitcode is there for the JIT to inline a module's functions into the
expressions that call them. This module's are administrative set
returning functions, which gain nothing from that.

(cherry picked from commit 7cd12f1)
Both tests print plans with their costs, and make some of them with
enable_* settings off, which costs a plan 10000000000 or more.  That is
above jit_above_cost.  In a server built --with-llvm, where jit is on by
default, EXPLAIN then adds the JIT summary to such a plan:

   JIT:
     Functions: 49
     Options: Inlining true, Optimization true, Expressions true, Deforming true

One plan of cte_prune and five of gporca get it, under the planner.
Built --with-llvm and left at its default settings, these are the only
two of installcheck-good's 718 tests that fail under the planner; under
GPORCA none does.

(cherry picked from commit f261a18)
installcheck-jit runs parallel_schedule and then the new jit_schedule
with the six jit_*_above_cost and optimizer_jit_*_above_cost settings
at 0, so that every plan of either optimizer is compiled, inlined and
optimized. JIT must not change an answer, so the tests keep their
expected output and no new one is added for them. The optimizer comes
from the caller's PGOPTIONS, as for installcheck-good.

Its first test, gp_jit_available, is what makes a pass mean something.
Where the JIT cannot be loaded a query is interpreted and nothing says
so, so on a server built without --with-llvm every other test here
passes. This one fails unless the provider loads on the coordinator
and on every segment, and unless a plan is in fact compiled, inlined
and optimized under the optimizer in use. It is in no other schedule.

Four tests of parallel_schedule are left out. explain, cte_prune and
partition_prune_opfamily_append print plans together with their JIT
summaries or with the settings in force. brin_bloom's plans depend on
when autovacuum analyzes its table; forced JIT makes the test ten times
longer.

(cherry picked from commit 7400412)
ENABLE_LLVM=true now adds --with-llvm, as ENABLE_YEZZEY=true adds
--with-yezzey. It defaults to false: the JIT stays optional, and the
workflows and packages that do not ask for it build as before, without
it.

(cherry picked from commit 683d57d)
build-jit-cloudberry.yml builds with ENABLE_LLVM=true on each of the
five build images the other workflows use, then runs installcheck-jit
with optimizer=off and with optimizer=on, each on a new demo cluster.
It installs LLVM and clang from the distribution in its job, so the
build images stay as they are, and a distribution's move to a newer
LLVM shows here first. A failed run lists its failing tests in the job
summary and uploads their diffs and the cluster's server logs.

(cherry picked from commit 7fa9ac8)
Complete the context release logic from PostgreSQL 2cf5058 and dc0b584. Use the per-query LLVMJitContext consistently, balance the in-use count and fatal-on-OOM section, and verify the count at backend shutdown.
Link the frontend C source into the backend libpq build as for fe-exec and its siblings. Linking its compiled object bypassed the LLVM rule, so make install failed while constructing the postgres bitcode index.
@secwall

secwall commented Nov 24, 2026

Copy link
Copy Markdown
Author

LGTM. Thanks for your work!

Please resolve the comments. Let's work together to save more CI runner resources. You don't need to change the workflow file for REL_2_STABLE.

I've noticed that this PR has the same warnings issues as the previous one. I've tried to account for them. But I'm not sure if I'm moving into right direction. Could you take a look when you have time?

@leborchuk leborchuk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks to your work!

@leborchuk
leborchuk enabled auto-merge (rebase) November 24, 2026 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants