Skip to content
Open
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 @@ -180,7 +180,8 @@ REGRESS = scan \
map_projection \
direct_field_access \
security \
reserved_keyword_alias
reserved_keyword_alias \
containment_selectivity

ifneq ($(EXTRA_TESTS),)
REGRESS += $(EXTRA_TESTS)
Expand Down
35 changes: 35 additions & 0 deletions age--1.7.0--y.y.y.sql
Original file line number Diff line number Diff line change
Expand Up @@ -459,3 +459,38 @@ BEGIN
END LOOP;
END;
$$;


--
-- Issue #2356: restore lightweight selectivity functions for containment
-- and key-existence operators.
--
-- The PG14+ branches of AGE bound RESTRICT=matchingsel / JOIN=matchingjoinsel
-- on @>, <@, @>>, <<@, ?, ?|, ?&. matchingsel is built for pattern operators
-- (LIKE / regex) and invokes the operator's underlying support function on
-- pg_statistic MCVs during planning. For agtype that re-runs agtype_contains
-- per MCV, which can dominate planning time on point queries (TPS regression
-- reported on PG18). PostgreSQL core itself binds @>/<@/? on jsonb to
-- contsel/contjoinsel for the same reason; this aligns AGE with that
-- precedent.
--
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);
156 changes: 156 additions & 0 deletions regress/expected/containment_selectivity.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* 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)

30 changes: 14 additions & 16 deletions regress/expected/cypher_match.out
Original file line number Diff line number Diff line change
Expand Up @@ -2404,21 +2404,21 @@ SELECT * FROM cypher('cypher_match', $$ MATCH (a {name:a.name}) MATCH (a {age:a.
{"id": 281474976710659, "label": "", "properties": {"age": 3, "name": "orphan"}}::vertex
(3 rows)

SELECT * FROM cypher('cypher_match', $$ MATCH p=(a)-[u {relationship: u.relationship}]->(b) RETURN p $$) as (a agtype);
SELECT * FROM cypher('cypher_match', $$ MATCH p=(a)-[u {relationship: u.relationship}]->(b) RETURN p ORDER BY id(u) $$) as (a agtype);
a
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"id": 281474976710661, "label": "", "properties": {"age": 4, "name": "T"}}::vertex, {"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge, {"id": 281474976710666, "label": "", "properties": {"age": 6}}::vertex]::path
[{"id": 281474976710659, "label": "", "properties": {"age": 3, "name": "orphan"}}::vertex, {"id": 4785074604081154, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710659, "properties": {"years": 4, "relationship": "enemies"}}::edge, {"id": 281474976710666, "label": "", "properties": {"age": 6}}::vertex]::path
(2 rows)

SELECT * FROM cypher('cypher_match', $$ MATCH p=(a)-[u {relationship: u.relationship, years: u.years}]->(b) RETURN p $$) as (a agtype);
SELECT * FROM cypher('cypher_match', $$ MATCH p=(a)-[u {relationship: u.relationship, years: u.years}]->(b) RETURN p ORDER BY id(u) $$) as (a agtype);
a
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"id": 281474976710661, "label": "", "properties": {"age": 4, "name": "T"}}::vertex, {"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge, {"id": 281474976710666, "label": "", "properties": {"age": 6}}::vertex]::path
[{"id": 281474976710659, "label": "", "properties": {"age": 3, "name": "orphan"}}::vertex, {"id": 4785074604081154, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710659, "properties": {"years": 4, "relationship": "enemies"}}::edge, {"id": 281474976710666, "label": "", "properties": {"age": 6}}::vertex]::path
(2 rows)

SELECT * FROM cypher('cypher_match', $$ MATCH p=(a {name:a.name})-[u {relationship: u.relationship}]->(b {age:b.age}) RETURN p $$) as (a agtype);
SELECT * FROM cypher('cypher_match', $$ MATCH p=(a {name:a.name})-[u {relationship: u.relationship}]->(b {age:b.age}) RETURN p ORDER BY id(u) $$) as (a agtype);
a
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[{"id": 281474976710661, "label": "", "properties": {"age": 4, "name": "T"}}::vertex, {"id": 4785074604081153, "label": "knows", "end_id": 281474976710666, "start_id": 281474976710661, "properties": {"years": 3, "relationship": "friends"}}::edge, {"id": 281474976710666, "label": "", "properties": {"age": 6}}::vertex]::path
Expand Down Expand Up @@ -3398,19 +3398,17 @@ SELECT count(*) FROM cypher('test_enable_containment', $$ MATCH p=(x:Customer)-[
(1 row)

SELECT * FROM cypher('test_enable_containment', $$ EXPLAIN (costs off) MATCH (x:Customer)-[:bought ={store: 'Amazon', addr:{city: 'Vancouver', street: 30}}]->(y:Product) RETURN 0 $$) as (a agtype);
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
Hash Join
Hash Cond: (y.id = _age_default_alias_0.end_id)
-> Seq Scan on "Product" y
-> Hash
-> Hash Join
Hash Cond: (x.id = _age_default_alias_0.start_id)
-> Seq Scan on "Customer" x
-> Hash
-> Seq Scan on bought _age_default_alias_0
Filter: (properties @>> '{"addr": {"city": "Vancouver", "street": 30}, "store": "Amazon"}'::agtype)
(10 rows)
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
Nested Loop
-> Nested Loop
-> Seq Scan on bought _age_default_alias_0
Filter: (properties @>> '{"addr": {"city": "Vancouver", "street": 30}, "store": "Amazon"}'::agtype)
-> Index Only Scan using "Customer_pkey" on "Customer" x
Index Cond: (id = _age_default_alias_0.start_id)
-> Index Only Scan using "Product_pkey" on "Product" y
Index Cond: (id = _age_default_alias_0.end_id)
(8 rows)

SELECT * FROM cypher('test_enable_containment', $$ EXPLAIN (costs off) MATCH (x:Customer ={school: { name: 'XYZ College',program: { major: 'Psyc', degree: 'BSc'} },phone: [ 123456789, 987654321, 456987123 ]}) RETURN 0 $$) as (a agtype);
QUERY PLAN
Expand Down
8 changes: 4 additions & 4 deletions regress/expected/cypher_vle.out
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ BEGIN
RETURN QUERY
SELECT * FROM cypher('mygraph', $CYPHER$
MATCH (h:head {name: $list_name})-[e:next*]->(v:node)
RETURN v
RETURN v ORDER BY id(v)
$CYPHER$, ag_param) AS (node agtype);
END $$;
-- create a list
Expand Down Expand Up @@ -726,8 +726,8 @@ SELECT prepend_node('list01', 'b');
SELECT * FROM show_list_use_vle('list01');
node
-----------------------------------------------------------------------------------
{"id": 1407374883553282, "label": "node", "properties": {"content": "b"}}::vertex
{"id": 1407374883553281, "label": "node", "properties": {"content": "a"}}::vertex
{"id": 1407374883553282, "label": "node", "properties": {"content": "b"}}::vertex
(2 rows)
Comment thread
jrgemignani marked this conversation as resolved.

-- prepend a node 'c'
Expand All @@ -741,9 +741,9 @@ SELECT prepend_node('list01', 'c');
SELECT * FROM show_list_use_vle('list01');
node
-----------------------------------------------------------------------------------
{"id": 1407374883553283, "label": "node", "properties": {"content": "c"}}::vertex
{"id": 1407374883553282, "label": "node", "properties": {"content": "b"}}::vertex
{"id": 1407374883553281, "label": "node", "properties": {"content": "a"}}::vertex
{"id": 1407374883553282, "label": "node", "properties": {"content": "b"}}::vertex
{"id": 1407374883553283, "label": "node", "properties": {"content": "c"}}::vertex
(3 rows)

DROP FUNCTION show_list_use_vle;
Expand Down
Loading