Hi Joao,
Thanks for testing the patch and for the careful notes.
1. Agreed. v2 adds a comment above the second test: it was rejected
before the fix too, and it guards the reverse match. The code and the
other tests do not change.
2. I checked this. The stored jsonpath string ends with a NUL byte
(flattenJsonPathParseItem() appends one), so strcmp() would also work.
I kept the length comparison because GetJsonPathVar() makes the same
check at execution time (namelen == varNameLen && strncmp(...)), so
the two places match.
3. Thanks, I kept the upgrade note.
I also ran the items you listed as not tested. All builds have
assertions enabled.
- Wrong result on unpatched master (4892c94): a table of 1000 rows
with js = '"08:00 +00"', and an index on JSON_QUERY(js,
'$.datetime("HH:MI TZH") < $x' PASSING '12:34+00'::timetz AS xx,
'12:34'::time AS x), built with TimeZone = 'UTC'. With UTC, the index
scan and the sequential scan both return 1000 rows. With Asia/Tokyo,
the index scan returns 1000 rows and the sequential scan returns 0.
With the patch, CREATE INDEX fails.
- make check and make installcheck pass on master (243 tests).
- REL_18_STABLE and REL_17_STABLE: the v1 expected output does not
apply there, because these branches do not print the "LINE 1:" context
for this error. The attached back-branch patch has the same code and
tests, with only the expected output changed. It applies to both
branches, and make check passes (235 tests on 18, 229 on 17).
- JSON_QUERY, JSON_VALUE and JSON_EXISTS, each in an index expression
and in a stored generated column, with three PASSING names where each
is a prefix of the next (x, xx, xxx): all 6 orders, the time value on
each name, and each name referenced, plus the two-name cases. That is
348 statements. With the patch, every statement is rejected exactly
when the referenced variable is the time value. On unpatched master,
96 of the 348 give the wrong answer.
- The same 348 statements with non-ASCII names, in a UTF8 database (é,
éé, ééé) and in an EUC_JP database (あ, ああ, あああ), give the same results
with the patch.
- jsonb_path_exists(), jsonb_path_query_first() and jsonb_path_match()
with a vars argument are rejected in index expressions with and
without the patch. The patch does not change them.
I did not test the upgrade case, performance, or other compilers,
Windows or meson.
Regards,
Tomás
From 9371c11a34eb2ee0f8df543abd5215f9a0f81047 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1s=20Senart?= <[email protected]>
Date: Wed, 18 Nov 2026 21:00:42 +0000
Subject: [PATCH v2] Match jsonpath variable names exactly when checking
mutability
jspIsMutableWalker() found the PASSING argument for a jsonpath variable
with strncmp() over the length of the variable's name only. A PASSING
argument whose name starts with the variable's name, such as "aa" for
$a, could therefore supply the datatype used to decide whether a
datetime comparison depends on the TimeZone setting. GetJsonPathVar()
already requires an exact name match at execution time, so the
mutability check could disagree with execution.
As a result, CREATE INDEX could accept an index expression whose result
depends on TimeZone, and could reject one that is immutable.
Also require equal name lengths, as GetJsonPathVar() does. Add
regression tests for a PASSING name that extends the variable's name
and for one that is a prefix of it.
Indexes whose definitions were incorrectly accepted as immutable can
return wrong results and should be dropped or replaced with expressions
that are truly immutable. REINDEX does not make those definitions
safe. Restoring their current definitions from a dump will now fail at
CREATE INDEX.
Backpatch-through: 17
---
src/backend/utils/adt/jsonpath.c | 3 ++-
src/test/regress/expected/sqljson_queryfuncs.out | 6 ++++++
src/test/regress/sql/sqljson_queryfuncs.sql | 4 ++++
3 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/src/backend/utils/adt/jsonpath.c b/src/backend/utils/adt/jsonpath.c
index 11e6193e9..aaf8db16d 100644
--- a/src/backend/utils/adt/jsonpath.c
+++ b/src/backend/utils/adt/jsonpath.c
@@ -1339,7 +1339,8 @@ jspIsMutableWalker(JsonPathItem *jpi, struct
JsonPathMutableContext *cxt)
String *varname =
lfirst_node(String, lc1);
Node *varexpr =
lfirst(lc2);
- if (strncmp(varname->sval,
name, len))
+ if (strlen(varname->sval) !=
len ||
+ strncmp(varname->sval,
name, len) != 0)
continue;
switch (exprType(varexpr))
diff --git a/src/test/regress/expected/sqljson_queryfuncs.out
b/src/test/regress/expected/sqljson_queryfuncs.out
index b19568b34..f493ba07d 100644
--- a/src/test/regress/expected/sqljson_queryfuncs.out
+++ b/src/test/regress/expected/sqljson_queryfuncs.out
@@ -1297,6 +1297,12 @@ CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js,
'$.a ? (@.datetime("HH:MI
ERROR: functions in index expression must be marked IMMUTABLE
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH")
< $x' PASSING '12:34'::timetz AS x));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH")
< $y' PASSING '12:34'::timetz AS x));
+-- PASSING values whose names extend or prefix the variable's name are not used
+CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH")
< $x' PASSING '12:34'::timetz AS xx, '12:34'::time AS x));
+ERROR: functions in index expression must be marked IMMUTABLE
+-- This case was rejected before the fix too; it guards the reverse match
+CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH")
< $xx' PASSING '12:34'::timetz AS x, '12:34'::time AS xx));
+ERROR: functions in index expression must be marked IMMUTABLE
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime() < $x'
PASSING '12:34'::timetz AS x));
ERROR: functions in index expression must be marked IMMUTABLE
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime() < $x'
PASSING '1234'::int AS x));
diff --git a/src/test/regress/sql/sqljson_queryfuncs.sql
b/src/test/regress/sql/sqljson_queryfuncs.sql
index 5f2b5077e..1d5967eb1 100644
--- a/src/test/regress/sql/sqljson_queryfuncs.sql
+++ b/src/test/regress/sql/sqljson_queryfuncs.sql
@@ -430,6 +430,10 @@ CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.a
? (@.datetime("HH:MI"
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.a ?
(@.datetime("HH:MI TZH") < $.datetime("YY-MM-DD HH:MI"))'));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH")
< $x' PASSING '12:34'::timetz AS x));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH")
< $y' PASSING '12:34'::timetz AS x));
+-- PASSING values whose names extend or prefix the variable's name are not used
+CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH")
< $x' PASSING '12:34'::timetz AS xx, '12:34'::time AS x));
+-- This case was rejected before the fix too; it guards the reverse match
+CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH")
< $xx' PASSING '12:34'::timetz AS x, '12:34'::time AS xx));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime() < $x'
PASSING '12:34'::timetz AS x));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime() < $x'
PASSING '1234'::int AS x));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime() ? (@ ==
$x)' PASSING '12:34'::time AS x));
--
2.55.0
From df662ff5db4c2707b32567aa15eff1a5662268d5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1s=20Senart?= <[email protected]>
Date: Tue, 10 Nov 2026 14:05:43 +0000
Subject: [PATCH v2] Match jsonpath variable names exactly when checking
mutability
jspIsMutableWalker() found the PASSING argument for a jsonpath variable
with strncmp() over the length of the variable's name only. A PASSING
argument whose name starts with the variable's name, such as "aa" for
$a, could therefore supply the datatype used to decide whether a
datetime comparison depends on the TimeZone setting. GetJsonPathVar()
already requires an exact name match at execution time, so the
mutability check could disagree with execution.
As a result, CREATE INDEX could accept an index expression whose result
depends on TimeZone, and could reject one that is immutable.
Also require equal name lengths, as GetJsonPathVar() does. Add
regression tests for a PASSING name that extends the variable's name
and for one that is a prefix of it.
Indexes whose definitions were incorrectly accepted as immutable can
return wrong results and should be dropped or replaced with expressions
that are truly immutable. REINDEX does not make those definitions
safe. Restoring their current definitions from a dump will now fail at
CREATE INDEX.
Backpatch-through: 17
---
src/backend/utils/adt/jsonpath.c | 3 ++-
src/test/regress/expected/sqljson_queryfuncs.out | 10 ++++++++++
src/test/regress/sql/sqljson_queryfuncs.sql | 4 ++++
3 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/src/backend/utils/adt/jsonpath.c b/src/backend/utils/adt/jsonpath.c
index 7bfc18c98..a016565c0 100644
--- a/src/backend/utils/adt/jsonpath.c
+++ b/src/backend/utils/adt/jsonpath.c
@@ -1447,7 +1447,8 @@ jspIsMutableWalker(JsonPathItem *jpi, struct JsonPathMutableContext *cxt)
String *varname = lfirst_node(String, lc1);
Node *varexpr = lfirst(lc2);
- if (strncmp(varname->sval, name, len))
+ if (strlen(varname->sval) != len ||
+ strncmp(varname->sval, name, len) != 0)
continue;
switch (exprType(varexpr))
diff --git a/src/test/regress/expected/sqljson_queryfuncs.out b/src/test/regress/expected/sqljson_queryfuncs.out
index 19db825c8..e5b537f82 100644
--- a/src/test/regress/expected/sqljson_queryfuncs.out
+++ b/src/test/regress/expected/sqljson_queryfuncs.out
@@ -1343,6 +1343,16 @@ LINE 1: CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.a ?...
^
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH") < $x' PASSING '12:34'::timetz AS x));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH") < $y' PASSING '12:34'::timetz AS x));
+-- PASSING values whose names extend or prefix the variable's name are not used
+CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH") < $x' PASSING '12:34'::timetz AS xx, '12:34'::time AS x));
+ERROR: functions in index expression must be marked IMMUTABLE
+LINE 1: CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.dat...
+ ^
+-- This case was rejected before the fix too; it guards the reverse match
+CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH") < $xx' PASSING '12:34'::timetz AS x, '12:34'::time AS xx));
+ERROR: functions in index expression must be marked IMMUTABLE
+LINE 1: CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.dat...
+ ^
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime() < $x' PASSING '12:34'::timetz AS x));
ERROR: functions in index expression must be marked IMMUTABLE
LINE 1: CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.dat...
diff --git a/src/test/regress/sql/sqljson_queryfuncs.sql b/src/test/regress/sql/sqljson_queryfuncs.sql
index 5c675d982..8d5a3cdcf 100644
--- a/src/test/regress/sql/sqljson_queryfuncs.sql
+++ b/src/test/regress/sql/sqljson_queryfuncs.sql
@@ -430,6 +430,10 @@ CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.a ? (@.datetime("HH:MI"
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.a ? (@.datetime("HH:MI TZH") < $.datetime("YY-MM-DD HH:MI"))'));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH") < $x' PASSING '12:34'::timetz AS x));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH") < $y' PASSING '12:34'::timetz AS x));
+-- PASSING values whose names extend or prefix the variable's name are not used
+CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH") < $x' PASSING '12:34'::timetz AS xx, '12:34'::time AS x));
+-- This case was rejected before the fix too; it guards the reverse match
+CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime("HH:MI TZH") < $xx' PASSING '12:34'::timetz AS x, '12:34'::time AS xx));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime() < $x' PASSING '12:34'::timetz AS x));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime() < $x' PASSING '1234'::int AS x));
CREATE INDEX ON test_jsonb_mutability (JSON_QUERY(js, '$.datetime() ? (@ == $x)' PASSING '12:34'::time AS x));
--
2.55.0