Commit 0a57352
perf: Eliminate O(n²) in timeline_legal_unit via GUC session variable
The timeline_legal_unit_def view's establishment_aggs CTE scanned ALL
timesegments during partial refresh because PostgreSQL can't push outer
WHERE filters into a view's CTEs. This caused O(n²) growth per batch
(3034ms max, growing with data size).
Solution: A filter_ids CTE reads current_setting('statbus.filter_unit_ids',
true) once per query. When set (partial refresh), all three data CTEs
filter via unit_id = ANY(ids). When NULL/empty (full refresh, ad-hoc),
NULLIF normalizes to NULL and the IS NULL guard passes all rows.
The refresh procedure simply sets the GUC before delegating to
timeline_refresh — no inline SQL duplication, no temp tables.
Result: batch max 877ms (down from 3034ms), confirmed LINEAR O(n)
scaling with ms_per_row_ratio 1.07. Single source of truth preserved.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>1 parent 151b597 commit 0a57352
File tree
9 files changed
+2434
-222
lines changed- migrations
- test/expected
- explain
- performance
9 files changed
+2434
-222
lines changedLines changed: 426 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 690 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 658 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 442 additions & 0 deletions
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
823 | 823 | | |
824 | 824 | | |
825 | 825 | | |
826 | | - | |
| 826 | + | |
827 | 827 | | |
828 | 828 | | |
829 | 829 | | |
| |||
836 | 836 | | |
837 | 837 | | |
838 | 838 | | |
839 | | - | |
| 839 | + | |
840 | 840 | | |
841 | 841 | | |
842 | 842 | | |
| |||
Lines changed: 9 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
31 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| |||
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
45 | | - | |
| 46 | + | |
46 | 47 | | |
47 | | - | |
48 | | - | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
49 | 52 | | |
50 | 53 | | |
51 | 54 | | |
| |||
101 | 104 | | |
102 | 105 | | |
103 | 106 | | |
104 | | - | |
| 107 | + | |
105 | 108 | | |
Lines changed: 56 additions & 53 deletions
Large diffs are not rendered by default.
Lines changed: 146 additions & 156 deletions
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
| 17 | + | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | | - | |
29 | | - | |
| 28 | + | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
39 | | - | |
| 39 | + | |
40 | 40 | | |
0 commit comments