@@ -99,7 +99,57 @@ INSERT INTO t5 values (DEFAULT);
9999SELECT x AS sf_val FROM t4 WHERE x > 42 \gset
100100SELECT :sf_val < x AS is_growing FROM t5 WHERE x > 42;
101101
102+ -- Test INCREMENT
103+ CREATE TABLE t6(x bigserial, y int);
104+ INSERT INTO t6 VALUES (DEFAULT, 10);
105+ INSERT INTO t6 VALUES (DEFAULT, 20);
106+ SELECT * FROM t6;
107+ INSERT INTO t6 VALUES (DEFAULT, 30);
108+ SELECT * FROM t6;
109+ SELECT snowflake.convert_sequence_to_snowflake('t6_x_seq');
110+ -- Get the count portion of the id.
111+ -- It happens within the same milisecond, should increment
112+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'))
113+ UNION ALL
114+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'))
115+ UNION ALL
116+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'));
117+
118+ -- Should return 1 (the counter difference)
119+ SELECT ABS(snowflake.nextval('t6_x_seq') - snowflake.nextval('t6_x_seq'));
120+
121+ -- If < 4096, will increment that amount
122+ ALTER SEQUENCE t6_x_seq INCREMENT 100 NO MAXVALUE;
123+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'))
124+ UNION ALL
125+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'))
126+ UNION ALL
127+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'));
128+
129+ -- This will force the time ms portion to increment,
130+ -- the count portion should be 0
131+ ALTER SEQUENCE t6_x_seq INCREMENT 4096;
132+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'))
133+ UNION ALL
134+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'))
135+ UNION ALL
136+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'));
137+
138+ -- should return 1 (the ms difference)
139+ SELECT ABS(snowflake.nextval('t6_x_seq') - snowflake.nextval('t6_x_seq')) >> 22;
140+
141+ -- Test unreasonable value- still should get 0s
142+ ALTER SEQUENCE t6_x_seq INCREMENT 9999;
143+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'))
144+ UNION ALL
145+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'))
146+ UNION ALL
147+ SELECT snowflake.get_count(snowflake.nextval('t6_x_seq'));
148+
149+ -- should return 1 (the ms difference)
150+ SELECT ABS(snowflake.nextval('t6_x_seq') - snowflake.nextval('t6_x_seq')) >> 22;
151+
102152-- Cleanup
103- DROP TABLE t1,t2,t3,t4,t5 CASCADE;
153+ DROP TABLE t1,t2,t3,t4,t5,t6 CASCADE;
104154DROP SEQUENCE favorite_seq;
105155DROP EXTENSION snowflake;
0 commit comments