Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ REGRESS = scan \
direct_field_access \
security \
reserved_keyword_alias \
agtype_jsonb_cast
agtype_jsonb_cast \
containment_selectivity

ifneq ($(EXTRA_TESTS),)
REGRESS += $(EXTRA_TESTS)
Expand Down
38 changes: 38 additions & 0 deletions age--1.7.0--y.y.y.sql
Original file line number Diff line number Diff line change
Expand Up @@ -762,3 +762,41 @@ CREATE FUNCTION ag_catalog._agehash_self_test()
VOLATILE
PARALLEL UNSAFE
AS 'MODULE_PATHNAME';

--
-- Issue #2356: rebind containment and key-existence operators to the
-- lightweight contsel / contjoinsel selectivity estimators.
--
-- @>, <@, @>>, <<@, ?, ?|, ?& on agtype were bound to matchingsel /
-- matchingjoinsel. During planning matchingsel invokes the operator's
-- underlying function (agtype_contains) once per pg_statistic MCV entry and
-- histogram bin; with a realistic default_statistics_target that planning
-- cost dominates simple point queries (the regression reported in #2356).
--
-- contsel / contjoinsel return fixed selectivity constants without calling the
-- operator function, so planning is constant-time. This is a deliberate
-- planning-speed vs. estimate-accuracy trade-off. Note it DIVERGES from
-- PostgreSQL core, which keeps jsonb's @>, <@, ?, ?|, ?& on matchingsel /
-- matchingjoinsel; it is an AGE-specific choice favoring workloads where these
-- operators appear in selective point lookups.
--
ALTER OPERATOR ag_catalog.@>(agtype, agtype)
SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.<@(agtype, agtype)
SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.@>>(agtype, agtype)
SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.<<@(agtype, agtype)
SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?(agtype, text)
SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?(agtype, agtype)
SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?|(agtype, text[])
SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?|(agtype, agtype)
SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?&(agtype, text[])
SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?&(agtype, agtype)
SET (RESTRICT = contsel, JOIN = contjoinsel);
245 changes: 245 additions & 0 deletions regress/expected/containment_selectivity.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Regression coverage for issue #2356:
* The containment (@>, <@, @>>, <<@) and key-existence (?, ?|, ?&)
* operators on agtype must be bound to the lightweight selectivity
* helpers contsel / contjoinsel during planning. Earlier PG14+
* branches used matchingsel / matchingjoinsel, which caused planning
* to invoke agtype_contains() against pg_statistic MCVs and produced
* a 30%+ planning-time regression on point queries (severe TPS drop
* reported on the PG18 branch).
*
* This test pins the bindings by querying pg_operator directly. If
* someone re-introduces matchingsel here, the test diff is loud and
* precise.
*/
LOAD 'age';
SET search_path TO ag_catalog;
-- Selectivity helpers for the four containment operators.
SELECT o.oprname,
pg_catalog.format_type(o.oprleft, NULL) AS lhs,
pg_catalog.format_type(o.oprright, NULL) AS rhs,
o.oprrest::text AS restrict_fn,
o.oprjoin::text AS join_fn
FROM pg_catalog.pg_operator o
JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace
WHERE n.nspname = 'ag_catalog'
AND o.oprname IN ('@>', '<@', '@>>', '<<@')
ORDER BY o.oprname, lhs, rhs;
oprname | lhs | rhs | restrict_fn | join_fn
---------+--------+--------+-------------+-------------
<<@ | agtype | agtype | contsel | contjoinsel
<@ | agtype | agtype | contsel | contjoinsel
@> | agtype | agtype | contsel | contjoinsel
@>> | agtype | agtype | contsel | contjoinsel
(4 rows)

-- Selectivity helpers for all key-existence operator overloads
-- (right-hand side may be text, text[], or agtype).
SELECT o.oprname,
pg_catalog.format_type(o.oprleft, NULL) AS lhs,
pg_catalog.format_type(o.oprright, NULL) AS rhs,
o.oprrest::text AS restrict_fn,
o.oprjoin::text AS join_fn
FROM pg_catalog.pg_operator o
JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace
WHERE n.nspname = 'ag_catalog'
AND o.oprname IN ('?', '?|', '?&')
ORDER BY o.oprname, lhs, rhs;
oprname | lhs | rhs | restrict_fn | join_fn
---------+--------+--------+-------------+-------------
? | agtype | agtype | contsel | contjoinsel
? | agtype | text | contsel | contjoinsel
?& | agtype | agtype | contsel | contjoinsel
?& | agtype | text[] | contsel | contjoinsel
?| | agtype | agtype | contsel | contjoinsel
?| | agtype | text[] | contsel | contjoinsel
(6 rows)

-- Scoped guard for issue #2356: assert that none of the specific containment
-- and key-existence operators on agtype are bound to matchingsel /
-- matchingjoinsel. We deliberately limit the check to these operator names
-- (rather than every operator in ag_catalog) so unrelated operators that
-- legitimately use matchingsel for their own semantics are not affected by
-- this regression test.
SELECT COUNT(*) AS leaked_matchingsel_bindings
FROM pg_catalog.pg_operator o
JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace
WHERE n.nspname = 'ag_catalog'
AND o.oprname IN ('@>', '<@', '@>>', '<<@', '?', '?|', '?&')
AND (o.oprrest::text = 'matchingsel'
OR o.oprjoin::text = 'matchingjoinsel');
leaked_matchingsel_bindings
-----------------------------
0
(1 row)

-- Smoke test: each operator still works functionally. Selectivity binding
-- only affects the planner; this guards against an inadvertent operator
-- removal as part of any future cleanup.
SELECT '{"a":1,"b":2}'::agtype @> '{"a":1}'::agtype AS contains_yes;
contains_yes
--------------
t
(1 row)

SELECT '{"a":1}'::agtype <@ '{"a":1,"b":2}'::agtype AS contained_yes;
contained_yes
---------------
t
(1 row)

SELECT '{"a":{"b":1}}'::agtype @>> '{"a":{"b":1}}'::agtype AS top_contains_yes;
top_contains_yes
------------------
t
(1 row)

SELECT '{"a":{"b":1}}'::agtype <<@ '{"a":{"b":1}}'::agtype AS top_contained_yes;
top_contained_yes
-------------------
t
(1 row)

SELECT '{"a":1}'::agtype ? 'a'::text AS exists_text_yes;
exists_text_yes
-----------------
t
(1 row)

SELECT '{"a":1}'::agtype ? '"a"'::agtype AS exists_agtype_yes;
exists_agtype_yes
-------------------
t
(1 row)

SELECT '{"a":1,"b":2}'::agtype ?| ARRAY['a','c'] AS exists_any_text_yes;
exists_any_text_yes
---------------------
t
(1 row)

SELECT '{"a":1,"b":2}'::agtype ?| '["a","c"]'::agtype AS exists_any_agtype_yes;
exists_any_agtype_yes
-----------------------
t
(1 row)

SELECT '{"a":1,"b":2}'::agtype ?& ARRAY['a','b'] AS exists_all_text_yes;
exists_all_text_yes
---------------------
t
(1 row)

SELECT '{"a":1,"b":2}'::agtype ?& '["a","b"]'::agtype AS exists_all_agtype_yes;
exists_all_agtype_yes
-----------------------
t
(1 row)

-- Upgrade-path assertion for issue #2356.
--
-- The checks above cover a FRESH install: contsel / contjoinsel come straight
-- from agtype_operators.sql and agtype_exists.sql. Existing installs instead
-- pick up the fix from the ALTER OPERATOR ... SET (RESTRICT, JOIN) block that
-- age--1.7.0--y.y.y.sql ships and "ALTER EXTENSION age UPDATE" replays. Nothing
-- above exercises that block, so a silent regression in it would go unnoticed.
--
-- We replay the shipped ALTER OPERATOR statements directly rather than running
-- ALTER EXTENSION age UPDATE: the dev upgrade script targets the placeholder
-- version "y.y.y" and is not a stable version-chain target inside the
-- regression harness. The whole section runs in a transaction that is rolled
-- back, so it observes the flip without permanently mutating the operator
-- catalog (PostgreSQL DDL is transactional).
BEGIN;
-- Simulate a stale (pre-fix) install: force all ten overloads back onto
-- matchingsel / matchingjoinsel.
ALTER OPERATOR ag_catalog.@>(agtype, agtype) SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
ALTER OPERATOR ag_catalog.<@(agtype, agtype) SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
ALTER OPERATOR ag_catalog.@>>(agtype, agtype) SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
ALTER OPERATOR ag_catalog.<<@(agtype, agtype) SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
ALTER OPERATOR ag_catalog.?(agtype, text) SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
ALTER OPERATOR ag_catalog.?(agtype, agtype) SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
ALTER OPERATOR ag_catalog.?|(agtype, text[]) SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
ALTER OPERATOR ag_catalog.?|(agtype, agtype) SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
ALTER OPERATOR ag_catalog.?&(agtype, text[]) SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
ALTER OPERATOR ag_catalog.?&(agtype, agtype) SET (RESTRICT = matchingsel, JOIN = matchingjoinsel);
-- Stale state: every overload now reports matchingsel / matchingjoinsel.
SELECT o.oprname,
pg_catalog.format_type(o.oprleft, NULL) AS lhs,
pg_catalog.format_type(o.oprright, NULL) AS rhs,
o.oprrest::text AS restrict_fn,
o.oprjoin::text AS join_fn
FROM pg_catalog.pg_operator o
JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace
WHERE n.nspname = 'ag_catalog'
AND o.oprname IN ('@>', '<@', '@>>', '<<@', '?', '?|', '?&')
ORDER BY o.oprname, lhs, rhs;
oprname | lhs | rhs | restrict_fn | join_fn
---------+--------+--------+-------------+-----------------
<<@ | agtype | agtype | matchingsel | matchingjoinsel
<@ | agtype | agtype | matchingsel | matchingjoinsel
? | agtype | agtype | matchingsel | matchingjoinsel
? | agtype | text | matchingsel | matchingjoinsel
?& | agtype | agtype | matchingsel | matchingjoinsel
?& | agtype | text[] | matchingsel | matchingjoinsel
?| | agtype | agtype | matchingsel | matchingjoinsel
?| | agtype | text[] | matchingsel | matchingjoinsel
@> | agtype | agtype | matchingsel | matchingjoinsel
@>> | agtype | agtype | matchingsel | matchingjoinsel
(10 rows)

-- Replay the exact ALTER OPERATOR block shipped in age--1.7.0--y.y.y.sql.
ALTER OPERATOR ag_catalog.@>(agtype, agtype) SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.<@(agtype, agtype) SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.@>>(agtype, agtype) SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.<<@(agtype, agtype) SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?(agtype, text) SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?(agtype, agtype) SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?|(agtype, text[]) SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?|(agtype, agtype) SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?&(agtype, text[]) SET (RESTRICT = contsel, JOIN = contjoinsel);
ALTER OPERATOR ag_catalog.?&(agtype, agtype) SET (RESTRICT = contsel, JOIN = contjoinsel);
-- After the upgrade replay every overload is back on contsel / contjoinsel.
SELECT o.oprname,
pg_catalog.format_type(o.oprleft, NULL) AS lhs,
pg_catalog.format_type(o.oprright, NULL) AS rhs,
o.oprrest::text AS restrict_fn,
o.oprjoin::text AS join_fn
FROM pg_catalog.pg_operator o
JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace
WHERE n.nspname = 'ag_catalog'
AND o.oprname IN ('@>', '<@', '@>>', '<<@', '?', '?|', '?&')
ORDER BY o.oprname, lhs, rhs;
oprname | lhs | rhs | restrict_fn | join_fn
---------+--------+--------+-------------+-------------
<<@ | agtype | agtype | contsel | contjoinsel
<@ | agtype | agtype | contsel | contjoinsel
? | agtype | agtype | contsel | contjoinsel
? | agtype | text | contsel | contjoinsel
?& | agtype | agtype | contsel | contjoinsel
?& | agtype | text[] | contsel | contjoinsel
?| | agtype | agtype | contsel | contjoinsel
?| | agtype | text[] | contsel | contjoinsel
@> | agtype | agtype | contsel | contjoinsel
@>> | agtype | agtype | contsel | contjoinsel
(10 rows)

ROLLBACK;
Loading
Loading