Skip to content

Commit 1d57b59

Browse files
committed
Add ArrayRows hint
Implement ArrayRows hint parsing and application so array predicates can be adjusted using absolute, additive, subtractive, or multiplicative row corrections.
1 parent 8d28590 commit 1d57b59

File tree

8 files changed

+1655
-32
lines changed

8 files changed

+1655
-32
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ OBJS = \
1212

1313
HINTPLANVER = 1.9.0
1414

15-
REGRESS = init base_plan pg_hint_plan ut-init ut-A ut-S ut-J ut-L ut-G ut-R \
15+
REGRESS = init base_plan pg_hint_plan ut-init ut-A ut-AS ut-S ut-J ut-L ut-G ut-R \
1616
ut-fdw ut-W ut-T ut-fini plpgsql hint_table disable_index \
1717
query_parser oldextversions
1818
REGRESS_OPTS = --encoding=UTF8

docs/hint_list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@ The available hints are listed below.
2828
| Behavior control on Join | `Memoize(table table[ table...])` | Allows the topmost join of a join among the specified tables to Memoize the inner result. Not enforced. |
2929
| | `NoMemoize(table table[ table...])` | Inhibits the topmost join of a join among the specified tables from Memoizing the inner result. |
3030
| Row number correction | `Rows(table table[ table...] correction)` | Corrects row number of a result of the joins on the tables specified. The available correction methods are absolute (#<n>), addition (+<n>), subtract (-<n>) and multiplication (*<n>). <n> should be a string that strtod() can understand. |
31+
| | `ArrayRows(table[ qualifier] correction)` | Adjusts row estimation for array predicates on the tables specified. Targets array operators (`&&`, `@>`, `<@`) and scalar-array comparisons (`<cmp> ANY/ALL (array)`). The correction is absolute (#<n>), addition (+<n>), subtraction (-<n>) or multiplication (*<n>). `qualifier` is optional and can be an operator (`&&`, `@>`, `<@`), a quantifier (`ANY`, `ALL`) or a comparison+quantifier (e.g. `= ANY`, `<> ALL`). |
3132
| Parallel query configuration | `Parallel(table <# of workers> [soft\|hard])` | Enforces or inhibits parallel execution of the specified table. <# of workers> is the desired number of parallel workers, where zero means inhibiting parallel execution. If the third parameter is soft (default), it just changes max\_parallel\_workers\_per\_gather and leaves everything else to the planner. Hard enforces the specified number of workers. |
3233
| GUC | `Set(GUC-param value)` | Sets GUC parameter to the value defined while planner is running. |

docs/hint_table.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,45 @@ from restrictions in the planner. For example:
105105
=# /*+ Rows(a b *10) */ SELECT... ; Makes the number 10 times larger.
106106
```
107107

108+
### Hints for ArrayRows (array predicate row corrections)
109+
110+
This hint, named `ArrayRows`, adjusts row estimation for array-related
111+
predicates. It can be useful when PostgreSQL's default estimate is too crude
112+
(especially when the array expression is not a constant).
113+
114+
`ArrayRows` targets:
115+
116+
- Array operators: `&&`, `@>`, `<@`
117+
- Scalar-array comparisons: `<cmp> ANY (array)` and `<cmp> ALL (array)`
118+
119+
`<cmp>` can be `=`, `<>` (or `!=`), `<`, `<=`, `>` and `>=`.
120+
121+
The last argument is the correction term:
122+
123+
- `#<n>` sets the estimated number of rows produced by the matching predicate(s)
124+
- `+<n>` increases the estimated rows by the given amount
125+
- `-<n>` decreases the estimated rows by the given amount
126+
- `*<n>` scales the matching predicate(s) by the given factor
127+
128+
The optional `qualifier` can narrow the hint down to only a specific operator
129+
or quantifier, for example `&&`, `ANY`, or `= ANY`. If multiple matching
130+
predicates exist for the same relation set, the correction is distributed
131+
evenly across them.
132+
133+
If multiple `ArrayRows` hints match the same predicate, the most specific
134+
qualifier wins (comparison+quantifier like `= ANY` is preferred over `ANY`/`ALL`
135+
or an array operator like `&&`, which are preferred over no qualifier).
136+
137+
For inheritance/partitioned tables, it is usually best to target the parent
138+
relation name/alias; child rels may appear in `EXPLAIN` with planner-generated
139+
aliases like `parent_1`, `parent_2`, but the parent hint still applies.
140+
141+
```sql
142+
=# /*+ ArrayRows(t && #10) */ EXPLAIN SELECT * FROM t WHERE t.tags && some_array();
143+
=# /*+ ArrayRows(t = ANY #100) */ EXPLAIN SELECT * FROM t WHERE t.id = ANY(some_array());
144+
=# /*+ ArrayRows(t ANY *0.1) */ EXPLAIN SELECT * FROM t WHERE t.id < ANY(some_array());
145+
```
146+
108147
### Hints for parallel plans
109148

110149
This hint, named `Parallel`, enforces parallel execution configuration

0 commit comments

Comments
 (0)