postgres.git / summary / log / commit / refs
commit d0d809a6c4a13f4ff6c59ca5b56b363d15c5d451
Author: Fujii Masao <fujii@postgresql.org>
Date: Fri Nov 20 06:21:26 2026 +0000
Fix parsing mode tracking when recycling datetime format cache entries
JSONPath datetime() could accept invalid format strings depending on
earlier datetime formatting operations in the same session. For example,
a format containing an unquoted "z" separator could be accepted after
to_char() had used it, although SQL/JSON standard mode prohibited it.
The cause was that DCH_cache_getnew() did not update the std flag when
recycling a cache entry. Since cache lookups matched both the format
string and std, a format parsed in one mode could be reused in the other.
Fix this by setting std when recycling an entry, as already done when
allocating a new one.
Backpatch to all supported versions.
Reported-by: Michael Malis <malis@pgrust.com>
Author: Andrey Rachitskiy <pl0h0yp1@gmail.com>
Reviewed-by: Aleksander Alekseev <aleksander@tigerdata.com>
Reviewed-by: Fujii Masao <masao.fujii@gmail.com>
Discussion: https://postgr.es/m/19594-5d9bdc019e3f7f6e@postgresql.org
Backpatch-through: 15
src/backend/utils/adt/formatting.c | 1 +
src/test/regress/expected/jsonb_jsonpath.out | 22 ++++++++++++++++++++++
src/test/regress/sql/jsonb_jsonpath.sql | 12 ++++++++++++
3 files changed, 35 insertions(+)
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index 580526bd832..116b22b72da 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -4092,6 +4092,7 @@ DCH_cache_getnew(const char *str, bool std)
#endif
old->valid = false;
strlcpy(old->str, str, DCH_CACHE_SIZE + 1);
+ old->std = std;
old->age = (++DCHCounter);
/* caller is expected to fill format, then set valid */
return old;
diff --git a/src/test/regress/expected/jsonb_jsonpath.out b/src/test/regress/expected/jsonb_jsonpath.out
index eafb421c7af..e66671d3645 100644
--- a/src/test/regress/expected/jsonb_jsonpath.out
+++ b/src/test/regress/expected/jsonb_jsonpath.out
@@ -1739,6 +1739,28 @@ select jsonb_path_query('"10-03-2017t12:34:56"', '$.datetime("dd-mm-yyyy\"T\"HH2
ERROR: unmatched format character "T"
select jsonb_path_query('"10-03-2017 12:34:56"', '$.datetime("dd-mm-yyyy\"T\"HH24:MI:SS")');
ERROR: unmatched format character "T"
+-- Fill all 20 DCH cache entries (DCH_CACHE_ENTRIES) in standard mode,
+-- then check that recycling an entry for to_char() updates its parsing mode.
+select count(*)
+from (
+ select jsonb_path_query(
+ to_jsonb('x' || g || '1234'),
+ format('$.datetime("\"x%s\"HH24MI")', g)::jsonpath)
+ from generate_series(1, 20) g
+) s;
+ count
+-------
+ 20
+(1 row)
+
+select to_char(timestamp '2000-01-01 12:34:00', 'HH24zMI');
+ to_char
+---------
+ 12z34
+(1 row)
+
+select jsonb_path_query('"12z34"', '$.datetime("HH24zMI")');
+ERROR: invalid datetime format separator: "z"
set time zone '+00';
select jsonb_path_query('"10-03-2017 12:34"', '$.datetime("dd-mm-yyyy HH24:MI")');
jsonb_path_query
diff --git a/src/test/regress/sql/jsonb_jsonpath.sql b/src/test/regress/sql/jsonb_jsonpath.sql
index 8163fc6713b..3768f04b347 100644
--- a/src/test/regress/sql/jsonb_jsonpath.sql
+++ b/src/test/regress/sql/jsonb_jsonpath.sql
@@ -377,6 +377,18 @@ select jsonb_path_query('"10-03-2017T12:34:56"', '$.datetime("dd-mm-yyyy\"T\"HH2
select jsonb_path_query('"10-03-2017t12:34:56"', '$.datetime("dd-mm-yyyy\"T\"HH24:MI:SS")');
select jsonb_path_query('"10-03-2017 12:34:56"', '$.datetime("dd-mm-yyyy\"T\"HH24:MI:SS")');
+-- Fill all 20 DCH cache entries (DCH_CACHE_ENTRIES) in standard mode,
+-- then check that recycling an entry for to_char() updates its parsing mode.
+select count(*)
+from (
+ select jsonb_path_query(
+ to_jsonb('x' || g || '1234'),
+ format('$.datetime("\"x%s\"HH24MI")', g)::jsonpath)
+ from generate_series(1, 20) g
+) s;
+select to_char(timestamp '2000-01-01 12:34:00', 'HH24zMI');
+select jsonb_path_query('"12z34"', '$.datetime("HH24zMI")');
+
set time zone '+00';
select jsonb_path_query('"10-03-2017 12:34"', '$.datetime("dd-mm-yyyy HH24:MI")');
[parent: f772e165b87b]