Remove old RULE privilege completely.
[pgsql.git] / src / test / regress / expected / cluster.out
bloba13aafff0b69e992a0e8703163541b9b2f3d90b6
1 --
2 --  CLUSTER
3 --
4 CREATE TABLE clstr_tst_s (rf_a SERIAL PRIMARY KEY,
5         b INT);
6 CREATE TABLE clstr_tst (a SERIAL PRIMARY KEY,
7         b INT,
8         c TEXT,
9         d TEXT,
10         CONSTRAINT clstr_tst_con FOREIGN KEY (b) REFERENCES clstr_tst_s);
11 CREATE INDEX clstr_tst_b ON clstr_tst (b);
12 CREATE INDEX clstr_tst_c ON clstr_tst (c);
13 CREATE INDEX clstr_tst_c_b ON clstr_tst (c,b);
14 CREATE INDEX clstr_tst_b_c ON clstr_tst (b,c);
15 INSERT INTO clstr_tst_s (b) VALUES (0);
16 INSERT INTO clstr_tst_s (b) SELECT b FROM clstr_tst_s;
17 INSERT INTO clstr_tst_s (b) SELECT b FROM clstr_tst_s;
18 INSERT INTO clstr_tst_s (b) SELECT b FROM clstr_tst_s;
19 INSERT INTO clstr_tst_s (b) SELECT b FROM clstr_tst_s;
20 INSERT INTO clstr_tst_s (b) SELECT b FROM clstr_tst_s;
21 CREATE TABLE clstr_tst_inh () INHERITS (clstr_tst);
22 INSERT INTO clstr_tst (b, c) VALUES (11, 'once');
23 INSERT INTO clstr_tst (b, c) VALUES (10, 'diez');
24 INSERT INTO clstr_tst (b, c) VALUES (31, 'treinta y uno');
25 INSERT INTO clstr_tst (b, c) VALUES (22, 'veintidos');
26 INSERT INTO clstr_tst (b, c) VALUES (3, 'tres');
27 INSERT INTO clstr_tst (b, c) VALUES (20, 'veinte');
28 INSERT INTO clstr_tst (b, c) VALUES (23, 'veintitres');
29 INSERT INTO clstr_tst (b, c) VALUES (21, 'veintiuno');
30 INSERT INTO clstr_tst (b, c) VALUES (4, 'cuatro');
31 INSERT INTO clstr_tst (b, c) VALUES (14, 'catorce');
32 INSERT INTO clstr_tst (b, c) VALUES (2, 'dos');
33 INSERT INTO clstr_tst (b, c) VALUES (18, 'dieciocho');
34 INSERT INTO clstr_tst (b, c) VALUES (27, 'veintisiete');
35 INSERT INTO clstr_tst (b, c) VALUES (25, 'veinticinco');
36 INSERT INTO clstr_tst (b, c) VALUES (13, 'trece');
37 INSERT INTO clstr_tst (b, c) VALUES (28, 'veintiocho');
38 INSERT INTO clstr_tst (b, c) VALUES (32, 'treinta y dos');
39 INSERT INTO clstr_tst (b, c) VALUES (5, 'cinco');
40 INSERT INTO clstr_tst (b, c) VALUES (29, 'veintinueve');
41 INSERT INTO clstr_tst (b, c) VALUES (1, 'uno');
42 INSERT INTO clstr_tst (b, c) VALUES (24, 'veinticuatro');
43 INSERT INTO clstr_tst (b, c) VALUES (30, 'treinta');
44 INSERT INTO clstr_tst (b, c) VALUES (12, 'doce');
45 INSERT INTO clstr_tst (b, c) VALUES (17, 'diecisiete');
46 INSERT INTO clstr_tst (b, c) VALUES (9, 'nueve');
47 INSERT INTO clstr_tst (b, c) VALUES (19, 'diecinueve');
48 INSERT INTO clstr_tst (b, c) VALUES (26, 'veintiseis');
49 INSERT INTO clstr_tst (b, c) VALUES (15, 'quince');
50 INSERT INTO clstr_tst (b, c) VALUES (7, 'siete');
51 INSERT INTO clstr_tst (b, c) VALUES (16, 'dieciseis');
52 INSERT INTO clstr_tst (b, c) VALUES (8, 'ocho');
53 -- This entry is needed to test that TOASTED values are copied correctly.
54 INSERT INTO clstr_tst (b, c, d) VALUES (6, 'seis', repeat('xyzzy', 100000));
55 CLUSTER clstr_tst_c ON clstr_tst;
56 SELECT a,b,c,substring(d for 30), length(d) from clstr_tst;
57  a  | b  |       c       |           substring            | length 
58 ----+----+---------------+--------------------------------+--------
59  10 | 14 | catorce       |                                |       
60  18 |  5 | cinco         |                                |       
61   9 |  4 | cuatro        |                                |       
62  26 | 19 | diecinueve    |                                |       
63  12 | 18 | dieciocho     |                                |       
64  30 | 16 | dieciseis     |                                |       
65  24 | 17 | diecisiete    |                                |       
66   2 | 10 | diez          |                                |       
67  23 | 12 | doce          |                                |       
68  11 |  2 | dos           |                                |       
69  25 |  9 | nueve         |                                |       
70  31 |  8 | ocho          |                                |       
71   1 | 11 | once          |                                |       
72  28 | 15 | quince        |                                |       
73  32 |  6 | seis          | xyzzyxyzzyxyzzyxyzzyxyzzyxyzzy | 500000
74  29 |  7 | siete         |                                |       
75  15 | 13 | trece         |                                |       
76  22 | 30 | treinta       |                                |       
77  17 | 32 | treinta y dos |                                |       
78   3 | 31 | treinta y uno |                                |       
79   5 |  3 | tres          |                                |       
80  20 |  1 | uno           |                                |       
81   6 | 20 | veinte        |                                |       
82  14 | 25 | veinticinco   |                                |       
83  21 | 24 | veinticuatro  |                                |       
84   4 | 22 | veintidos     |                                |       
85  19 | 29 | veintinueve   |                                |       
86  16 | 28 | veintiocho    |                                |       
87  27 | 26 | veintiseis    |                                |       
88  13 | 27 | veintisiete   |                                |       
89   7 | 23 | veintitres    |                                |       
90   8 | 21 | veintiuno     |                                |       
91 (32 rows)
93 SELECT a,b,c,substring(d for 30), length(d) from clstr_tst ORDER BY a;
94  a  | b  |       c       |           substring            | length 
95 ----+----+---------------+--------------------------------+--------
96   1 | 11 | once          |                                |       
97   2 | 10 | diez          |                                |       
98   3 | 31 | treinta y uno |                                |       
99   4 | 22 | veintidos     |                                |       
100   5 |  3 | tres          |                                |       
101   6 | 20 | veinte        |                                |       
102   7 | 23 | veintitres    |                                |       
103   8 | 21 | veintiuno     |                                |       
104   9 |  4 | cuatro        |                                |       
105  10 | 14 | catorce       |                                |       
106  11 |  2 | dos           |                                |       
107  12 | 18 | dieciocho     |                                |       
108  13 | 27 | veintisiete   |                                |       
109  14 | 25 | veinticinco   |                                |       
110  15 | 13 | trece         |                                |       
111  16 | 28 | veintiocho    |                                |       
112  17 | 32 | treinta y dos |                                |       
113  18 |  5 | cinco         |                                |       
114  19 | 29 | veintinueve   |                                |       
115  20 |  1 | uno           |                                |       
116  21 | 24 | veinticuatro  |                                |       
117  22 | 30 | treinta       |                                |       
118  23 | 12 | doce          |                                |       
119  24 | 17 | diecisiete    |                                |       
120  25 |  9 | nueve         |                                |       
121  26 | 19 | diecinueve    |                                |       
122  27 | 26 | veintiseis    |                                |       
123  28 | 15 | quince        |                                |       
124  29 |  7 | siete         |                                |       
125  30 | 16 | dieciseis     |                                |       
126  31 |  8 | ocho          |                                |       
127  32 |  6 | seis          | xyzzyxyzzyxyzzyxyzzyxyzzyxyzzy | 500000
128 (32 rows)
130 SELECT a,b,c,substring(d for 30), length(d) from clstr_tst ORDER BY b;
131  a  | b  |       c       |           substring            | length 
132 ----+----+---------------+--------------------------------+--------
133  20 |  1 | uno           |                                |       
134  11 |  2 | dos           |                                |       
135   5 |  3 | tres          |                                |       
136   9 |  4 | cuatro        |                                |       
137  18 |  5 | cinco         |                                |       
138  32 |  6 | seis          | xyzzyxyzzyxyzzyxyzzyxyzzyxyzzy | 500000
139  29 |  7 | siete         |                                |       
140  31 |  8 | ocho          |                                |       
141  25 |  9 | nueve         |                                |       
142   2 | 10 | diez          |                                |       
143   1 | 11 | once          |                                |       
144  23 | 12 | doce          |                                |       
145  15 | 13 | trece         |                                |       
146  10 | 14 | catorce       |                                |       
147  28 | 15 | quince        |                                |       
148  30 | 16 | dieciseis     |                                |       
149  24 | 17 | diecisiete    |                                |       
150  12 | 18 | dieciocho     |                                |       
151  26 | 19 | diecinueve    |                                |       
152   6 | 20 | veinte        |                                |       
153   8 | 21 | veintiuno     |                                |       
154   4 | 22 | veintidos     |                                |       
155   7 | 23 | veintitres    |                                |       
156  21 | 24 | veinticuatro  |                                |       
157  14 | 25 | veinticinco   |                                |       
158  27 | 26 | veintiseis    |                                |       
159  13 | 27 | veintisiete   |                                |       
160  16 | 28 | veintiocho    |                                |       
161  19 | 29 | veintinueve   |                                |       
162  22 | 30 | treinta       |                                |       
163   3 | 31 | treinta y uno |                                |       
164  17 | 32 | treinta y dos |                                |       
165 (32 rows)
167 SELECT a,b,c,substring(d for 30), length(d) from clstr_tst ORDER BY c;
168  a  | b  |       c       |           substring            | length 
169 ----+----+---------------+--------------------------------+--------
170  10 | 14 | catorce       |                                |       
171  18 |  5 | cinco         |                                |       
172   9 |  4 | cuatro        |                                |       
173  26 | 19 | diecinueve    |                                |       
174  12 | 18 | dieciocho     |                                |       
175  30 | 16 | dieciseis     |                                |       
176  24 | 17 | diecisiete    |                                |       
177   2 | 10 | diez          |                                |       
178  23 | 12 | doce          |                                |       
179  11 |  2 | dos           |                                |       
180  25 |  9 | nueve         |                                |       
181  31 |  8 | ocho          |                                |       
182   1 | 11 | once          |                                |       
183  28 | 15 | quince        |                                |       
184  32 |  6 | seis          | xyzzyxyzzyxyzzyxyzzyxyzzyxyzzy | 500000
185  29 |  7 | siete         |                                |       
186  15 | 13 | trece         |                                |       
187  22 | 30 | treinta       |                                |       
188  17 | 32 | treinta y dos |                                |       
189   3 | 31 | treinta y uno |                                |       
190   5 |  3 | tres          |                                |       
191  20 |  1 | uno           |                                |       
192   6 | 20 | veinte        |                                |       
193  14 | 25 | veinticinco   |                                |       
194  21 | 24 | veinticuatro  |                                |       
195   4 | 22 | veintidos     |                                |       
196  19 | 29 | veintinueve   |                                |       
197  16 | 28 | veintiocho    |                                |       
198  27 | 26 | veintiseis    |                                |       
199  13 | 27 | veintisiete   |                                |       
200   7 | 23 | veintitres    |                                |       
201   8 | 21 | veintiuno     |                                |       
202 (32 rows)
204 -- Verify that inheritance link still works
205 INSERT INTO clstr_tst_inh VALUES (0, 100, 'in child table');
206 SELECT a,b,c,substring(d for 30), length(d) from clstr_tst;
207  a  |  b  |       c        |           substring            | length 
208 ----+-----+----------------+--------------------------------+--------
209  10 |  14 | catorce        |                                |       
210  18 |   5 | cinco          |                                |       
211   9 |   4 | cuatro         |                                |       
212  26 |  19 | diecinueve     |                                |       
213  12 |  18 | dieciocho      |                                |       
214  30 |  16 | dieciseis      |                                |       
215  24 |  17 | diecisiete     |                                |       
216   2 |  10 | diez           |                                |       
217  23 |  12 | doce           |                                |       
218  11 |   2 | dos            |                                |       
219  25 |   9 | nueve          |                                |       
220  31 |   8 | ocho           |                                |       
221   1 |  11 | once           |                                |       
222  28 |  15 | quince         |                                |       
223  32 |   6 | seis           | xyzzyxyzzyxyzzyxyzzyxyzzyxyzzy | 500000
224  29 |   7 | siete          |                                |       
225  15 |  13 | trece          |                                |       
226  22 |  30 | treinta        |                                |       
227  17 |  32 | treinta y dos  |                                |       
228   3 |  31 | treinta y uno  |                                |       
229   5 |   3 | tres           |                                |       
230  20 |   1 | uno            |                                |       
231   6 |  20 | veinte         |                                |       
232  14 |  25 | veinticinco    |                                |       
233  21 |  24 | veinticuatro   |                                |       
234   4 |  22 | veintidos      |                                |       
235  19 |  29 | veintinueve    |                                |       
236  16 |  28 | veintiocho     |                                |       
237  27 |  26 | veintiseis     |                                |       
238  13 |  27 | veintisiete    |                                |       
239   7 |  23 | veintitres     |                                |       
240   8 |  21 | veintiuno      |                                |       
241   0 | 100 | in child table |                                |       
242 (33 rows)
244 -- Verify that foreign key link still works
245 INSERT INTO clstr_tst (b, c) VALUES (1111, 'this should fail');
246 ERROR:  insert or update on table "clstr_tst" violates foreign key constraint "clstr_tst_con"
247 DETAIL:  Key (b)=(1111) is not present in table "clstr_tst_s".
248 SELECT conname FROM pg_constraint WHERE conrelid = 'clstr_tst'::regclass
249 ORDER BY 1;
250     conname     
251 ----------------
252  clstr_tst_con
253  clstr_tst_pkey
254 (2 rows)
256 SELECT relname, relkind,
257     EXISTS(SELECT 1 FROM pg_class WHERE oid = c.reltoastrelid) AS hastoast
258 FROM pg_class c WHERE relname LIKE 'clstr_tst%' ORDER BY relname;
259        relname        | relkind | hastoast 
260 ----------------------+---------+----------
261  clstr_tst            | r       | t
262  clstr_tst_a_seq      | S       | f
263  clstr_tst_b          | i       | f
264  clstr_tst_b_c        | i       | f
265  clstr_tst_c          | i       | f
266  clstr_tst_c_b        | i       | f
267  clstr_tst_inh        | r       | t
268  clstr_tst_pkey       | i       | f
269  clstr_tst_s          | r       | f
270  clstr_tst_s_pkey     | i       | f
271  clstr_tst_s_rf_a_seq | S       | f
272 (11 rows)
274 -- Verify that indisclustered is correctly set
275 SELECT pg_class.relname FROM pg_index, pg_class, pg_class AS pg_class_2
276 WHERE pg_class.oid=indexrelid
277         AND indrelid=pg_class_2.oid
278         AND pg_class_2.relname = 'clstr_tst'
279         AND indisclustered;
280    relname   
281 -------------
282  clstr_tst_c
283 (1 row)
285 -- Try changing indisclustered
286 ALTER TABLE clstr_tst CLUSTER ON clstr_tst_b_c;
287 SELECT pg_class.relname FROM pg_index, pg_class, pg_class AS pg_class_2
288 WHERE pg_class.oid=indexrelid
289         AND indrelid=pg_class_2.oid
290         AND pg_class_2.relname = 'clstr_tst'
291         AND indisclustered;
292     relname    
293 ---------------
294  clstr_tst_b_c
295 (1 row)
297 -- Try turning off all clustering
298 ALTER TABLE clstr_tst SET WITHOUT CLUSTER;
299 SELECT pg_class.relname FROM pg_index, pg_class, pg_class AS pg_class_2
300 WHERE pg_class.oid=indexrelid
301         AND indrelid=pg_class_2.oid
302         AND pg_class_2.relname = 'clstr_tst'
303         AND indisclustered;
304  relname 
305 ---------
306 (0 rows)
308 -- Verify that toast tables are clusterable
309 CLUSTER pg_toast.pg_toast_826 USING pg_toast_826_index;
310 -- Verify that clustering all tables does in fact cluster the right ones
311 CREATE USER regress_clstr_user;
312 CREATE TABLE clstr_1 (a INT PRIMARY KEY);
313 CREATE TABLE clstr_2 (a INT PRIMARY KEY);
314 CREATE TABLE clstr_3 (a INT PRIMARY KEY);
315 ALTER TABLE clstr_1 OWNER TO regress_clstr_user;
316 ALTER TABLE clstr_3 OWNER TO regress_clstr_user;
317 GRANT SELECT ON clstr_2 TO regress_clstr_user;
318 INSERT INTO clstr_1 VALUES (2);
319 INSERT INTO clstr_1 VALUES (1);
320 INSERT INTO clstr_2 VALUES (2);
321 INSERT INTO clstr_2 VALUES (1);
322 INSERT INTO clstr_3 VALUES (2);
323 INSERT INTO clstr_3 VALUES (1);
324 -- "CLUSTER <tablename>" on a table that hasn't been clustered
325 CLUSTER clstr_2;
326 ERROR:  there is no previously clustered index for table "clstr_2"
327 CLUSTER clstr_1_pkey ON clstr_1;
328 CLUSTER clstr_2 USING clstr_2_pkey;
329 SELECT * FROM clstr_1 UNION ALL
330   SELECT * FROM clstr_2 UNION ALL
331   SELECT * FROM clstr_3;
332  a 
340 (6 rows)
342 -- revert to the original state
343 DELETE FROM clstr_1;
344 DELETE FROM clstr_2;
345 DELETE FROM clstr_3;
346 INSERT INTO clstr_1 VALUES (2);
347 INSERT INTO clstr_1 VALUES (1);
348 INSERT INTO clstr_2 VALUES (2);
349 INSERT INTO clstr_2 VALUES (1);
350 INSERT INTO clstr_3 VALUES (2);
351 INSERT INTO clstr_3 VALUES (1);
352 -- this user can only cluster clstr_1 and clstr_3, but the latter
353 -- has not been clustered
354 SET SESSION AUTHORIZATION regress_clstr_user;
355 SET client_min_messages = ERROR;  -- order of "skipping" warnings may vary
356 CLUSTER;
357 RESET client_min_messages;
358 SELECT * FROM clstr_1 UNION ALL
359   SELECT * FROM clstr_2 UNION ALL
360   SELECT * FROM clstr_3;
361  a 
369 (6 rows)
371 -- cluster a single table using the indisclustered bit previously set
372 DELETE FROM clstr_1;
373 INSERT INTO clstr_1 VALUES (2);
374 INSERT INTO clstr_1 VALUES (1);
375 CLUSTER clstr_1;
376 SELECT * FROM clstr_1;
377  a 
381 (2 rows)
383 -- Test MVCC-safety of cluster. There isn't much we can do to verify the
384 -- results with a single backend...
385 CREATE TABLE clustertest (key int PRIMARY KEY);
386 INSERT INTO clustertest VALUES (10);
387 INSERT INTO clustertest VALUES (20);
388 INSERT INTO clustertest VALUES (30);
389 INSERT INTO clustertest VALUES (40);
390 INSERT INTO clustertest VALUES (50);
391 -- Use a transaction so that updates are not committed when CLUSTER sees 'em
392 BEGIN;
393 -- Test update where the old row version is found first in the scan
394 UPDATE clustertest SET key = 100 WHERE key = 10;
395 -- Test update where the new row version is found first in the scan
396 UPDATE clustertest SET key = 35 WHERE key = 40;
397 -- Test longer update chain
398 UPDATE clustertest SET key = 60 WHERE key = 50;
399 UPDATE clustertest SET key = 70 WHERE key = 60;
400 UPDATE clustertest SET key = 80 WHERE key = 70;
401 SELECT * FROM clustertest;
402  key 
403 -----
404   20
405   30
406  100
407   35
408   80
409 (5 rows)
411 CLUSTER clustertest_pkey ON clustertest;
412 SELECT * FROM clustertest;
413  key 
414 -----
415   20
416   30
417   35
418   80
419  100
420 (5 rows)
422 COMMIT;
423 SELECT * FROM clustertest;
424  key 
425 -----
426   20
427   30
428   35
429   80
430  100
431 (5 rows)
433 -- check that temp tables can be clustered
434 create temp table clstr_temp (col1 int primary key, col2 text);
435 insert into clstr_temp values (2, 'two'), (1, 'one');
436 cluster clstr_temp using clstr_temp_pkey;
437 select * from clstr_temp;
438  col1 | col2 
439 ------+------
440     1 | one
441     2 | two
442 (2 rows)
444 drop table clstr_temp;
445 RESET SESSION AUTHORIZATION;
446 -- check clustering an empty table
447 DROP TABLE clustertest;
448 CREATE TABLE clustertest (f1 int PRIMARY KEY);
449 CLUSTER clustertest USING clustertest_pkey;
450 CLUSTER clustertest;
451 -- Check that partitioned tables can be clustered
452 CREATE TABLE clstrpart (a int) PARTITION BY RANGE (a);
453 CREATE TABLE clstrpart1 PARTITION OF clstrpart FOR VALUES FROM (1) TO (10) PARTITION BY RANGE (a);
454 CREATE TABLE clstrpart11 PARTITION OF clstrpart1 FOR VALUES FROM (1) TO (5);
455 CREATE TABLE clstrpart12 PARTITION OF clstrpart1 FOR VALUES FROM (5) TO (10) PARTITION BY RANGE (a);
456 CREATE TABLE clstrpart2 PARTITION OF clstrpart FOR VALUES FROM (10) TO (20);
457 CREATE TABLE clstrpart3 PARTITION OF clstrpart DEFAULT PARTITION BY RANGE (a);
458 CREATE TABLE clstrpart33 PARTITION OF clstrpart3 DEFAULT;
459 CREATE INDEX clstrpart_only_idx ON ONLY clstrpart (a);
460 CLUSTER clstrpart USING clstrpart_only_idx; -- fails
461 ERROR:  cannot cluster on invalid index "clstrpart_only_idx"
462 DROP INDEX clstrpart_only_idx;
463 CREATE INDEX clstrpart_idx ON clstrpart (a);
464 -- Check that clustering sets new relfilenodes:
465 CREATE TEMP TABLE old_cluster_info AS SELECT relname, level, relfilenode, relkind FROM pg_partition_tree('clstrpart'::regclass) AS tree JOIN pg_class c ON c.oid=tree.relid ;
466 CLUSTER clstrpart USING clstrpart_idx;
467 CREATE TEMP TABLE new_cluster_info AS SELECT relname, level, relfilenode, relkind FROM pg_partition_tree('clstrpart'::regclass) AS tree JOIN pg_class c ON c.oid=tree.relid ;
468 SELECT relname, old.level, old.relkind, old.relfilenode = new.relfilenode FROM old_cluster_info AS old JOIN new_cluster_info AS new USING (relname) ORDER BY relname COLLATE "C";
469    relname   | level | relkind | ?column? 
470 -------------+-------+---------+----------
471  clstrpart   |     0 | p       | t
472  clstrpart1  |     1 | p       | t
473  clstrpart11 |     2 | r       | f
474  clstrpart12 |     2 | p       | t
475  clstrpart2  |     1 | r       | f
476  clstrpart3  |     1 | p       | t
477  clstrpart33 |     2 | r       | f
478 (7 rows)
480 -- Partitioned indexes aren't and can't be marked un/clustered:
481 \d clstrpart
482        Partitioned table "public.clstrpart"
483  Column |  Type   | Collation | Nullable | Default 
484 --------+---------+-----------+----------+---------
485  a      | integer |           |          | 
486 Partition key: RANGE (a)
487 Indexes:
488     "clstrpart_idx" btree (a)
489 Number of partitions: 3 (Use \d+ to list them.)
491 CLUSTER clstrpart;
492 ERROR:  there is no previously clustered index for table "clstrpart"
493 ALTER TABLE clstrpart SET WITHOUT CLUSTER;
494 ERROR:  cannot mark index clustered in partitioned table
495 ALTER TABLE clstrpart CLUSTER ON clstrpart_idx;
496 ERROR:  cannot mark index clustered in partitioned table
497 DROP TABLE clstrpart;
498 -- Ownership of partitions is checked
499 CREATE TABLE ptnowner(i int unique) PARTITION BY LIST (i);
500 CREATE INDEX ptnowner_i_idx ON ptnowner(i);
501 CREATE TABLE ptnowner1 PARTITION OF ptnowner FOR VALUES IN (1);
502 CREATE ROLE regress_ptnowner;
503 CREATE TABLE ptnowner2 PARTITION OF ptnowner FOR VALUES IN (2);
504 ALTER TABLE ptnowner1 OWNER TO regress_ptnowner;
505 SET SESSION AUTHORIZATION regress_ptnowner;
506 CLUSTER ptnowner USING ptnowner_i_idx;
507 ERROR:  permission denied for table ptnowner
508 RESET SESSION AUTHORIZATION;
509 ALTER TABLE ptnowner OWNER TO regress_ptnowner;
510 CREATE TEMP TABLE ptnowner_oldnodes AS
511   SELECT oid, relname, relfilenode FROM pg_partition_tree('ptnowner') AS tree
512   JOIN pg_class AS c ON c.oid=tree.relid;
513 SET SESSION AUTHORIZATION regress_ptnowner;
514 CLUSTER ptnowner USING ptnowner_i_idx;
515 WARNING:  permission denied to cluster "ptnowner2", skipping it
516 RESET SESSION AUTHORIZATION;
517 SELECT a.relname, a.relfilenode=b.relfilenode FROM pg_class a
518   JOIN ptnowner_oldnodes b USING (oid) ORDER BY a.relname COLLATE "C";
519   relname  | ?column? 
520 -----------+----------
521  ptnowner  | t
522  ptnowner1 | f
523  ptnowner2 | t
524 (3 rows)
526 DROP TABLE ptnowner;
527 DROP ROLE regress_ptnowner;
528 -- Test CLUSTER with external tuplesorting
529 create table clstr_4 as select * from tenk1;
530 create index cluster_sort on clstr_4 (hundred, thousand, tenthous);
531 -- ensure we don't use the index in CLUSTER nor the checking SELECTs
532 set enable_indexscan = off;
533 -- Use external sort:
534 set maintenance_work_mem = '1MB';
535 cluster clstr_4 using cluster_sort;
536 select * from
537 (select hundred, lag(hundred) over () as lhundred,
538         thousand, lag(thousand) over () as lthousand,
539         tenthous, lag(tenthous) over () as ltenthous from clstr_4) ss
540 where row(hundred, thousand, tenthous) <= row(lhundred, lthousand, ltenthous);
541  hundred | lhundred | thousand | lthousand | tenthous | ltenthous 
542 ---------+----------+----------+-----------+----------+-----------
543 (0 rows)
545 reset enable_indexscan;
546 reset maintenance_work_mem;
547 -- test CLUSTER on expression index
548 CREATE TABLE clstr_expression(id serial primary key, a int, b text COLLATE "C");
549 INSERT INTO clstr_expression(a, b) SELECT g.i % 42, 'prefix'||g.i FROM generate_series(1, 133) g(i);
550 CREATE INDEX clstr_expression_minus_a ON clstr_expression ((-a), b);
551 CREATE INDEX clstr_expression_upper_b ON clstr_expression ((upper(b)));
552 -- verify indexes work before cluster
553 BEGIN;
554 SET LOCAL enable_seqscan = false;
555 EXPLAIN (COSTS OFF) SELECT * FROM clstr_expression WHERE upper(b) = 'PREFIX3';
556                           QUERY PLAN                           
557 ---------------------------------------------------------------
558  Index Scan using clstr_expression_upper_b on clstr_expression
559    Index Cond: (upper(b) = 'PREFIX3'::text)
560 (2 rows)
562 SELECT * FROM clstr_expression WHERE upper(b) = 'PREFIX3';
563  id | a |    b    
564 ----+---+---------
565   3 | 3 | prefix3
566 (1 row)
568 EXPLAIN (COSTS OFF) SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
569                           QUERY PLAN                           
570 ---------------------------------------------------------------
571  Index Scan using clstr_expression_minus_a on clstr_expression
572    Index Cond: ((- a) = '-3'::integer)
573 (2 rows)
575 SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
576  id  | a |     b     
577 -----+---+-----------
578  129 | 3 | prefix129
579    3 | 3 | prefix3
580   45 | 3 | prefix45
581   87 | 3 | prefix87
582 (4 rows)
584 COMMIT;
585 -- and after clustering on clstr_expression_minus_a
586 CLUSTER clstr_expression USING clstr_expression_minus_a;
587 WITH rows AS
588   (SELECT ctid, lag(a) OVER (ORDER BY ctid) AS la, a FROM clstr_expression)
589 SELECT * FROM rows WHERE la < a;
590  ctid | la | a 
591 ------+----+---
592 (0 rows)
594 BEGIN;
595 SET LOCAL enable_seqscan = false;
596 EXPLAIN (COSTS OFF) SELECT * FROM clstr_expression WHERE upper(b) = 'PREFIX3';
597                           QUERY PLAN                           
598 ---------------------------------------------------------------
599  Index Scan using clstr_expression_upper_b on clstr_expression
600    Index Cond: (upper(b) = 'PREFIX3'::text)
601 (2 rows)
603 SELECT * FROM clstr_expression WHERE upper(b) = 'PREFIX3';
604  id | a |    b    
605 ----+---+---------
606   3 | 3 | prefix3
607 (1 row)
609 EXPLAIN (COSTS OFF) SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
610                           QUERY PLAN                           
611 ---------------------------------------------------------------
612  Index Scan using clstr_expression_minus_a on clstr_expression
613    Index Cond: ((- a) = '-3'::integer)
614 (2 rows)
616 SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
617  id  | a |     b     
618 -----+---+-----------
619  129 | 3 | prefix129
620    3 | 3 | prefix3
621   45 | 3 | prefix45
622   87 | 3 | prefix87
623 (4 rows)
625 COMMIT;
626 -- and after clustering on clstr_expression_upper_b
627 CLUSTER clstr_expression USING clstr_expression_upper_b;
628 WITH rows AS
629   (SELECT ctid, lag(b) OVER (ORDER BY ctid) AS lb, b FROM clstr_expression)
630 SELECT * FROM rows WHERE upper(lb) > upper(b);
631  ctid | lb | b 
632 ------+----+---
633 (0 rows)
635 BEGIN;
636 SET LOCAL enable_seqscan = false;
637 EXPLAIN (COSTS OFF) SELECT * FROM clstr_expression WHERE upper(b) = 'PREFIX3';
638                           QUERY PLAN                           
639 ---------------------------------------------------------------
640  Index Scan using clstr_expression_upper_b on clstr_expression
641    Index Cond: (upper(b) = 'PREFIX3'::text)
642 (2 rows)
644 SELECT * FROM clstr_expression WHERE upper(b) = 'PREFIX3';
645  id | a |    b    
646 ----+---+---------
647   3 | 3 | prefix3
648 (1 row)
650 EXPLAIN (COSTS OFF) SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
651                           QUERY PLAN                           
652 ---------------------------------------------------------------
653  Index Scan using clstr_expression_minus_a on clstr_expression
654    Index Cond: ((- a) = '-3'::integer)
655 (2 rows)
657 SELECT * FROM clstr_expression WHERE -a = -3 ORDER BY -a, b;
658  id  | a |     b     
659 -----+---+-----------
660  129 | 3 | prefix129
661    3 | 3 | prefix3
662   45 | 3 | prefix45
663   87 | 3 | prefix87
664 (4 rows)
666 COMMIT;
667 -- clean up
668 DROP TABLE clustertest;
669 DROP TABLE clstr_1;
670 DROP TABLE clstr_2;
671 DROP TABLE clstr_3;
672 DROP TABLE clstr_4;
673 DROP TABLE clstr_expression;
674 DROP USER regress_clstr_user;