# Copyright (c) 2026, QDB Development Group
|
# PostgreSQL 19 GA reverted the broader JSON constructor immutability.
|
# Preserve the aggregate, composite/array/range rejection and scalar controls.
|
use strict;
|
use warnings FATAL => 'all';
|
use PostgreSQL::Test::Cluster;
|
use PostgreSQL::Test::Utils;
|
use Test::More;
|
|
my $node = PostgreSQL::Test::Cluster->new('sqljson_immutability');
|
$node->init(extra => ['--no-locale', '--encoding=UTF8']);
|
$node->start;
|
|
sub reject_sql
|
{
|
my ($sql, $state) = @_;
|
my ($rc, $out, $err) = $node->psql('postgres', $sql,
|
extra_params => ['--variable=VERBOSITY=verbose']);
|
my @states = ($err =~ /^.*ERROR:\s+([0-9A-Z]{5}):/mg);
|
is_deeply([$rc, $out, \@states], [3, '', [$state]], $sql)
|
or diag($err);
|
}
|
|
$node->safe_psql('postgres', q{create type comp1 as (a int, b date);});
|
$node->safe_psql('postgres', q{create domain d_comp1 as comp1;});
|
$node->safe_psql('postgres', q{create domain mydomain as timestamptz;});
|
$node->safe_psql('postgres', q{create type mydomainrange as range(subtype=mydomain);});
|
$node->safe_psql('postgres', q{create type comp3 as (a int, b mydomainrange);});
|
$node->safe_psql('postgres', q{create table test_mutability( a text[], b timestamp, c timestamptz, d date, f1 comp1[], f2 timestamp[], f3 d_comp1[], f4 mydomainrange[], f5 comp3, f6 mydomainmultirange);});
|
reject_sql(q{create index xx on test_mutability(json_objectagg(a: b absent on null with unique keys returning jsonb));}, '42803');
|
reject_sql(q{create index xx on test_mutability(json_objectagg(a: b absent on null with unique keys returning json));}, '42803');
|
reject_sql(q{create index xx on test_mutability(json_arrayagg(a returning jsonb));}, '42803');
|
reject_sql(q{create index xx on test_mutability(json_arrayagg(a returning json));}, '42803');
|
reject_sql(q{create index on test_mutability(json_array(a returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_array(b returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_array(c returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_array(d returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_array(f1 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_array(f2 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_array(f3 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_array(f4 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_array(f5 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_array(f6 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_object('hello' value a returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_object('hello' value b returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_object('hello' value c returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_object('hello' value d returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_object('hello' value f1 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_object('hello' value f2 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_object('hello' value f3 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_object('hello' value f4 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_object('hello' value f5 returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_mutability(json_object('hello' value f6 returning jsonb));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f10 json generated always as (json_array(a returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f11 json generated always as (json_array(b returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f11 json generated always as (json_array(c returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f11 json generated always as (json_array(d returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f11 json generated always as (json_array(f1 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f11 json generated always as (json_array(f2 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f11 json generated always as (json_array(f3 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f11 json generated always as (json_array(f4 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f11 json generated always as (json_array(f5 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f11 json generated always as (json_array(f6 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f11 json generated always as (json_object('hello' value a returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f12 json generated always as (json_object('hello' value b returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f12 json generated always as (json_object('hello' value c returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f12 json generated always as (json_object('hello' value d returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f12 json generated always as (json_object('hello' value f1 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f12 json generated always as (json_object('hello' value f2 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f12 json generated always as (json_object('hello' value f3 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f12 json generated always as (json_object('hello' value f4 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f12 json generated always as (json_object('hello' value f5 returning json));}, '42P17');
|
reject_sql(q{alter table test_mutability add column f12 json generated always as (json_object('hello' value f6 returning json));}, '42P17');
|
$node->safe_psql('postgres', q{drop table test_mutability;});
|
$node->safe_psql('postgres', q{drop domain d_comp1;});
|
$node->safe_psql('postgres', q{drop type comp3;});
|
$node->safe_psql('postgres', q{drop type mydomainrange;});
|
$node->safe_psql('postgres', q{drop domain mydomain;});
|
$node->safe_psql('postgres', q{drop type comp1;});
|
$node->safe_psql('postgres', q{create type range_int as range(subtype=int);});
|
$node->safe_psql('postgres', q{create table test_range_immutable(r range_int, m multirange_int);});
|
reject_sql(q{create index on test_range_immutable(json_array(r returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_range_immutable(json_array(m returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_range_immutable(json_object('key' value r returning jsonb));}, '42P17');
|
reject_sql(q{create index on test_range_immutable(json_object('key' value m returning jsonb));}, '42P17');
|
$node->safe_psql('postgres', q{drop table test_range_immutable;});
|
$node->safe_psql('postgres', q{drop type range_int;});
|
$node->safe_psql('postgres', q{CREATE TABLE scalar_json_control(n integer NOT NULL, j json GENERATED ALWAYS AS (JSON_ARRAY(n RETURNING json)) STORED);});
|
$node->safe_psql('postgres', q{CREATE INDEX scalar_json_control_idx ON scalar_json_control ((JSON_OBJECT('n' VALUE n RETURNING jsonb)));});
|
$node->safe_psql('postgres', q{INSERT INTO scalar_json_control(n) VALUES (7), (11);});
|
is($node->safe_psql('postgres', q{SELECT count(*) = 2 AND bool_and(j::jsonb = jsonb_build_array(n)) FROM scalar_json_control;}),
|
't', 'scalar generated JSON values match');
|
is($node->safe_psql('postgres', q{SELECT count(*) = 1 FROM pg_index WHERE indexrelid = 'scalar_json_control_idx'::regclass AND indisvalid;}),
|
't', 'scalar expression index is valid');
|
$node->safe_psql('postgres', q{DROP TABLE scalar_json_control;});
|
|
$node->stop;
|
done_testing();
|