Skip to content

Commit f17369b

Browse files
knizhnikkelvich
authored andcommitted
Add expected output for alter index test
1 parent 3c76069 commit f17369b

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
create table tmptab (pk integer primary key, sk integer);
2+
-- insert ebnough records to make psotgresql optimizer use indexes
3+
insert into tmptab values (generate_series(1, 10000), generate_series(1, 10000));
4+
vacuum analyze;
5+
-- create normal index
6+
create index idx on tmptab(sk);
7+
-- just normal index search
8+
select * from tmptab where sk = 100;
9+
pk | sk
10+
-----+-----
11+
100 | 100
12+
(1 row)
13+
14+
-- make index partial
15+
alter index idx where pk < 1000;
16+
-- select using exact partial index range
17+
select * from tmptab where sk = 100 and pk < 1000;
18+
pk | sk
19+
-----+-----
20+
100 | 100
21+
(1 row)
22+
23+
explain select * from tmptab where sk = 100 and pk < 1000;
24+
QUERY PLAN
25+
------------------------------------------------------------------
26+
Index Scan using idx on tmptab (cost=0.28..8.29 rows=1 width=8)
27+
Index Cond: (sk = 100)
28+
(2 rows)
29+
30+
-- select using subset of partial index range
31+
select * from tmptab where sk = 100 and pk < 200;
32+
pk | sk
33+
-----+-----
34+
100 | 100
35+
(1 row)
36+
37+
explain select * from tmptab where sk = 100 and pk < 200;
38+
QUERY PLAN
39+
------------------------------------------------------------------
40+
Index Scan using idx on tmptab (cost=0.28..8.29 rows=1 width=8)
41+
Index Cond: (sk = 100)
42+
Filter: (pk < 200)
43+
(3 rows)
44+
45+
-- select outside partial index range
46+
select * from tmptab where sk = 100 and pk > 1000;
47+
pk | sk
48+
----+----
49+
(0 rows)
50+
51+
explain select * from tmptab where sk = 100 and pk > 1000;
52+
QUERY PLAN
53+
--------------------------------------------------------
54+
Seq Scan on tmptab (cost=0.00..195.00 rows=1 width=8)
55+
Filter: ((pk > 1000) AND (sk = 100))
56+
(2 rows)
57+
58+
-- select without partial index range
59+
select * from tmptab where sk = 100;
60+
pk | sk
61+
-----+-----
62+
100 | 100
63+
(1 row)
64+
65+
explain select * from tmptab where sk = 100;
66+
QUERY PLAN
67+
--------------------------------------------------------
68+
Seq Scan on tmptab (cost=0.00..170.00 rows=1 width=8)
69+
Filter: (sk = 100)
70+
(2 rows)
71+
72+
-- extend partial index range
73+
alter index idx where pk < 10000;
74+
-- select using exact partial index range
75+
select * from tmptab where sk = 1000 and pk < 10000;
76+
pk | sk
77+
------+------
78+
1000 | 1000
79+
(1 row)
80+
81+
explain select * from tmptab where sk = 1000 and pk < 10000;
82+
QUERY PLAN
83+
------------------------------------------------------------------
84+
Index Scan using idx on tmptab (cost=0.28..8.30 rows=1 width=8)
85+
Index Cond: (sk = 1000)
86+
(2 rows)
87+
88+
-- calculating aggregate within exact partial index range
89+
select count(*) from tmptab where sk < 1000 and pk < 10000;
90+
count
91+
-------
92+
999
93+
(1 row)
94+
95+
explain select count(*) from tmptab where sk < 1000 and pk < 10000;
96+
QUERY PLAN
97+
---------------------------------------------------------------------------------
98+
Aggregate (cost=40.28..40.29 rows=1 width=0)
99+
-> Index Only Scan using idx on tmptab (cost=0.28..37.78 rows=1000 width=0)
100+
Index Cond: (sk < 1000)
101+
(3 rows)
102+
103+
-- reducing partial idex predicate
104+
alter index idx where pk < 9000;
105+
-- select using new exact partial index range and key value belonging to old range
106+
select * from tmptab where sk = 9000 and pk < 9000;
107+
pk | sk
108+
----+----
109+
(0 rows)
110+
111+
explain select * from tmptab where sk = 9000 and pk < 9000;
112+
QUERY PLAN
113+
------------------------------------------------------------------
114+
Index Scan using idx on tmptab (cost=0.29..8.30 rows=1 width=8)
115+
Index Cond: (sk = 9000)
116+
(2 rows)
117+
118+
-- select using exact partial index range
119+
select * from tmptab where sk = 900 and pk < 9000;
120+
pk | sk
121+
-----+-----
122+
900 | 900
123+
(1 row)
124+
125+
explain select * from tmptab where sk = 900 and pk < 9000;
126+
QUERY PLAN
127+
------------------------------------------------------------------
128+
Index Scan using idx on tmptab (cost=0.29..8.30 rows=1 width=8)
129+
Index Cond: (sk = 900)
130+
(2 rows)
131+
132+
drop table tmptab;

0 commit comments

Comments
 (0)