pg.ddx.io  pgsql-committers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Dunstan <andrew@dunslane.net>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: make immutability tests in to_json and to_jsonb complete
Date: Tue, 17 Mar 2026 15:31:24 +0000
Message-ID: <E1w2WOB-000BJE-0h@gemulon.postgresql.org> (raw)

make immutability tests in to_json and to_jsonb complete

Complete the TODOs in to_json_is_immutable() and to_jsonb_is_immutable()
by recursing into container types (arrays, composites, ranges, multiranges,
domains) to check element/sub-type mutability, rather than conservatively
returning "mutable" for all arrays and composites.

The shared logic is factored into a single json_check_mutability() function
in jsonfuncs.c, with the existing exported functions as thin wrappers.
Composite type inspection uses lookup_rowtype_tupdesc() (typcache) instead
of relation_open() to avoid unnecessary lock acquisition in the optimizer.

Range and multirange types are now also checked recursively: if the
subtype's conversion is immutable, the range is considered immutable
for JSON purposes, even though range_out is generically marked STABLE.
This is a behavioral change: range types with immutable subtypes (e.g.,
int4range) can now appear in expression indexes via JSON_ARRAY/JSON_OBJECT,
whereas previously they were conservatively rejected.

Add regression tests for JSON_ARRAY and JSON_OBJECT mutability with
expression indexes and generated columns, covering arrays, composites,
domains, ranges, multiranges and combinations thereof.

Author: Jian He <jian.universality@gmail.com>
Reviewed-by: Andrew Dunstan <andrew@dunslane.net>
Discussion: https://postgr.es/m/CACJufxFz=OsXQdsMJ-cqoqspD9aJrwntsQP-U2A-UaV_M+-S9g@mail.gmail.com
Commitfest: https://commitfest.postgresql.org/patch/5759

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/ecd9288624a1582a732cf86ac5a01475a1ce7815

Modified Files
--------------
src/backend/utils/adt/json.c          |  38 +-------
src/backend/utils/adt/jsonb.c         |  38 +-------
src/backend/utils/adt/jsonfuncs.c     | 105 ++++++++++++++++++++++
src/include/utils/jsonfuncs.h         |   2 +
src/test/regress/expected/sqljson.out | 165 ++++++++++++++++++++++++++++++++++
src/test/regress/sql/sqljson.sql      |  89 ++++++++++++++++++
6 files changed, 367 insertions(+), 70 deletions(-)



Message-ID: <E1w2WOB-000BJE-0h@gemulon.postgresql.org>
Permalink:  ../E1w2WOB-000BJE-0h@gemulon.postgresql.org/
Also on:    postgresql.org/message-id/E1w2WOB-000BJE-0h@gemulon.postgresql.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-committers@postgresql.org
  Cc: andrew@dunslane.net, pgsql-committers@lists.postgresql.org
  Subject: Re: pgsql: make immutability tests in to_json and to_jsonb complete
  In-Reply-To: <E1w2WOB-000BJE-0h@gemulon.postgresql.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox