1 -- only use parallelism when explicitly intending to do so
2 SET max_parallel_maintenance_workers = 0;
3 SET max_parallel_workers = 0;
4 -- A table with contents that, when sorted, triggers abbreviated
5 -- key aborts. One easy way to achieve that is to use uuids that all
6 -- have the same prefix, as abbreviated keys for uuids just use the
7 -- first sizeof(Datum) bytes.
8 CREATE TEMP TABLE abbrev_abort_uuids (
10 abort_increasing uuid,
11 abort_decreasing uuid,
12 noabort_increasing uuid,
13 noabort_decreasing uuid);
14 INSERT INTO abbrev_abort_uuids (abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing)
16 ('00000000-0000-0000-0000-'||to_char(g.i, '000000000000FM'))::uuid abort_increasing,
17 ('00000000-0000-0000-0000-'||to_char(20000 - g.i, '000000000000FM'))::uuid abort_decreasing,
18 (to_char(g.i % 10009, '00000000FM')||'-0000-0000-0000-'||to_char(g.i, '000000000000FM'))::uuid noabort_increasing,
19 (to_char(((20000 - g.i) % 10009), '00000000FM')||'-0000-0000-0000-'||to_char(20000 - g.i, '000000000000FM'))::uuid noabort_decreasing
20 FROM generate_series(0, 20000, 1) g(i);
22 INSERT INTO abbrev_abort_uuids(id) VALUES(0);
23 INSERT INTO abbrev_abort_uuids DEFAULT VALUES;
24 INSERT INTO abbrev_abort_uuids DEFAULT VALUES;
25 -- add just a few duplicates
26 INSERT INTO abbrev_abort_uuids (abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing)
27 SELECT abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing
28 FROM abbrev_abort_uuids
29 WHERE (id < 10 OR id > 19990) AND id % 3 = 0 AND abort_increasing is not null;
31 -- Check sort node uses of tuplesort wrt. abbreviated keys
33 -- plain sort triggering abbreviated abort
34 SELECT abort_increasing, abort_decreasing FROM abbrev_abort_uuids ORDER BY abort_increasing OFFSET 20000 - 4;
35 abort_increasing | abort_decreasing
36 --------------------------------------+--------------------------------------
37 00000000-0000-0000-0000-000000019992 | 00000000-0000-0000-0000-000000000008
38 00000000-0000-0000-0000-000000019993 | 00000000-0000-0000-0000-000000000007
39 00000000-0000-0000-0000-000000019994 | 00000000-0000-0000-0000-000000000006
40 00000000-0000-0000-0000-000000019994 | 00000000-0000-0000-0000-000000000006
41 00000000-0000-0000-0000-000000019995 | 00000000-0000-0000-0000-000000000005
42 00000000-0000-0000-0000-000000019996 | 00000000-0000-0000-0000-000000000004
43 00000000-0000-0000-0000-000000019997 | 00000000-0000-0000-0000-000000000003
44 00000000-0000-0000-0000-000000019997 | 00000000-0000-0000-0000-000000000003
45 00000000-0000-0000-0000-000000019998 | 00000000-0000-0000-0000-000000000002
46 00000000-0000-0000-0000-000000019999 | 00000000-0000-0000-0000-000000000001
47 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
48 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
54 SELECT abort_increasing, abort_decreasing FROM abbrev_abort_uuids ORDER BY abort_decreasing NULLS FIRST OFFSET 20000 - 4;
55 abort_increasing | abort_decreasing
56 --------------------------------------+--------------------------------------
57 00000000-0000-0000-0000-000000000011 | 00000000-0000-0000-0000-000000019989
58 00000000-0000-0000-0000-000000000010 | 00000000-0000-0000-0000-000000019990
59 00000000-0000-0000-0000-000000000009 | 00000000-0000-0000-0000-000000019991
60 00000000-0000-0000-0000-000000000008 | 00000000-0000-0000-0000-000000019992
61 00000000-0000-0000-0000-000000000008 | 00000000-0000-0000-0000-000000019992
62 00000000-0000-0000-0000-000000000007 | 00000000-0000-0000-0000-000000019993
63 00000000-0000-0000-0000-000000000006 | 00000000-0000-0000-0000-000000019994
64 00000000-0000-0000-0000-000000000005 | 00000000-0000-0000-0000-000000019995
65 00000000-0000-0000-0000-000000000005 | 00000000-0000-0000-0000-000000019995
66 00000000-0000-0000-0000-000000000004 | 00000000-0000-0000-0000-000000019996
67 00000000-0000-0000-0000-000000000003 | 00000000-0000-0000-0000-000000019997
68 00000000-0000-0000-0000-000000000002 | 00000000-0000-0000-0000-000000019998
69 00000000-0000-0000-0000-000000000002 | 00000000-0000-0000-0000-000000019998
70 00000000-0000-0000-0000-000000000001 | 00000000-0000-0000-0000-000000019999
71 00000000-0000-0000-0000-000000000000 | 00000000-0000-0000-0000-000000020000
74 -- plain sort not triggering abbreviated abort
75 SELECT noabort_increasing, noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_increasing OFFSET 20000 - 4;
76 noabort_increasing | noabort_decreasing
77 --------------------------------------+--------------------------------------
78 00009997-0000-0000-0000-000000009997 | 00010003-0000-0000-0000-000000010003
79 00009998-0000-0000-0000-000000009998 | 00010002-0000-0000-0000-000000010002
80 00009999-0000-0000-0000-000000009999 | 00010001-0000-0000-0000-000000010001
81 00010000-0000-0000-0000-000000010000 | 00010000-0000-0000-0000-000000010000
82 00010001-0000-0000-0000-000000010001 | 00009999-0000-0000-0000-000000009999
83 00010002-0000-0000-0000-000000010002 | 00009998-0000-0000-0000-000000009998
84 00010003-0000-0000-0000-000000010003 | 00009997-0000-0000-0000-000000009997
85 00010004-0000-0000-0000-000000010004 | 00009996-0000-0000-0000-000000009996
86 00010005-0000-0000-0000-000000010005 | 00009995-0000-0000-0000-000000009995
87 00010006-0000-0000-0000-000000010006 | 00009994-0000-0000-0000-000000009994
88 00010007-0000-0000-0000-000000010007 | 00009993-0000-0000-0000-000000009993
89 00010008-0000-0000-0000-000000010008 | 00009992-0000-0000-0000-000000009992
95 SELECT noabort_increasing, noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_decreasing NULLS FIRST OFFSET 20000 - 4;
96 noabort_increasing | noabort_decreasing
97 --------------------------------------+--------------------------------------
98 00010006-0000-0000-0000-000000010006 | 00009994-0000-0000-0000-000000009994
99 00010005-0000-0000-0000-000000010005 | 00009995-0000-0000-0000-000000009995
100 00010004-0000-0000-0000-000000010004 | 00009996-0000-0000-0000-000000009996
101 00010003-0000-0000-0000-000000010003 | 00009997-0000-0000-0000-000000009997
102 00010002-0000-0000-0000-000000010002 | 00009998-0000-0000-0000-000000009998
103 00010001-0000-0000-0000-000000010001 | 00009999-0000-0000-0000-000000009999
104 00010000-0000-0000-0000-000000010000 | 00010000-0000-0000-0000-000000010000
105 00009999-0000-0000-0000-000000009999 | 00010001-0000-0000-0000-000000010001
106 00009998-0000-0000-0000-000000009998 | 00010002-0000-0000-0000-000000010002
107 00009997-0000-0000-0000-000000009997 | 00010003-0000-0000-0000-000000010003
108 00009996-0000-0000-0000-000000009996 | 00010004-0000-0000-0000-000000010004
109 00009995-0000-0000-0000-000000009995 | 00010005-0000-0000-0000-000000010005
110 00009994-0000-0000-0000-000000009994 | 00010006-0000-0000-0000-000000010006
111 00009993-0000-0000-0000-000000009993 | 00010007-0000-0000-0000-000000010007
112 00009992-0000-0000-0000-000000009992 | 00010008-0000-0000-0000-000000010008
115 -- bounded sort (disables abbreviated keys)
116 SELECT abort_increasing, noabort_increasing FROM abbrev_abort_uuids ORDER BY abort_increasing LIMIT 5;
117 abort_increasing | noabort_increasing
118 --------------------------------------+--------------------------------------
119 00000000-0000-0000-0000-000000000000 | 00000000-0000-0000-0000-000000000000
120 00000000-0000-0000-0000-000000000001 | 00000001-0000-0000-0000-000000000001
121 00000000-0000-0000-0000-000000000002 | 00000002-0000-0000-0000-000000000002
122 00000000-0000-0000-0000-000000000002 | 00000002-0000-0000-0000-000000000002
123 00000000-0000-0000-0000-000000000003 | 00000003-0000-0000-0000-000000000003
126 SELECT abort_increasing, noabort_increasing FROM abbrev_abort_uuids ORDER BY noabort_increasing NULLS FIRST LIMIT 5;
127 abort_increasing | noabort_increasing
128 --------------------------------------+--------------------------------------
132 00000000-0000-0000-0000-000000000000 | 00000000-0000-0000-0000-000000000000
133 00000000-0000-0000-0000-000000010009 | 00000000-0000-0000-0000-000000010009
137 -- Check index creation uses of tuplesort wrt. abbreviated keys
139 -- index creation using abbreviated keys successfully
140 CREATE INDEX abbrev_abort_uuids__noabort_increasing_idx ON abbrev_abort_uuids (noabort_increasing);
141 CREATE INDEX abbrev_abort_uuids__noabort_decreasing_idx ON abbrev_abort_uuids (noabort_decreasing);
144 SELECT id, noabort_increasing, noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_increasing LIMIT 5;
146 -----------------------------------------------------------------------------------------
148 -> Index Scan using abbrev_abort_uuids__noabort_increasing_idx on abbrev_abort_uuids
151 SELECT id, noabort_increasing, noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_increasing LIMIT 5;
152 id | noabort_increasing | noabort_decreasing
153 -------+--------------------------------------+--------------------------------------
154 1 | 00000000-0000-0000-0000-000000000000 | 00009991-0000-0000-0000-000000020000
155 10010 | 00000000-0000-0000-0000-000000010009 | 00009991-0000-0000-0000-000000009991
156 2 | 00000001-0000-0000-0000-000000000001 | 00009990-0000-0000-0000-000000019999
157 10011 | 00000001-0000-0000-0000-000000010010 | 00009990-0000-0000-0000-000000009990
158 3 | 00000002-0000-0000-0000-000000000002 | 00009989-0000-0000-0000-000000019998
162 SELECT id, noabort_increasing, noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_decreasing LIMIT 5;
164 -----------------------------------------------------------------------------------------
166 -> Index Scan using abbrev_abort_uuids__noabort_decreasing_idx on abbrev_abort_uuids
169 SELECT id, noabort_increasing, noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_decreasing LIMIT 5;
170 id | noabort_increasing | noabort_decreasing
171 -------+--------------------------------------+--------------------------------------
172 20001 | 00009991-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
173 20010 | 00009991-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
174 9992 | 00009991-0000-0000-0000-000000009991 | 00000000-0000-0000-0000-000000010009
175 20000 | 00009990-0000-0000-0000-000000019999 | 00000001-0000-0000-0000-000000000001
176 9991 | 00009990-0000-0000-0000-000000009990 | 00000001-0000-0000-0000-000000010010
179 -- index creation using abbreviated keys, hitting abort
180 CREATE INDEX abbrev_abort_uuids__abort_increasing_idx ON abbrev_abort_uuids (abort_increasing);
181 CREATE INDEX abbrev_abort_uuids__abort_decreasing_idx ON abbrev_abort_uuids (abort_decreasing);
184 SELECT id, abort_increasing, abort_decreasing FROM abbrev_abort_uuids ORDER BY abort_increasing LIMIT 5;
186 ---------------------------------------------------------------------------------------
188 -> Index Scan using abbrev_abort_uuids__abort_increasing_idx on abbrev_abort_uuids
191 SELECT id, abort_increasing, abort_decreasing FROM abbrev_abort_uuids ORDER BY abort_increasing LIMIT 5;
192 id | abort_increasing | abort_decreasing
193 -------+--------------------------------------+--------------------------------------
194 1 | 00000000-0000-0000-0000-000000000000 | 00000000-0000-0000-0000-000000020000
195 2 | 00000000-0000-0000-0000-000000000001 | 00000000-0000-0000-0000-000000019999
196 3 | 00000000-0000-0000-0000-000000000002 | 00000000-0000-0000-0000-000000019998
197 20004 | 00000000-0000-0000-0000-000000000002 | 00000000-0000-0000-0000-000000019998
198 4 | 00000000-0000-0000-0000-000000000003 | 00000000-0000-0000-0000-000000019997
202 SELECT id, abort_increasing, abort_decreasing FROM abbrev_abort_uuids ORDER BY abort_decreasing LIMIT 5;
204 ---------------------------------------------------------------------------------------
206 -> Index Scan using abbrev_abort_uuids__abort_decreasing_idx on abbrev_abort_uuids
209 SELECT id, abort_increasing, abort_decreasing FROM abbrev_abort_uuids ORDER BY abort_decreasing LIMIT 5;
210 id | abort_increasing | abort_decreasing
211 -------+--------------------------------------+--------------------------------------
212 20001 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
213 20010 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
214 20000 | 00000000-0000-0000-0000-000000019999 | 00000000-0000-0000-0000-000000000001
215 19999 | 00000000-0000-0000-0000-000000019998 | 00000000-0000-0000-0000-000000000002
216 19998 | 00000000-0000-0000-0000-000000019997 | 00000000-0000-0000-0000-000000000003
220 -- Check CLUSTER uses of tuplesort wrt. abbreviated keys
222 -- when aborting, increasing order
224 SET LOCAL enable_indexscan = false;
225 CLUSTER abbrev_abort_uuids USING abbrev_abort_uuids__abort_increasing_idx;
227 SELECT id, abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing
228 FROM abbrev_abort_uuids
229 ORDER BY ctid LIMIT 5;
230 id | abort_increasing | abort_decreasing | noabort_increasing | noabort_decreasing
231 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
232 1 | 00000000-0000-0000-0000-000000000000 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000 | 00009991-0000-0000-0000-000000020000
233 2 | 00000000-0000-0000-0000-000000000001 | 00000000-0000-0000-0000-000000019999 | 00000001-0000-0000-0000-000000000001 | 00009990-0000-0000-0000-000000019999
234 3 | 00000000-0000-0000-0000-000000000002 | 00000000-0000-0000-0000-000000019998 | 00000002-0000-0000-0000-000000000002 | 00009989-0000-0000-0000-000000019998
235 20004 | 00000000-0000-0000-0000-000000000002 | 00000000-0000-0000-0000-000000019998 | 00000002-0000-0000-0000-000000000002 | 00009989-0000-0000-0000-000000019998
236 4 | 00000000-0000-0000-0000-000000000003 | 00000000-0000-0000-0000-000000019997 | 00000003-0000-0000-0000-000000000003 | 00009988-0000-0000-0000-000000019997
240 SELECT id, abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing
241 FROM abbrev_abort_uuids
242 ORDER BY ctid DESC LIMIT 5;
243 id | abort_increasing | abort_decreasing | noabort_increasing | noabort_decreasing
244 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
248 20001 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000 | 00009991-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
249 20010 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000 | 00009991-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
253 -- when aborting, decreasing order
255 SET LOCAL enable_indexscan = false;
256 CLUSTER abbrev_abort_uuids USING abbrev_abort_uuids__abort_decreasing_idx;
258 SELECT id, abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing
259 FROM abbrev_abort_uuids
260 ORDER BY ctid LIMIT 5;
261 id | abort_increasing | abort_decreasing | noabort_increasing | noabort_decreasing
262 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
263 20010 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000 | 00009991-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
264 20001 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000 | 00009991-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
265 20000 | 00000000-0000-0000-0000-000000019999 | 00000000-0000-0000-0000-000000000001 | 00009990-0000-0000-0000-000000019999 | 00000001-0000-0000-0000-000000000001
266 19999 | 00000000-0000-0000-0000-000000019998 | 00000000-0000-0000-0000-000000000002 | 00009989-0000-0000-0000-000000019998 | 00000002-0000-0000-0000-000000000002
267 20009 | 00000000-0000-0000-0000-000000019997 | 00000000-0000-0000-0000-000000000003 | 00009988-0000-0000-0000-000000019997 | 00000003-0000-0000-0000-000000000003
271 SELECT id, abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing
272 FROM abbrev_abort_uuids
273 ORDER BY ctid DESC LIMIT 5;
274 id | abort_increasing | abort_decreasing | noabort_increasing | noabort_decreasing
275 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
279 1 | 00000000-0000-0000-0000-000000000000 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000 | 00009991-0000-0000-0000-000000020000
280 2 | 00000000-0000-0000-0000-000000000001 | 00000000-0000-0000-0000-000000019999 | 00000001-0000-0000-0000-000000000001 | 00009990-0000-0000-0000-000000019999
284 -- when not aborting, increasing order
286 SET LOCAL enable_indexscan = false;
287 CLUSTER abbrev_abort_uuids USING abbrev_abort_uuids__noabort_increasing_idx;
289 SELECT id, abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing
290 FROM abbrev_abort_uuids
291 ORDER BY ctid LIMIT 5;
292 id | abort_increasing | abort_decreasing | noabort_increasing | noabort_decreasing
293 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
294 1 | 00000000-0000-0000-0000-000000000000 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000 | 00009991-0000-0000-0000-000000020000
295 10010 | 00000000-0000-0000-0000-000000010009 | 00000000-0000-0000-0000-000000009991 | 00000000-0000-0000-0000-000000010009 | 00009991-0000-0000-0000-000000009991
296 2 | 00000000-0000-0000-0000-000000000001 | 00000000-0000-0000-0000-000000019999 | 00000001-0000-0000-0000-000000000001 | 00009990-0000-0000-0000-000000019999
297 10011 | 00000000-0000-0000-0000-000000010010 | 00000000-0000-0000-0000-000000009990 | 00000001-0000-0000-0000-000000010010 | 00009990-0000-0000-0000-000000009990
298 20004 | 00000000-0000-0000-0000-000000000002 | 00000000-0000-0000-0000-000000019998 | 00000002-0000-0000-0000-000000000002 | 00009989-0000-0000-0000-000000019998
302 SELECT id, abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing
303 FROM abbrev_abort_uuids
304 ORDER BY ctid DESC LIMIT 5;
305 id | abort_increasing | abort_decreasing | noabort_increasing | noabort_decreasing
306 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
310 10009 | 00000000-0000-0000-0000-000000010008 | 00000000-0000-0000-0000-000000009992 | 00010008-0000-0000-0000-000000010008 | 00009992-0000-0000-0000-000000009992
311 10008 | 00000000-0000-0000-0000-000000010007 | 00000000-0000-0000-0000-000000009993 | 00010007-0000-0000-0000-000000010007 | 00009993-0000-0000-0000-000000009993
315 -- when no aborting, decreasing order
317 SET LOCAL enable_indexscan = false;
318 CLUSTER abbrev_abort_uuids USING abbrev_abort_uuids__noabort_decreasing_idx;
320 SELECT id, abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing
321 FROM abbrev_abort_uuids
322 ORDER BY ctid LIMIT 5;
323 id | abort_increasing | abort_decreasing | noabort_increasing | noabort_decreasing
324 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
325 20010 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000 | 00009991-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
326 20001 | 00000000-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000 | 00009991-0000-0000-0000-000000020000 | 00000000-0000-0000-0000-000000000000
327 9992 | 00000000-0000-0000-0000-000000009991 | 00000000-0000-0000-0000-000000010009 | 00009991-0000-0000-0000-000000009991 | 00000000-0000-0000-0000-000000010009
328 20000 | 00000000-0000-0000-0000-000000019999 | 00000000-0000-0000-0000-000000000001 | 00009990-0000-0000-0000-000000019999 | 00000001-0000-0000-0000-000000000001
329 9991 | 00000000-0000-0000-0000-000000009990 | 00000000-0000-0000-0000-000000010010 | 00009990-0000-0000-0000-000000009990 | 00000001-0000-0000-0000-000000010010
333 SELECT id, abort_increasing, abort_decreasing, noabort_increasing, noabort_decreasing
334 FROM abbrev_abort_uuids
335 ORDER BY ctid DESC LIMIT 5;
336 id | abort_increasing | abort_decreasing | noabort_increasing | noabort_decreasing
337 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
341 9993 | 00000000-0000-0000-0000-000000009992 | 00000000-0000-0000-0000-000000010008 | 00009992-0000-0000-0000-000000009992 | 00010008-0000-0000-0000-000000010008
342 9994 | 00000000-0000-0000-0000-000000009993 | 00000000-0000-0000-0000-000000010007 | 00009993-0000-0000-0000-000000009993 | 00010007-0000-0000-0000-000000010007
347 -- test forward and backward scans for in-memory and disk based tuplesort
351 SET LOCAL enable_indexscan = false;
352 -- unfortunately can't show analyze output confirming sort method,
353 -- the memory used output wouldn't be stable
354 EXPLAIN (COSTS OFF) DECLARE c SCROLL CURSOR FOR SELECT noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_decreasing;
356 --------------------------------------
358 Sort Key: noabort_decreasing
359 -> Seq Scan on abbrev_abort_uuids
362 DECLARE c SCROLL CURSOR FOR SELECT noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_decreasing;
366 --------------------------------------
367 00000000-0000-0000-0000-000000000000
372 --------------------------------------
373 00000000-0000-0000-0000-000000000000
376 -- scroll beyond beginning
377 FETCH BACKWARD FROM c;
379 --------------------------------------
380 00000000-0000-0000-0000-000000000000
383 FETCH BACKWARD FROM c;
388 FETCH BACKWARD FROM c;
393 FETCH BACKWARD FROM c;
400 --------------------------------------
401 00000000-0000-0000-0000-000000000000
404 -- scroll beyond end end
411 FETCH BACKWARD FROM c;
433 FETCH BACKWARD FROM c;
447 SET LOCAL enable_indexscan = false;
448 SET LOCAL work_mem = '100kB';
449 -- unfortunately can't show analyze output confirming sort method,
450 -- the memory used output wouldn't be stable
451 EXPLAIN (COSTS OFF) DECLARE c SCROLL CURSOR FOR SELECT noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_decreasing;
453 --------------------------------------
455 Sort Key: noabort_decreasing
456 -> Seq Scan on abbrev_abort_uuids
459 DECLARE c SCROLL CURSOR FOR SELECT noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_decreasing;
463 --------------------------------------
464 00000000-0000-0000-0000-000000000000
469 --------------------------------------
470 00000000-0000-0000-0000-000000000000
473 -- scroll beyond beginning
474 FETCH BACKWARD FROM c;
476 --------------------------------------
477 00000000-0000-0000-0000-000000000000
480 FETCH BACKWARD FROM c;
485 FETCH BACKWARD FROM c;
490 FETCH BACKWARD FROM c;
497 --------------------------------------
498 00000000-0000-0000-0000-000000000000
501 -- scroll beyond end end
508 FETCH BACKWARD FROM c;
530 FETCH BACKWARD FROM c;
543 -- test tuplesort using both in-memory and disk sort
547 -- fixed-width by-value datum
548 (array_agg(id ORDER BY id DESC NULLS FIRST))[0:5],
549 -- fixed-width by-ref datum
550 (array_agg(abort_increasing ORDER BY abort_increasing DESC NULLS LAST))[0:5],
551 -- variable-width datum
552 (array_agg(id::text ORDER BY id::text DESC NULLS LAST))[0:5],
553 -- fixed width by-value datum tuplesort
554 percentile_disc(0.99) WITHIN GROUP (ORDER BY id),
555 -- ensure state is shared
556 percentile_disc(0.01) WITHIN GROUP (ORDER BY id),
557 -- fixed width by-ref datum tuplesort
558 percentile_disc(0.8) WITHIN GROUP (ORDER BY abort_increasing),
559 -- variable width by-ref datum tuplesort
560 percentile_disc(0.2) WITHIN GROUP (ORDER BY id::text),
561 -- multi-column tuplesort
562 rank('00000000-0000-0000-0000-000000000000', '2', '2') WITHIN GROUP (ORDER BY noabort_increasing, id, id::text)
564 SELECT * FROM abbrev_abort_uuids
566 SELECT NULL, NULL, NULL, NULL, NULL) s;
567 array_agg | array_agg | array_agg | percentile_disc | percentile_disc | percentile_disc | percentile_disc | rank
568 --------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------------+-----------------+--------------------------------------+-----------------+------
569 {NULL,20010,20009,20008,20007} | {00000000-0000-0000-0000-000000020000,00000000-0000-0000-0000-000000020000,00000000-0000-0000-0000-000000019999,00000000-0000-0000-0000-000000019998,00000000-0000-0000-0000-000000019997} | {9999,9998,9997,9996,9995} | 19810 | 200 | 00000000-0000-0000-0000-000000016003 | 136 | 2
572 -- disk based (see also above)
574 SET LOCAL work_mem = '100kB';
576 (array_agg(id ORDER BY id DESC NULLS FIRST))[0:5],
577 (array_agg(abort_increasing ORDER BY abort_increasing DESC NULLS LAST))[0:5],
578 (array_agg(id::text ORDER BY id::text DESC NULLS LAST))[0:5],
579 percentile_disc(0.99) WITHIN GROUP (ORDER BY id),
580 percentile_disc(0.01) WITHIN GROUP (ORDER BY id),
581 percentile_disc(0.8) WITHIN GROUP (ORDER BY abort_increasing),
582 percentile_disc(0.2) WITHIN GROUP (ORDER BY id::text),
583 rank('00000000-0000-0000-0000-000000000000', '2', '2') WITHIN GROUP (ORDER BY noabort_increasing, id, id::text)
585 SELECT * FROM abbrev_abort_uuids
587 SELECT NULL, NULL, NULL, NULL, NULL) s;
588 array_agg | array_agg | array_agg | percentile_disc | percentile_disc | percentile_disc | percentile_disc | rank
589 --------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------+-----------------+-----------------+--------------------------------------+-----------------+------
590 {NULL,20010,20009,20008,20007} | {00000000-0000-0000-0000-000000020000,00000000-0000-0000-0000-000000020000,00000000-0000-0000-0000-000000019999,00000000-0000-0000-0000-000000019998,00000000-0000-0000-0000-000000019997} | {9999,9998,9997,9996,9995} | 19810 | 200 | 00000000-0000-0000-0000-000000016003 | 136 | 2
595 -- test tuplesort mark/restore
597 CREATE TEMP TABLE test_mark_restore(col1 int, col2 int, col12 int);
598 -- need a few duplicates for mark/restore to matter
599 INSERT INTO test_mark_restore(col1, col2, col12)
600 SELECT a.i, b.i, a.i * b.i FROM generate_series(1, 500) a(i), generate_series(1, 5) b(i);
602 SET LOCAL enable_nestloop = off;
603 SET LOCAL enable_hashjoin = off;
604 SET LOCAL enable_material = off;
605 -- set query into variable once, to avoid repetition of the fairly long query
607 SELECT col12, count(distinct a.col1), count(distinct a.col2), count(distinct b.col1), count(distinct b.col2), count(*)
608 FROM test_mark_restore a
609 JOIN test_mark_restore b USING(col12)
612 ORDER BY 2 DESC, 1 DESC, 3 DESC, 4 DESC, 5 DESC, 6 DESC
615 -- test mark/restore with in-memory sorts
616 EXPLAIN (COSTS OFF) :qry;
618 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
621 Sort Key: (count(DISTINCT a.col1)) DESC, a.col12 DESC, (count(DISTINCT a.col2)) DESC, (count(DISTINCT b.col1)) DESC, (count(DISTINCT b.col2)) DESC, (count(*)) DESC
624 Filter: (count(*) > 1)
626 Merge Cond: (a.col12 = b.col12)
628 Sort Key: a.col12 DESC
629 -> Seq Scan on test_mark_restore a
631 Sort Key: b.col12 DESC
632 -> Seq Scan on test_mark_restore b
636 col12 | count | count | count | count | count
637 -------+-------+-------+-------+-------+-------
638 480 | 5 | 5 | 5 | 5 | 25
639 420 | 5 | 5 | 5 | 5 | 25
640 360 | 5 | 5 | 5 | 5 | 25
641 300 | 5 | 5 | 5 | 5 | 25
642 240 | 5 | 5 | 5 | 5 | 25
643 180 | 5 | 5 | 5 | 5 | 25
644 120 | 5 | 5 | 5 | 5 | 25
645 60 | 5 | 5 | 5 | 5 | 25
646 960 | 4 | 4 | 4 | 4 | 16
647 900 | 4 | 4 | 4 | 4 | 16
650 -- test mark/restore with on-disk sorts
651 SET LOCAL work_mem = '100kB';
652 EXPLAIN (COSTS OFF) :qry;
654 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
657 Sort Key: (count(DISTINCT a.col1)) DESC, a.col12 DESC, (count(DISTINCT a.col2)) DESC, (count(DISTINCT b.col1)) DESC, (count(DISTINCT b.col2)) DESC, (count(*)) DESC
660 Filter: (count(*) > 1)
662 Merge Cond: (a.col12 = b.col12)
664 Sort Key: a.col12 DESC
665 -> Seq Scan on test_mark_restore a
667 Sort Key: b.col12 DESC
668 -> Seq Scan on test_mark_restore b
672 col12 | count | count | count | count | count
673 -------+-------+-------+-------+-------+-------
674 480 | 5 | 5 | 5 | 5 | 25
675 420 | 5 | 5 | 5 | 5 | 25
676 360 | 5 | 5 | 5 | 5 | 25
677 300 | 5 | 5 | 5 | 5 | 25
678 240 | 5 | 5 | 5 | 5 | 25
679 180 | 5 | 5 | 5 | 5 | 25
680 120 | 5 | 5 | 5 | 5 | 25
681 60 | 5 | 5 | 5 | 5 | 25
682 960 | 4 | 4 | 4 | 4 | 16
683 900 | 4 | 4 | 4 | 4 | 16