1---
2--- test creation of SERIAL column
3---
4CREATE TABLE serialTest (f1 text, f2 serial);
5INSERT INTO serialTest VALUES ('foo');
6INSERT INTO serialTest VALUES ('bar');
7INSERT INTO serialTest VALUES ('force', 100);
8INSERT INTO serialTest VALUES ('wrong', NULL);
9ERROR:  null value in column "f2" violates not-null constraint
10DETAIL:  Failing row contains (wrong, null).
11SELECT * FROM serialTest;
12  f1   | f2
13-------+-----
14 foo   |   1
15 bar   |   2
16 force | 100
17(3 rows)
18
19-- test smallserial / bigserial
20CREATE TABLE serialTest2 (f1 text, f2 serial, f3 smallserial, f4 serial2,
21  f5 bigserial, f6 serial8);
22INSERT INTO serialTest2 (f1)
23  VALUES ('test_defaults');
24INSERT INTO serialTest2 (f1, f2, f3, f4, f5, f6)
25  VALUES ('test_max_vals', 2147483647, 32767, 32767, 9223372036854775807,
26          9223372036854775807),
27         ('test_min_vals', -2147483648, -32768, -32768, -9223372036854775808,
28          -9223372036854775808);
29-- All these INSERTs should fail:
30INSERT INTO serialTest2 (f1, f3)
31  VALUES ('bogus', -32769);
32ERROR:  smallint out of range
33INSERT INTO serialTest2 (f1, f4)
34  VALUES ('bogus', -32769);
35ERROR:  smallint out of range
36INSERT INTO serialTest2 (f1, f3)
37  VALUES ('bogus', 32768);
38ERROR:  smallint out of range
39INSERT INTO serialTest2 (f1, f4)
40  VALUES ('bogus', 32768);
41ERROR:  smallint out of range
42INSERT INTO serialTest2 (f1, f5)
43  VALUES ('bogus', -9223372036854775809);
44ERROR:  bigint out of range
45INSERT INTO serialTest2 (f1, f6)
46  VALUES ('bogus', -9223372036854775809);
47ERROR:  bigint out of range
48INSERT INTO serialTest2 (f1, f5)
49  VALUES ('bogus', 9223372036854775808);
50ERROR:  bigint out of range
51INSERT INTO serialTest2 (f1, f6)
52  VALUES ('bogus', 9223372036854775808);
53ERROR:  bigint out of range
54SELECT * FROM serialTest2 ORDER BY f2 ASC;
55      f1       |     f2      |   f3   |   f4   |          f5          |          f6
56---------------+-------------+--------+--------+----------------------+----------------------
57 test_min_vals | -2147483648 | -32768 | -32768 | -9223372036854775808 | -9223372036854775808
58 test_defaults |           1 |      1 |      1 |                    1 |                    1
59 test_max_vals |  2147483647 |  32767 |  32767 |  9223372036854775807 |  9223372036854775807
60(3 rows)
61
62SELECT nextval('serialTest2_f2_seq');
63 nextval
64---------
65       2
66(1 row)
67
68SELECT nextval('serialTest2_f3_seq');
69 nextval
70---------
71       2
72(1 row)
73
74SELECT nextval('serialTest2_f4_seq');
75 nextval
76---------
77       2
78(1 row)
79
80SELECT nextval('serialTest2_f5_seq');
81 nextval
82---------
83       2
84(1 row)
85
86SELECT nextval('serialTest2_f6_seq');
87 nextval
88---------
89       2
90(1 row)
91
92-- basic sequence operations using both text and oid references
93CREATE SEQUENCE sequence_test;
94CREATE SEQUENCE IF NOT EXISTS sequence_test;
95NOTICE:  relation "sequence_test" already exists, skipping
96SELECT nextval('sequence_test'::text);
97 nextval
98---------
99       1
100(1 row)
101
102SELECT nextval('sequence_test'::regclass);
103 nextval
104---------
105       2
106(1 row)
107
108SELECT currval('sequence_test'::text);
109 currval
110---------
111       2
112(1 row)
113
114SELECT currval('sequence_test'::regclass);
115 currval
116---------
117       2
118(1 row)
119
120SELECT setval('sequence_test'::text, 32);
121 setval
122--------
123     32
124(1 row)
125
126SELECT nextval('sequence_test'::regclass);
127 nextval
128---------
129      33
130(1 row)
131
132SELECT setval('sequence_test'::text, 99, false);
133 setval
134--------
135     99
136(1 row)
137
138SELECT nextval('sequence_test'::regclass);
139 nextval
140---------
141      99
142(1 row)
143
144SELECT setval('sequence_test'::regclass, 32);
145 setval
146--------
147     32
148(1 row)
149
150SELECT nextval('sequence_test'::text);
151 nextval
152---------
153      33
154(1 row)
155
156SELECT setval('sequence_test'::regclass, 99, false);
157 setval
158--------
159     99
160(1 row)
161
162SELECT nextval('sequence_test'::text);
163 nextval
164---------
165      99
166(1 row)
167
168DISCARD SEQUENCES;
169SELECT currval('sequence_test'::regclass);
170ERROR:  currval of sequence "sequence_test" is not yet defined in this session
171DROP SEQUENCE sequence_test;
172-- renaming sequences
173CREATE SEQUENCE foo_seq;
174ALTER TABLE foo_seq RENAME TO foo_seq_new;
175SELECT * FROM foo_seq_new;
176 sequence_name | last_value | start_value | increment_by |      max_value      | min_value | cache_value | log_cnt | is_cycled | is_called
177---------------+------------+-------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
178 foo_seq       |          1 |           1 |            1 | 9223372036854775807 |         1 |           1 |       0 | f         | f
179(1 row)
180
181SELECT nextval('foo_seq_new');
182 nextval
183---------
184       1
185(1 row)
186
187SELECT nextval('foo_seq_new');
188 nextval
189---------
190       2
191(1 row)
192
193SELECT * FROM foo_seq_new;
194 sequence_name | last_value | start_value | increment_by |      max_value      | min_value | cache_value | log_cnt | is_cycled | is_called
195---------------+------------+-------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
196 foo_seq       |          2 |           1 |            1 | 9223372036854775807 |         1 |           1 |      32 | f         | t
197(1 row)
198
199DROP SEQUENCE foo_seq_new;
200-- renaming serial sequences
201ALTER TABLE serialtest_f2_seq RENAME TO serialtest_f2_foo;
202INSERT INTO serialTest VALUES ('more');
203SELECT * FROM serialTest;
204  f1   | f2
205-------+-----
206 foo   |   1
207 bar   |   2
208 force | 100
209 more  |   3
210(4 rows)
211
212--
213-- Check dependencies of serial and ordinary sequences
214--
215CREATE TEMP SEQUENCE myseq2;
216CREATE TEMP SEQUENCE myseq3;
217CREATE TEMP TABLE t1 (
218  f1 serial,
219  f2 int DEFAULT nextval('myseq2'),
220  f3 int DEFAULT nextval('myseq3'::text)
221);
222-- Both drops should fail, but with different error messages:
223DROP SEQUENCE t1_f1_seq;
224ERROR:  cannot drop sequence t1_f1_seq because other objects depend on it
225DETAIL:  default for table t1 column f1 depends on sequence t1_f1_seq
226HINT:  Use DROP ... CASCADE to drop the dependent objects too.
227DROP SEQUENCE myseq2;
228ERROR:  cannot drop sequence myseq2 because other objects depend on it
229DETAIL:  default for table t1 column f2 depends on sequence myseq2
230HINT:  Use DROP ... CASCADE to drop the dependent objects too.
231-- This however will work:
232DROP SEQUENCE myseq3;
233DROP TABLE t1;
234-- Fails because no longer existent:
235DROP SEQUENCE t1_f1_seq;
236ERROR:  sequence "t1_f1_seq" does not exist
237-- Now OK:
238DROP SEQUENCE myseq2;
239--
240-- Alter sequence
241--
242ALTER SEQUENCE IF EXISTS sequence_test2 RESTART WITH 24
243	 INCREMENT BY 4 MAXVALUE 36 MINVALUE 5 CYCLE;
244NOTICE:  relation "sequence_test2" does not exist, skipping
245CREATE SEQUENCE sequence_test2 START WITH 32;
246SELECT nextval('sequence_test2');
247 nextval
248---------
249      32
250(1 row)
251
252ALTER SEQUENCE sequence_test2 RESTART WITH 24
253	 INCREMENT BY 4 MAXVALUE 36 MINVALUE 5 CYCLE;
254SELECT nextval('sequence_test2');
255 nextval
256---------
257      24
258(1 row)
259
260SELECT nextval('sequence_test2');
261 nextval
262---------
263      28
264(1 row)
265
266SELECT nextval('sequence_test2');
267 nextval
268---------
269      32
270(1 row)
271
272SELECT nextval('sequence_test2');
273 nextval
274---------
275      36
276(1 row)
277
278SELECT nextval('sequence_test2');
279 nextval
280---------
281       5
282(1 row)
283
284ALTER SEQUENCE sequence_test2 RESTART;
285SELECT nextval('sequence_test2');
286 nextval
287---------
288      32
289(1 row)
290
291SELECT nextval('sequence_test2');
292 nextval
293---------
294      36
295(1 row)
296
297SELECT nextval('sequence_test2');
298 nextval
299---------
300       5
301(1 row)
302
303-- Information schema
304SELECT * FROM information_schema.sequences WHERE sequence_name IN
305  ('sequence_test2', 'serialtest2_f2_seq', 'serialtest2_f3_seq',
306   'serialtest2_f4_seq', 'serialtest2_f5_seq', 'serialtest2_f6_seq')
307  ORDER BY sequence_name ASC;
308 sequence_catalog | sequence_schema |   sequence_name    | data_type | numeric_precision | numeric_precision_radix | numeric_scale | start_value | minimum_value |    maximum_value    | increment | cycle_option
309------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+---------------+---------------------+-----------+--------------
310 regression       | public          | sequence_test2     | bigint    |                64 |                       2 |             0 | 32          | 5             | 36                  | 4         | YES
311 regression       | public          | serialtest2_f2_seq | bigint    |                64 |                       2 |             0 | 1           | 1             | 9223372036854775807 | 1         | NO
312 regression       | public          | serialtest2_f3_seq | bigint    |                64 |                       2 |             0 | 1           | 1             | 9223372036854775807 | 1         | NO
313 regression       | public          | serialtest2_f4_seq | bigint    |                64 |                       2 |             0 | 1           | 1             | 9223372036854775807 | 1         | NO
314 regression       | public          | serialtest2_f5_seq | bigint    |                64 |                       2 |             0 | 1           | 1             | 9223372036854775807 | 1         | NO
315 regression       | public          | serialtest2_f6_seq | bigint    |                64 |                       2 |             0 | 1           | 1             | 9223372036854775807 | 1         | NO
316(6 rows)
317
318-- Test comments
319COMMENT ON SEQUENCE asdf IS 'won''t work';
320ERROR:  relation "asdf" does not exist
321COMMENT ON SEQUENCE sequence_test2 IS 'will work';
322COMMENT ON SEQUENCE sequence_test2 IS NULL;
323-- Test lastval()
324CREATE SEQUENCE seq;
325SELECT nextval('seq');
326 nextval
327---------
328       1
329(1 row)
330
331SELECT lastval();
332 lastval
333---------
334       1
335(1 row)
336
337SELECT setval('seq', 99);
338 setval
339--------
340     99
341(1 row)
342
343SELECT lastval();
344 lastval
345---------
346      99
347(1 row)
348
349DISCARD SEQUENCES;
350SELECT lastval();
351ERROR:  lastval is not yet defined in this session
352CREATE SEQUENCE seq2;
353SELECT nextval('seq2');
354 nextval
355---------
356       1
357(1 row)
358
359SELECT lastval();
360 lastval
361---------
362       1
363(1 row)
364
365DROP SEQUENCE seq2;
366-- should fail
367SELECT lastval();
368ERROR:  lastval is not yet defined in this session
369CREATE USER regress_seq_user;
370-- privileges tests
371-- nextval
372BEGIN;
373SET LOCAL SESSION AUTHORIZATION regress_seq_user;
374CREATE SEQUENCE seq3;
375REVOKE ALL ON seq3 FROM regress_seq_user;
376GRANT SELECT ON seq3 TO regress_seq_user;
377SELECT nextval('seq3');
378ERROR:  permission denied for sequence seq3
379ROLLBACK;
380BEGIN;
381SET LOCAL SESSION AUTHORIZATION regress_seq_user;
382CREATE SEQUENCE seq3;
383REVOKE ALL ON seq3 FROM regress_seq_user;
384GRANT UPDATE ON seq3 TO regress_seq_user;
385SELECT nextval('seq3');
386 nextval
387---------
388       1
389(1 row)
390
391ROLLBACK;
392BEGIN;
393SET LOCAL SESSION AUTHORIZATION regress_seq_user;
394CREATE SEQUENCE seq3;
395REVOKE ALL ON seq3 FROM regress_seq_user;
396GRANT USAGE ON seq3 TO regress_seq_user;
397SELECT nextval('seq3');
398 nextval
399---------
400       1
401(1 row)
402
403ROLLBACK;
404-- currval
405BEGIN;
406SET LOCAL SESSION AUTHORIZATION regress_seq_user;
407CREATE SEQUENCE seq3;
408SELECT nextval('seq3');
409 nextval
410---------
411       1
412(1 row)
413
414REVOKE ALL ON seq3 FROM regress_seq_user;
415GRANT SELECT ON seq3 TO regress_seq_user;
416SELECT currval('seq3');
417 currval
418---------
419       1
420(1 row)
421
422ROLLBACK;
423BEGIN;
424SET LOCAL SESSION AUTHORIZATION regress_seq_user;
425CREATE SEQUENCE seq3;
426SELECT nextval('seq3');
427 nextval
428---------
429       1
430(1 row)
431
432REVOKE ALL ON seq3 FROM regress_seq_user;
433GRANT UPDATE ON seq3 TO regress_seq_user;
434SELECT currval('seq3');
435ERROR:  permission denied for sequence seq3
436ROLLBACK;
437BEGIN;
438SET LOCAL SESSION AUTHORIZATION regress_seq_user;
439CREATE SEQUENCE seq3;
440SELECT nextval('seq3');
441 nextval
442---------
443       1
444(1 row)
445
446REVOKE ALL ON seq3 FROM regress_seq_user;
447GRANT USAGE ON seq3 TO regress_seq_user;
448SELECT currval('seq3');
449 currval
450---------
451       1
452(1 row)
453
454ROLLBACK;
455-- lastval
456BEGIN;
457SET LOCAL SESSION AUTHORIZATION regress_seq_user;
458CREATE SEQUENCE seq3;
459SELECT nextval('seq3');
460 nextval
461---------
462       1
463(1 row)
464
465REVOKE ALL ON seq3 FROM regress_seq_user;
466GRANT SELECT ON seq3 TO regress_seq_user;
467SELECT lastval();
468 lastval
469---------
470       1
471(1 row)
472
473ROLLBACK;
474BEGIN;
475SET LOCAL SESSION AUTHORIZATION regress_seq_user;
476CREATE SEQUENCE seq3;
477SELECT nextval('seq3');
478 nextval
479---------
480       1
481(1 row)
482
483REVOKE ALL ON seq3 FROM regress_seq_user;
484GRANT UPDATE ON seq3 TO regress_seq_user;
485SELECT lastval();
486ERROR:  permission denied for sequence seq3
487ROLLBACK;
488BEGIN;
489SET LOCAL SESSION AUTHORIZATION regress_seq_user;
490CREATE SEQUENCE seq3;
491SELECT nextval('seq3');
492 nextval
493---------
494       1
495(1 row)
496
497REVOKE ALL ON seq3 FROM regress_seq_user;
498GRANT USAGE ON seq3 TO regress_seq_user;
499SELECT lastval();
500 lastval
501---------
502       1
503(1 row)
504
505ROLLBACK;
506-- Sequences should get wiped out as well:
507DROP TABLE serialTest, serialTest2;
508-- Make sure sequences are gone:
509SELECT * FROM information_schema.sequences WHERE sequence_name IN
510  ('sequence_test2', 'serialtest2_f2_seq', 'serialtest2_f3_seq',
511   'serialtest2_f4_seq', 'serialtest2_f5_seq', 'serialtest2_f6_seq')
512  ORDER BY sequence_name ASC;
513 sequence_catalog | sequence_schema | sequence_name  | data_type | numeric_precision | numeric_precision_radix | numeric_scale | start_value | minimum_value | maximum_value | increment | cycle_option
514------------------+-----------------+----------------+-----------+-------------------+-------------------------+---------------+-------------+---------------+---------------+-----------+--------------
515 regression       | public          | sequence_test2 | bigint    |                64 |                       2 |             0 | 32          | 5             | 36            | 4         | YES
516(1 row)
517
518DROP USER regress_seq_user;
519DROP SEQUENCE seq;
520