File: 05_useindex.out

package info (click to toggle)
pgtt 4.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 536 kB
  • sloc: ansic: 1,547; sql: 569; makefile: 27; sh: 1
file content (124 lines) | stat: -rw-r--r-- 6,727 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
----
-- Regression test to Global Temporary Table implementation
--
-- Test for GTT with index, the temporary table must have the
-- index too.
--
----
-- Create a GTT like table
CREATE /*GLOBAL*/ TEMPORARY TABLE t_glob_temptable1 (id integer, lbl text) ON COMMIT PRESERVE ROWS;
SET pgtt.enabled TO off;
CREATE INDEX ON pgtt_schema.t_glob_temptable1 (id);
CREATE INDEX ON pgtt_schema.t_glob_temptable1 (lbl);
-- Look at Global Temporary Table definition
SELECT nspname, relname, preserved, code FROM pgtt_schema.pg_global_temp_tables;
   nspname   |      relname      | preserved |         code         
-------------+-------------------+-----------+----------------------
 pgtt_schema | t_glob_temptable1 | t         | id integer, lbl text
(1 row)

-- A "template" unlogged table should exists
SELECT a.attname,
  pg_catalog.format_type(a.atttypid, a.atttypmod),
  (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid, true) for 128)
   FROM pg_catalog.pg_attrdef d
   WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef),
  a.attnotnull,
  pg_catalog.col_description(a.attrelid, a.attnum)
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = (
        SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = 't_glob_temptable1' AND n.nspname = 'pgtt_schema'
        ) AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum;
 attname | format_type | substring | attnotnull | col_description 
---------+-------------+-----------+------------+-----------------
 id      | integer     |           | f          | 
 lbl     | text        |           | f          | 
(2 rows)

-- With indexes still defined
SELECT c2.relname, i.indisprimary, i.indisunique, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true),
  pg_catalog.pg_get_constraintdef(con.oid, true), contype
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
  LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))
WHERE c.oid = (
        SELECT c.oid FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relname = 't_glob_temptable1' AND n.nspname = 'pgtt_schema'
        ) AND c.oid = i.indrelid AND i.indexrelid = c2.oid
ORDER BY i.indisprimary DESC, c2.relname;
          relname          | indisprimary | indisunique |                                pg_get_indexdef                                | pg_get_constraintdef | contype 
---------------------------+--------------+-------------+-------------------------------------------------------------------------------+----------------------+---------
 t_glob_temptable1_id_idx  | f            | f           | CREATE INDEX t_glob_temptable1_id_idx ON t_glob_temptable1 USING btree (id)   |                      | 
 t_glob_temptable1_lbl_idx | f            | f           | CREATE INDEX t_glob_temptable1_lbl_idx ON t_glob_temptable1 USING btree (lbl) |                      | 
(2 rows)

SET pgtt.enabled TO on;
-- With the first insert some value in the temporary table
INSERT INTO t_glob_temptable1 VALUES (1, 'One');
-- Look if we have two tables now
SELECT regexp_replace(n.nspname, '\d+', 'x', 'g'), c.relname FROM pg_class c JOIN pg_namespace n ON (c.relnamespace=n.oid) WHERE relname = 't_glob_temptable1';
 regexp_replace |      relname      
----------------+-------------------
 pgtt_schema    | t_glob_temptable1
 pg_temp_x      | t_glob_temptable1
(2 rows)

-- and that the temporary table has the indexes too
SELECT tablename, indexname FROM pg_indexes WHERE tablename = 't_glob_temptable1' and schemaname LIKE 'pg_temp_%' ORDER BY tablename, indexname;
     tablename     |         indexname         
-------------------+---------------------------
 t_glob_temptable1 | t_glob_temptable1_id_idx
 t_glob_temptable1 | t_glob_temptable1_lbl_idx
(2 rows)

INSERT INTO t_glob_temptable1 VALUES (2, 'two');
-- Verify that the index is used
SET enable_bitmapscan TO off;
EXPLAIN (COSTS OFF) SELECT * FROM t_glob_temptable1 WHERE id = 2;
                           QUERY PLAN                           
----------------------------------------------------------------
 Index Scan using t_glob_temptable1_id_idx on t_glob_temptable1
   Index Cond: (id = 2)
(2 rows)

-- Reconnect and drop it
\c - -
-- Cleanup
DROP TABLE t_glob_temptable1;
-- Create a GTT like table
CREATE /*GLOBAL*/ TEMPORARY TABLE t_glob_temptable2 (id integer, lbl text) ON COMMIT PRESERVE ROWS;
CREATE INDEX ON t_glob_temptable2 (id);
SELECT * FROM t_glob_temptable2;
 id | lbl 
----+-----
(0 rows)

-- Must complain that the GTT is in use
CREATE INDEX ON t_glob_temptable2 (lbl);
ERROR:  a temporary table has been created and is active, can not add an index on the GTT table in this session.
SELECT pg_catalog.pg_get_indexdef(i.indexrelid, 0, true)
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))
WHERE c.oid = 'pgtt_schema.t_glob_temptable2'::regclass::oid AND c.oid = i.indrelid AND i.indexrelid = c2.oid
ORDER BY i.indisprimary DESC, c2.relname;
                                     pg_get_indexdef                                     
-----------------------------------------------------------------------------------------
 CREATE INDEX t_glob_temptable2_id_idx ON pgtt_schema.t_glob_temptable2 USING btree (id)
(1 row)

-- Check that we do not break LIKE ... USING INDEXES
CREATE TABLE tb_with_index (id integer PRIMARY KEY, lbl varchar);
CREATE INDEX ON tb_with_index(lbl);
CREATE TEMPORARY TABLE temptb_with_index (LIKE tb_with_index INCLUDING INDEXES);
SELECT c2.relname, pg_catalog.pg_get_indexdef(i.indexrelid, 0, true), pg_catalog.pg_get_constraintdef(con.oid, true)
FROM pg_catalog.pg_class c, pg_catalog.pg_class c2, pg_catalog.pg_index i
  LEFT JOIN pg_catalog.pg_constraint con ON (conrelid = i.indrelid AND conindid = i.indexrelid AND contype IN ('p','u','x'))
WHERE c.oid = 'temptb_with_index'::regclass AND c.oid = i.indrelid AND i.indexrelid = c2.oid
ORDER BY i.indisprimary DESC, c2.relname;
          relname          |                                 pg_get_indexdef                                  | pg_get_constraintdef 
---------------------------+----------------------------------------------------------------------------------+----------------------
 temptb_with_index_pkey    | CREATE UNIQUE INDEX temptb_with_index_pkey ON temptb_with_index USING btree (id) | PRIMARY KEY (id)
 temptb_with_index_lbl_idx | CREATE INDEX temptb_with_index_lbl_idx ON temptb_with_index USING btree (lbl)    | 
(2 rows)

\c - -
DROP TABLE t_glob_temptable2;