2 * 1.1. test CREATE INDEX with buffered build
4 -- Regular index with included columns
5 CREATE TABLE tbl_gist (c1 int, c2 int, c3 int, c4 box);
6 -- size is chosen to exceed page size and trigger actual truncation
7 INSERT INTO tbl_gist SELECT x, 2*x, 3*x, box(point(x,x+1),point(2*x,2*x+1)) FROM generate_series(1,8000) AS x;
8 CREATE INDEX tbl_gist_idx ON tbl_gist using gist (c4) INCLUDE (c1,c2,c3);
9 SELECT pg_get_indexdef(i.indexrelid)
10 FROM pg_index i JOIN pg_class c ON i.indexrelid = c.oid
11 WHERE i.indrelid = 'tbl_gist'::regclass ORDER BY c.relname;
13 -----------------------------------------------------------------------------------
14 CREATE INDEX tbl_gist_idx ON public.tbl_gist USING gist (c4) INCLUDE (c1, c2, c3)
17 SELECT * FROM tbl_gist where c4 <@ box(point(1,1),point(10,10));
19 ----+----+----+-------------
20 1 | 2 | 3 | (2,3),(1,2)
21 2 | 4 | 6 | (4,5),(2,3)
22 3 | 6 | 9 | (6,7),(3,4)
23 4 | 8 | 12 | (8,9),(4,5)
26 SET enable_bitmapscan TO off;
27 EXPLAIN (costs off) SELECT * FROM tbl_gist where c4 <@ box(point(1,1),point(10,10));
29 ------------------------------------------------
30 Index Only Scan using tbl_gist_idx on tbl_gist
31 Index Cond: (c4 <@ '(10,10),(1,1)'::box)
34 SET enable_bitmapscan TO default;
37 * 1.2. test CREATE INDEX with inserts
39 -- Regular index with included columns
40 CREATE TABLE tbl_gist (c1 int, c2 int, c3 int, c4 box);
41 -- size is chosen to exceed page size and trigger actual truncation
42 CREATE INDEX tbl_gist_idx ON tbl_gist using gist (c4) INCLUDE (c1,c2,c3);
43 INSERT INTO tbl_gist SELECT x, 2*x, 3*x, box(point(x,x+1),point(2*x,2*x+1)) FROM generate_series(1,8000) AS x;
44 SELECT pg_get_indexdef(i.indexrelid)
45 FROM pg_index i JOIN pg_class c ON i.indexrelid = c.oid
46 WHERE i.indrelid = 'tbl_gist'::regclass ORDER BY c.relname;
48 -----------------------------------------------------------------------------------
49 CREATE INDEX tbl_gist_idx ON public.tbl_gist USING gist (c4) INCLUDE (c1, c2, c3)
52 SELECT * FROM tbl_gist where c4 <@ box(point(1,1),point(10,10));
54 ----+----+----+-------------
55 1 | 2 | 3 | (2,3),(1,2)
56 2 | 4 | 6 | (4,5),(2,3)
57 3 | 6 | 9 | (6,7),(3,4)
58 4 | 8 | 12 | (8,9),(4,5)
61 SET enable_bitmapscan TO off;
62 EXPLAIN (costs off) SELECT * FROM tbl_gist where c4 <@ box(point(1,1),point(10,10));
64 ------------------------------------------------
65 Index Only Scan using tbl_gist_idx on tbl_gist
66 Index Cond: (c4 <@ '(10,10),(1,1)'::box)
69 SET enable_bitmapscan TO default;
72 * 2. CREATE INDEX CONCURRENTLY
74 CREATE TABLE tbl_gist (c1 int, c2 int, c3 int, c4 box);
75 INSERT INTO tbl_gist SELECT x, 2*x, 3*x, box(point(x,x+1),point(2*x,2*x+1)) FROM generate_series(1,10) AS x;
76 CREATE INDEX CONCURRENTLY tbl_gist_idx ON tbl_gist using gist (c4) INCLUDE (c1,c2,c3);
77 SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl_gist' ORDER BY indexname;
79 -----------------------------------------------------------------------------------
80 CREATE INDEX tbl_gist_idx ON public.tbl_gist USING gist (c4) INCLUDE (c1, c2, c3)
87 CREATE TABLE tbl_gist (c1 int, c2 int, c3 int, c4 box);
88 INSERT INTO tbl_gist SELECT x, 2*x, 3*x, box(point(x,x+1),point(2*x,2*x+1)) FROM generate_series(1,10) AS x;
89 CREATE INDEX tbl_gist_idx ON tbl_gist using gist (c4) INCLUDE (c1,c3);
90 SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl_gist' ORDER BY indexname;
92 -------------------------------------------------------------------------------
93 CREATE INDEX tbl_gist_idx ON public.tbl_gist USING gist (c4) INCLUDE (c1, c3)
96 REINDEX INDEX tbl_gist_idx;
97 SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl_gist' ORDER BY indexname;
99 -------------------------------------------------------------------------------
100 CREATE INDEX tbl_gist_idx ON public.tbl_gist USING gist (c4) INCLUDE (c1, c3)
103 ALTER TABLE tbl_gist DROP COLUMN c1;
104 SELECT indexdef FROM pg_indexes WHERE tablename = 'tbl_gist' ORDER BY indexname;
111 * 4. Update, delete values in indexed table.
113 CREATE TABLE tbl_gist (c1 int, c2 int, c3 int, c4 box);
114 INSERT INTO tbl_gist SELECT x, 2*x, 3*x, box(point(x,x+1),point(2*x,2*x+1)) FROM generate_series(1,10) AS x;
115 CREATE INDEX tbl_gist_idx ON tbl_gist using gist (c4) INCLUDE (c1,c3);
116 UPDATE tbl_gist SET c1 = 100 WHERE c1 = 2;
117 UPDATE tbl_gist SET c1 = 1 WHERE c1 = 3;
118 DELETE FROM tbl_gist WHERE c1 = 5 OR c3 = 12;
121 * 5. Alter column type.
123 CREATE TABLE tbl_gist (c1 int, c2 int, c3 int, c4 box);
124 INSERT INTO tbl_gist SELECT x, 2*x, 3*x, box(point(x,x+1),point(2*x,2*x+1)) FROM generate_series(1,10) AS x;
125 CREATE INDEX tbl_gist_idx ON tbl_gist using gist (c4) INCLUDE (c1,c3);
126 ALTER TABLE tbl_gist ALTER c1 TYPE bigint;
127 ALTER TABLE tbl_gist ALTER c3 TYPE bigint;
129 Table "public.tbl_gist"
130 Column | Type | Collation | Nullable | Default
131 --------+---------+-----------+----------+---------
137 "tbl_gist_idx" gist (c4) INCLUDE (c1, c3)
141 * 6. EXCLUDE constraint.
143 CREATE TABLE tbl_gist (c1 int, c2 int, c3 int, c4 box, EXCLUDE USING gist (c4 WITH &&) INCLUDE (c1, c2, c3));
144 INSERT INTO tbl_gist SELECT x, 2*x, 3*x, box(point(x,x+1),point(2*x,2*x+1)) FROM generate_series(1,10) AS x;
145 ERROR: conflicting key value violates exclusion constraint "tbl_gist_c4_c1_c2_c3_excl"
146 DETAIL: Key (c4)=((4,5),(2,3)) conflicts with existing key (c4)=((2,3),(1,2)).
147 INSERT INTO tbl_gist SELECT x, 2*x, 3*x, box(point(3*x,2*x),point(3*x+1,2*x+1)) FROM generate_series(1,10) AS x;
148 EXPLAIN (costs off) SELECT * FROM tbl_gist where c4 <@ box(point(1,1),point(10,10));
150 -------------------------------------------------------------
151 Index Only Scan using tbl_gist_c4_c1_c2_c3_excl on tbl_gist
152 Index Cond: (c4 <@ '(10,10),(1,1)'::box)
156 Table "public.tbl_gist"
157 Column | Type | Collation | Nullable | Default
158 --------+---------+-----------+----------+---------
164 "tbl_gist_c4_c1_c2_c3_excl" EXCLUDE USING gist (c4 WITH &&) INCLUDE (c1, c2, c3)