postgres.git / summary / log / commit / refs

commit    47ca0f45e6fed487d9c3b83db0e7c6a631c1cfb7
Author:   Fujii Masao <fujii@postgresql.org>
Date:     Fri Nov 20 06:20:00 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 8b5f99e6a82..6d1d7664f1e 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -3789,6 +3789,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 c7b8c36c842..56aea472ba1 100644 --- a/src/test/regress/expected/jsonb_jsonpath.out +++ b/src/test/regress/expected/jsonb_jsonpath.out @@ -1782,6 +1782,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" -- Test .bigint() select jsonb_path_query('null', '$.bigint()'); ERROR: jsonpath item method .bigint() can only be applied to a string or numeric value diff --git a/src/test/regress/sql/jsonb_jsonpath.sql b/src/test/regress/sql/jsonb_jsonpath.sql index c37dc3817ff..136b439b596 100644 --- a/src/test/regress/sql/jsonb_jsonpath.sql +++ b/src/test/regress/sql/jsonb_jsonpath.sql @@ -383,6 +383,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")'); + -- Test .bigint() select jsonb_path_query('null', '$.bigint()'); select jsonb_path_query('true', '$.bigint()'); [parent: e7e1835e6b02]