2 --- test creation of SERIAL column
5 CREATE TABLE serialTest (f1 text, f2 serial);
6 NOTICE: CREATE TABLE will create implicit sequence "serialtest_f2_seq" for serial column "serialtest.f2"
8 INSERT INTO serialTest VALUES ('foo');
9 INSERT INTO serialTest VALUES ('bar');
10 INSERT INTO serialTest VALUES ('force', 100);
11 INSERT INTO serialTest VALUES ('wrong', NULL);
12 ERROR: null value in column "f2" violates not-null constraint
14 SELECT * FROM serialTest;
22 -- basic sequence operations using both text and oid references
23 CREATE SEQUENCE sequence_test;
25 SELECT nextval('sequence_test'::text);
31 SELECT nextval('sequence_test'::regclass);
37 SELECT currval('sequence_test'::text);
43 SELECT currval('sequence_test'::regclass);
49 SELECT setval('sequence_test'::text, 32);
55 SELECT nextval('sequence_test'::regclass);
61 SELECT setval('sequence_test'::text, 99, false);
67 SELECT nextval('sequence_test'::regclass);
73 SELECT setval('sequence_test'::regclass, 32);
79 SELECT nextval('sequence_test'::text);
85 SELECT setval('sequence_test'::regclass, 99, false);
91 SELECT nextval('sequence_test'::text);
97 DROP SEQUENCE sequence_test;
99 CREATE SEQUENCE foo_seq;
100 ALTER TABLE foo_seq RENAME TO foo_seq_new;
101 SELECT * FROM foo_seq_new;
102 sequence_name | last_value | start_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called
103 ---------------+------------+-------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
104 foo_seq | 1 | 1 | 1 | 9223372036854775807 | 1 | 1 | 1 | f | f
107 SELECT nextval('foo_seq_new');
113 SELECT nextval('foo_seq_new');
119 SELECT * FROM foo_seq_new;
120 sequence_name | last_value | start_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called
121 ---------------+------------+-------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
122 foo_seq | 2 | 1 | 1 | 9223372036854775807 | 1 | 1 | 31 | f | t
125 DROP SEQUENCE foo_seq_new;
126 -- renaming serial sequences
127 ALTER TABLE serialtest_f2_seq RENAME TO serialtest_f2_foo;
128 INSERT INTO serialTest VALUES ('more');
129 SELECT * FROM serialTest;
139 -- Check dependencies of serial and ordinary sequences
141 CREATE TEMP SEQUENCE myseq2;
142 CREATE TEMP SEQUENCE myseq3;
143 CREATE TEMP TABLE t1 (
145 f2 int DEFAULT nextval('myseq2'),
146 f3 int DEFAULT nextval('myseq3'::text)
148 NOTICE: CREATE TABLE will create implicit sequence "t1_f1_seq" for serial column "t1.f1"
149 -- Both drops should fail, but with different error messages:
150 DROP SEQUENCE t1_f1_seq;
151 ERROR: cannot drop sequence t1_f1_seq because other objects depend on it
152 DETAIL: default for table t1 column f1 depends on sequence t1_f1_seq
153 HINT: Use DROP ... CASCADE to drop the dependent objects too.
154 DROP SEQUENCE myseq2;
155 ERROR: cannot drop sequence myseq2 because other objects depend on it
156 DETAIL: default for table t1 column f2 depends on sequence myseq2
157 HINT: Use DROP ... CASCADE to drop the dependent objects too.
158 -- This however will work:
159 DROP SEQUENCE myseq3;
161 -- Fails because no longer existent:
162 DROP SEQUENCE t1_f1_seq;
163 ERROR: sequence "t1_f1_seq" does not exist
165 DROP SEQUENCE myseq2;
169 CREATE SEQUENCE sequence_test2 START WITH 32;
170 SELECT nextval('sequence_test2');
176 ALTER SEQUENCE sequence_test2 RESTART WITH 24
177 INCREMENT BY 4 MAXVALUE 36 MINVALUE 5 CYCLE;
178 SELECT nextval('sequence_test2');
184 SELECT nextval('sequence_test2');
190 SELECT nextval('sequence_test2');
196 SELECT nextval('sequence_test2');
202 SELECT nextval('sequence_test2');
208 ALTER SEQUENCE sequence_test2 RESTART;
209 SELECT nextval('sequence_test2');
215 SELECT nextval('sequence_test2');
221 SELECT nextval('sequence_test2');
228 COMMENT ON SEQUENCE asdf IS 'won''t work';
229 ERROR: relation "asdf" does not exist
230 COMMENT ON SEQUENCE sequence_test2 IS 'will work';
231 COMMENT ON SEQUENCE sequence_test2 IS NULL;
234 SELECT nextval('seq');
246 SELECT setval('seq', 99);
258 CREATE SEQUENCE seq2;
259 SELECT nextval('seq2');
274 ERROR: lastval is not yet defined in this session
275 CREATE USER seq_user;
277 SET LOCAL SESSION AUTHORIZATION seq_user;
278 CREATE SEQUENCE seq3;
279 SELECT nextval('seq3');
285 REVOKE ALL ON seq3 FROM seq_user;
287 ERROR: permission denied for sequence seq3