Consistently use "superuser" instead of "super user"
[pgsql.git] / src / test / regress / expected / tuplesort.out
blob418f296a3f9e40d056cbd98ee487e84b9694352a
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 (
9     id serial not null,
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)
15     SELECT
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);
21 -- and a few NULLs
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;
30 ----
31 -- Check sort node uses of tuplesort wrt. abbreviated keys
32 ----
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
49                                       | 
50                                       | 
51                                       | 
52 (15 rows)
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
72 (15 rows)
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
90                                       | 
91                                       | 
92                                       | 
93 (15 rows)
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
113 (15 rows)
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
124 (5 rows)
126 SELECT abort_increasing, noabort_increasing FROM abbrev_abort_uuids ORDER BY noabort_increasing NULLS FIRST LIMIT 5;
127            abort_increasing           |          noabort_increasing          
128 --------------------------------------+--------------------------------------
129                                       | 
130                                       | 
131                                       | 
132  00000000-0000-0000-0000-000000000000 | 00000000-0000-0000-0000-000000000000
133  00000000-0000-0000-0000-000000010009 | 00000000-0000-0000-0000-000000010009
134 (5 rows)
136 ----
137 -- Check index creation uses of tuplesort wrt. abbreviated keys
138 ----
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);
142 -- verify
143 EXPLAIN (COSTS OFF)
144 SELECT id, noabort_increasing, noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_increasing LIMIT 5;
145                                        QUERY PLAN                                        
146 -----------------------------------------------------------------------------------------
147  Limit
148    ->  Index Scan using abbrev_abort_uuids__noabort_increasing_idx on abbrev_abort_uuids
149 (2 rows)
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
159 (5 rows)
161 EXPLAIN (COSTS OFF)
162 SELECT id, noabort_increasing, noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_decreasing LIMIT 5;
163                                        QUERY PLAN                                        
164 -----------------------------------------------------------------------------------------
165  Limit
166    ->  Index Scan using abbrev_abort_uuids__noabort_decreasing_idx on abbrev_abort_uuids
167 (2 rows)
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
177 (5 rows)
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);
182 -- verify
183 EXPLAIN (COSTS OFF)
184 SELECT id, abort_increasing, abort_decreasing FROM abbrev_abort_uuids ORDER BY abort_increasing LIMIT 5;
185                                       QUERY PLAN                                       
186 ---------------------------------------------------------------------------------------
187  Limit
188    ->  Index Scan using abbrev_abort_uuids__abort_increasing_idx on abbrev_abort_uuids
189 (2 rows)
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
199 (5 rows)
201 EXPLAIN (COSTS OFF)
202 SELECT id, abort_increasing, abort_decreasing FROM abbrev_abort_uuids ORDER BY abort_decreasing LIMIT 5;
203                                       QUERY PLAN                                       
204 ---------------------------------------------------------------------------------------
205  Limit
206    ->  Index Scan using abbrev_abort_uuids__abort_decreasing_idx on abbrev_abort_uuids
207 (2 rows)
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
217 (5 rows)
219 ----
220 -- Check CLUSTER uses of tuplesort wrt. abbreviated keys
221 ----
222 -- when aborting, increasing order
223 BEGIN;
224 SET LOCAL enable_indexscan = false;
225 CLUSTER abbrev_abort_uuids USING abbrev_abort_uuids__abort_increasing_idx;
226 -- head
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
237 (5 rows)
239 -- tail
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 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
245      0 |                                      |                                      |                                      | 
246  20002 |                                      |                                      |                                      | 
247  20003 |                                      |                                      |                                      | 
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
250 (5 rows)
252 ROLLBACK;
253 -- when aborting, decreasing order
254 BEGIN;
255 SET LOCAL enable_indexscan = false;
256 CLUSTER abbrev_abort_uuids USING abbrev_abort_uuids__abort_decreasing_idx;
257 -- head
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
268 (5 rows)
270 -- tail
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 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
276      0 |                                      |                                      |                                      | 
277  20002 |                                      |                                      |                                      | 
278  20003 |                                      |                                      |                                      | 
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
281 (5 rows)
283 ROLLBACK;
284 -- when not aborting, increasing order
285 BEGIN;
286 SET LOCAL enable_indexscan = false;
287 CLUSTER abbrev_abort_uuids USING abbrev_abort_uuids__noabort_increasing_idx;
288 -- head
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
299 (5 rows)
301 -- tail
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 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
307      0 |                                      |                                      |                                      | 
308  20002 |                                      |                                      |                                      | 
309  20003 |                                      |                                      |                                      | 
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
312 (5 rows)
314 ROLLBACK;
315 -- when no aborting, decreasing order
316 BEGIN;
317 SET LOCAL enable_indexscan = false;
318 CLUSTER abbrev_abort_uuids USING abbrev_abort_uuids__noabort_decreasing_idx;
319 -- head
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
330 (5 rows)
332 -- tail
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 -------+--------------------------------------+--------------------------------------+--------------------------------------+--------------------------------------
338      0 |                                      |                                      |                                      | 
339  20003 |                                      |                                      |                                      | 
340  20002 |                                      |                                      |                                      | 
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
343 (5 rows)
345 ROLLBACK;
346 ----
347 -- test forward and backward scans for in-memory and disk based tuplesort
348 ----
349 -- in-memory
350 BEGIN;
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;
355               QUERY PLAN              
356 --------------------------------------
357  Sort
358    Sort Key: noabort_decreasing
359    ->  Seq Scan on abbrev_abort_uuids
360 (3 rows)
362 DECLARE c SCROLL CURSOR FOR SELECT noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_decreasing;
363 -- first and second
364 FETCH NEXT FROM c;
365           noabort_decreasing          
366 --------------------------------------
367  00000000-0000-0000-0000-000000000000
368 (1 row)
370 FETCH NEXT FROM c;
371           noabort_decreasing          
372 --------------------------------------
373  00000000-0000-0000-0000-000000000000
374 (1 row)
376 -- scroll beyond beginning
377 FETCH BACKWARD FROM c;
378           noabort_decreasing          
379 --------------------------------------
380  00000000-0000-0000-0000-000000000000
381 (1 row)
383 FETCH BACKWARD FROM c;
384  noabort_decreasing 
385 --------------------
386 (0 rows)
388 FETCH BACKWARD FROM c;
389  noabort_decreasing 
390 --------------------
391 (0 rows)
393 FETCH BACKWARD FROM c;
394  noabort_decreasing 
395 --------------------
396 (0 rows)
398 FETCH NEXT FROM c;
399           noabort_decreasing          
400 --------------------------------------
401  00000000-0000-0000-0000-000000000000
402 (1 row)
404 -- scroll beyond end end
405 FETCH LAST FROM c;
406  noabort_decreasing 
407 --------------------
409 (1 row)
411 FETCH BACKWARD FROM c;
412  noabort_decreasing 
413 --------------------
415 (1 row)
417 FETCH NEXT FROM c;
418  noabort_decreasing 
419 --------------------
421 (1 row)
423 FETCH NEXT FROM c;
424  noabort_decreasing 
425 --------------------
426 (0 rows)
428 FETCH NEXT FROM c;
429  noabort_decreasing 
430 --------------------
431 (0 rows)
433 FETCH BACKWARD FROM c;
434  noabort_decreasing 
435 --------------------
437 (1 row)
439 FETCH NEXT FROM c;
440  noabort_decreasing 
441 --------------------
442 (0 rows)
444 COMMIT;
445 -- disk based
446 BEGIN;
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;
452               QUERY PLAN              
453 --------------------------------------
454  Sort
455    Sort Key: noabort_decreasing
456    ->  Seq Scan on abbrev_abort_uuids
457 (3 rows)
459 DECLARE c SCROLL CURSOR FOR SELECT noabort_decreasing FROM abbrev_abort_uuids ORDER BY noabort_decreasing;
460 -- first and second
461 FETCH NEXT FROM c;
462           noabort_decreasing          
463 --------------------------------------
464  00000000-0000-0000-0000-000000000000
465 (1 row)
467 FETCH NEXT FROM c;
468           noabort_decreasing          
469 --------------------------------------
470  00000000-0000-0000-0000-000000000000
471 (1 row)
473 -- scroll beyond beginning
474 FETCH BACKWARD FROM c;
475           noabort_decreasing          
476 --------------------------------------
477  00000000-0000-0000-0000-000000000000
478 (1 row)
480 FETCH BACKWARD FROM c;
481  noabort_decreasing 
482 --------------------
483 (0 rows)
485 FETCH BACKWARD FROM c;
486  noabort_decreasing 
487 --------------------
488 (0 rows)
490 FETCH BACKWARD FROM c;
491  noabort_decreasing 
492 --------------------
493 (0 rows)
495 FETCH NEXT FROM c;
496           noabort_decreasing          
497 --------------------------------------
498  00000000-0000-0000-0000-000000000000
499 (1 row)
501 -- scroll beyond end end
502 FETCH LAST FROM c;
503  noabort_decreasing 
504 --------------------
506 (1 row)
508 FETCH BACKWARD FROM c;
509  noabort_decreasing 
510 --------------------
512 (1 row)
514 FETCH NEXT FROM c;
515  noabort_decreasing 
516 --------------------
518 (1 row)
520 FETCH NEXT FROM c;
521  noabort_decreasing 
522 --------------------
523 (0 rows)
525 FETCH NEXT FROM c;
526  noabort_decreasing 
527 --------------------
528 (0 rows)
530 FETCH BACKWARD FROM c;
531  noabort_decreasing 
532 --------------------
534 (1 row)
536 FETCH NEXT FROM c;
537  noabort_decreasing 
538 --------------------
539 (0 rows)
541 COMMIT;
542 ----
543 -- test tuplesort using both in-memory and disk sort
545 -- memory based
546 SELECT
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)
563 FROM (
564     SELECT * FROM abbrev_abort_uuids
565     UNION ALL
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
570 (1 row)
572 -- disk based (see also above)
573 BEGIN;
574 SET LOCAL work_mem = '100kB';
575 SELECT
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)
584 FROM (
585     SELECT * FROM abbrev_abort_uuids
586     UNION ALL
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
591 (1 row)
593 ROLLBACK;
594 ----
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);
601 BEGIN;
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
606 SELECT $$
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)
610     GROUP BY 1
611     HAVING count(*) > 1
612     ORDER BY 2 DESC, 1 DESC, 3 DESC, 4 DESC, 5 DESC, 6 DESC
613     LIMIT 10
614 $$ AS qry \gset
615 -- test mark/restore with in-memory sorts
616 EXPLAIN (COSTS OFF) :qry;
617                                                                                  QUERY PLAN                                                                                  
618 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
619  Limit
620    ->  Sort
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
622          ->  GroupAggregate
623                Group Key: a.col12
624                Filter: (count(*) > 1)
625                ->  Merge Join
626                      Merge Cond: (a.col12 = b.col12)
627                      ->  Sort
628                            Sort Key: a.col12 DESC
629                            ->  Seq Scan on test_mark_restore a
630                      ->  Sort
631                            Sort Key: b.col12 DESC
632                            ->  Seq Scan on test_mark_restore b
633 (14 rows)
635 :qry;
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
648 (10 rows)
650 -- test mark/restore with on-disk sorts
651 SET LOCAL work_mem = '100kB';
652 EXPLAIN (COSTS OFF) :qry;
653                                                                                  QUERY PLAN                                                                                  
654 -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
655  Limit
656    ->  Sort
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
658          ->  GroupAggregate
659                Group Key: a.col12
660                Filter: (count(*) > 1)
661                ->  Merge Join
662                      Merge Cond: (a.col12 = b.col12)
663                      ->  Sort
664                            Sort Key: a.col12 DESC
665                            ->  Seq Scan on test_mark_restore a
666                      ->  Sort
667                            Sort Key: b.col12 DESC
668                            ->  Seq Scan on test_mark_restore b
669 (14 rows)
671 :qry;
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
684 (10 rows)
686 COMMIT;