|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +/* |
| 20 | + * Regression coverage for issue #2356: |
| 21 | + * The containment (@>, <@, @>>, <<@) and key-existence (?, ?|, ?&) |
| 22 | + * operators on agtype must be bound to the lightweight selectivity |
| 23 | + * helpers contsel / contjoinsel during planning. Earlier PG14+ |
| 24 | + * branches used matchingsel / matchingjoinsel, which caused planning |
| 25 | + * to invoke agtype_contains() against pg_statistic MCVs and produced |
| 26 | + * a 30%+ planning-time regression on point queries (severe TPS drop |
| 27 | + * reported on the PG18 branch). |
| 28 | + * |
| 29 | + * This test pins the bindings by querying pg_operator directly. If |
| 30 | + * someone re-introduces matchingsel here, the test diff is loud and |
| 31 | + * precise. |
| 32 | + */ |
| 33 | +LOAD 'age'; |
| 34 | +SET search_path TO ag_catalog; |
| 35 | +-- Selectivity helpers for the four containment operators. |
| 36 | +SELECT o.oprname, |
| 37 | + pg_catalog.format_type(o.oprleft, NULL) AS lhs, |
| 38 | + pg_catalog.format_type(o.oprright, NULL) AS rhs, |
| 39 | + o.oprrest::text AS restrict_fn, |
| 40 | + o.oprjoin::text AS join_fn |
| 41 | +FROM pg_catalog.pg_operator o |
| 42 | +JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace |
| 43 | +WHERE n.nspname = 'ag_catalog' |
| 44 | + AND o.oprname IN ('@>', '<@', '@>>', '<<@') |
| 45 | +ORDER BY o.oprname, lhs, rhs; |
| 46 | + oprname | lhs | rhs | restrict_fn | join_fn |
| 47 | +---------+--------+--------+-------------+------------- |
| 48 | + <<@ | agtype | agtype | contsel | contjoinsel |
| 49 | + <@ | agtype | agtype | contsel | contjoinsel |
| 50 | + @> | agtype | agtype | contsel | contjoinsel |
| 51 | + @>> | agtype | agtype | contsel | contjoinsel |
| 52 | +(4 rows) |
| 53 | + |
| 54 | +-- Selectivity helpers for all key-existence operator overloads |
| 55 | +-- (right-hand side may be text, text[], or agtype). |
| 56 | +SELECT o.oprname, |
| 57 | + pg_catalog.format_type(o.oprleft, NULL) AS lhs, |
| 58 | + pg_catalog.format_type(o.oprright, NULL) AS rhs, |
| 59 | + o.oprrest::text AS restrict_fn, |
| 60 | + o.oprjoin::text AS join_fn |
| 61 | +FROM pg_catalog.pg_operator o |
| 62 | +JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace |
| 63 | +WHERE n.nspname = 'ag_catalog' |
| 64 | + AND o.oprname IN ('?', '?|', '?&') |
| 65 | +ORDER BY o.oprname, lhs, rhs; |
| 66 | + oprname | lhs | rhs | restrict_fn | join_fn |
| 67 | +---------+--------+--------+-------------+------------- |
| 68 | + ? | agtype | agtype | contsel | contjoinsel |
| 69 | + ? | agtype | text | contsel | contjoinsel |
| 70 | + ?& | agtype | agtype | contsel | contjoinsel |
| 71 | + ?& | agtype | text[] | contsel | contjoinsel |
| 72 | + ?| | agtype | agtype | contsel | contjoinsel |
| 73 | + ?| | agtype | text[] | contsel | contjoinsel |
| 74 | +(6 rows) |
| 75 | + |
| 76 | +-- Scoped guard for issue #2356: assert that none of the specific containment |
| 77 | +-- and key-existence operators on agtype are bound to matchingsel / |
| 78 | +-- matchingjoinsel. We deliberately limit the check to these operator names |
| 79 | +-- (rather than every operator in ag_catalog) so unrelated operators that |
| 80 | +-- legitimately use matchingsel for their own semantics are not affected by |
| 81 | +-- this regression test. |
| 82 | +SELECT COUNT(*) AS leaked_matchingsel_bindings |
| 83 | +FROM pg_catalog.pg_operator o |
| 84 | +JOIN pg_catalog.pg_namespace n ON n.oid = o.oprnamespace |
| 85 | +WHERE n.nspname = 'ag_catalog' |
| 86 | + AND o.oprname IN ('@>', '<@', '@>>', '<<@', '?', '?|', '?&') |
| 87 | + AND (o.oprrest::text = 'matchingsel' |
| 88 | + OR o.oprjoin::text = 'matchingjoinsel'); |
| 89 | + leaked_matchingsel_bindings |
| 90 | +----------------------------- |
| 91 | + 0 |
| 92 | +(1 row) |
| 93 | + |
| 94 | +-- Smoke test: each operator still works functionally. Selectivity binding |
| 95 | +-- only affects the planner; this guards against an inadvertent operator |
| 96 | +-- removal as part of any future cleanup. |
| 97 | +SELECT '{"a":1,"b":2}'::agtype @> '{"a":1}'::agtype AS contains_yes; |
| 98 | + contains_yes |
| 99 | +-------------- |
| 100 | + t |
| 101 | +(1 row) |
| 102 | + |
| 103 | +SELECT '{"a":1}'::agtype <@ '{"a":1,"b":2}'::agtype AS contained_yes; |
| 104 | + contained_yes |
| 105 | +--------------- |
| 106 | + t |
| 107 | +(1 row) |
| 108 | + |
| 109 | +SELECT '{"a":{"b":1}}'::agtype @>> '{"a":{"b":1}}'::agtype AS top_contains_yes; |
| 110 | + top_contains_yes |
| 111 | +------------------ |
| 112 | + t |
| 113 | +(1 row) |
| 114 | + |
| 115 | +SELECT '{"a":{"b":1}}'::agtype <<@ '{"a":{"b":1}}'::agtype AS top_contained_yes; |
| 116 | + top_contained_yes |
| 117 | +------------------- |
| 118 | + t |
| 119 | +(1 row) |
| 120 | + |
| 121 | +SELECT '{"a":1}'::agtype ? 'a'::text AS exists_text_yes; |
| 122 | + exists_text_yes |
| 123 | +----------------- |
| 124 | + t |
| 125 | +(1 row) |
| 126 | + |
| 127 | +SELECT '{"a":1}'::agtype ? '"a"'::agtype AS exists_agtype_yes; |
| 128 | + exists_agtype_yes |
| 129 | +------------------- |
| 130 | + t |
| 131 | +(1 row) |
| 132 | + |
| 133 | +SELECT '{"a":1,"b":2}'::agtype ?| ARRAY['a','c'] AS exists_any_text_yes; |
| 134 | + exists_any_text_yes |
| 135 | +--------------------- |
| 136 | + t |
| 137 | +(1 row) |
| 138 | + |
| 139 | +SELECT '{"a":1,"b":2}'::agtype ?| '["a","c"]'::agtype AS exists_any_agtype_yes; |
| 140 | + exists_any_agtype_yes |
| 141 | +----------------------- |
| 142 | + t |
| 143 | +(1 row) |
| 144 | + |
| 145 | +SELECT '{"a":1,"b":2}'::agtype ?& ARRAY['a','b'] AS exists_all_text_yes; |
| 146 | + exists_all_text_yes |
| 147 | +--------------------- |
| 148 | + t |
| 149 | +(1 row) |
| 150 | + |
| 151 | +SELECT '{"a":1,"b":2}'::agtype ?& '["a","b"]'::agtype AS exists_all_agtype_yes; |
| 152 | + exists_all_agtype_yes |
| 153 | +----------------------- |
| 154 | + t |
| 155 | +(1 row) |
| 156 | + |
0 commit comments