2003-03-20 08:02:11 +01:00
|
|
|
---
|
|
|
|
--- test creation of SERIAL column
|
|
|
|
---
|
|
|
|
CREATE TABLE serialTest (f1 text, f2 serial);
|
|
|
|
INSERT INTO serialTest VALUES ('foo');
|
|
|
|
INSERT INTO serialTest VALUES ('bar');
|
|
|
|
INSERT INTO serialTest VALUES ('force', 100);
|
|
|
|
INSERT INTO serialTest VALUES ('wrong', NULL);
|
2003-09-25 08:58:07 +02:00
|
|
|
ERROR: null value in column "f2" violates not-null constraint
|
2011-11-30 00:29:18 +01:00
|
|
|
DETAIL: Failing row contains (wrong, null).
|
2003-03-20 08:02:11 +01:00
|
|
|
SELECT * FROM serialTest;
|
|
|
|
f1 | f2
|
|
|
|
-------+-----
|
|
|
|
foo | 1
|
|
|
|
bar | 2
|
|
|
|
force | 100
|
|
|
|
(3 rows)
|
|
|
|
|
2011-06-22 04:57:08 +02:00
|
|
|
-- test smallserial / bigserial
|
|
|
|
CREATE TABLE serialTest2 (f1 text, f2 serial, f3 smallserial, f4 serial2,
|
|
|
|
f5 bigserial, f6 serial8);
|
|
|
|
INSERT INTO serialTest2 (f1)
|
|
|
|
VALUES ('test_defaults');
|
|
|
|
INSERT INTO serialTest2 (f1, f2, f3, f4, f5, f6)
|
|
|
|
VALUES ('test_max_vals', 2147483647, 32767, 32767, 9223372036854775807,
|
|
|
|
9223372036854775807),
|
|
|
|
('test_min_vals', -2147483648, -32768, -32768, -9223372036854775808,
|
|
|
|
-9223372036854775808);
|
|
|
|
-- All these INSERTs should fail:
|
|
|
|
INSERT INTO serialTest2 (f1, f3)
|
|
|
|
VALUES ('bogus', -32769);
|
|
|
|
ERROR: smallint out of range
|
|
|
|
INSERT INTO serialTest2 (f1, f4)
|
|
|
|
VALUES ('bogus', -32769);
|
|
|
|
ERROR: smallint out of range
|
|
|
|
INSERT INTO serialTest2 (f1, f3)
|
|
|
|
VALUES ('bogus', 32768);
|
|
|
|
ERROR: smallint out of range
|
|
|
|
INSERT INTO serialTest2 (f1, f4)
|
|
|
|
VALUES ('bogus', 32768);
|
|
|
|
ERROR: smallint out of range
|
|
|
|
INSERT INTO serialTest2 (f1, f5)
|
|
|
|
VALUES ('bogus', -9223372036854775809);
|
|
|
|
ERROR: bigint out of range
|
|
|
|
INSERT INTO serialTest2 (f1, f6)
|
|
|
|
VALUES ('bogus', -9223372036854775809);
|
|
|
|
ERROR: bigint out of range
|
|
|
|
INSERT INTO serialTest2 (f1, f5)
|
|
|
|
VALUES ('bogus', 9223372036854775808);
|
|
|
|
ERROR: bigint out of range
|
|
|
|
INSERT INTO serialTest2 (f1, f6)
|
|
|
|
VALUES ('bogus', 9223372036854775808);
|
|
|
|
ERROR: bigint out of range
|
|
|
|
SELECT * FROM serialTest2 ORDER BY f2 ASC;
|
|
|
|
f1 | f2 | f3 | f4 | f5 | f6
|
|
|
|
---------------+-------------+--------+--------+----------------------+----------------------
|
|
|
|
test_min_vals | -2147483648 | -32768 | -32768 | -9223372036854775808 | -9223372036854775808
|
|
|
|
test_defaults | 1 | 1 | 1 | 1 | 1
|
|
|
|
test_max_vals | 2147483647 | 32767 | 32767 | 9223372036854775807 | 9223372036854775807
|
|
|
|
(3 rows)
|
|
|
|
|
|
|
|
SELECT nextval('serialTest2_f2_seq');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('serialTest2_f3_seq');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('serialTest2_f4_seq');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('serialTest2_f5_seq');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('serialTest2_f6_seq');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
2005-10-03 01:50:16 +02:00
|
|
|
-- basic sequence operations using both text and oid references
|
2003-03-20 08:02:11 +01:00
|
|
|
CREATE SEQUENCE sequence_test;
|
2005-10-03 01:50:16 +02:00
|
|
|
SELECT nextval('sequence_test'::text);
|
2003-03-20 08:02:11 +01:00
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
2005-10-03 01:50:16 +02:00
|
|
|
SELECT nextval('sequence_test'::regclass);
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT currval('sequence_test'::text);
|
|
|
|
currval
|
|
|
|
---------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT currval('sequence_test'::regclass);
|
|
|
|
currval
|
|
|
|
---------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT setval('sequence_test'::text, 32);
|
|
|
|
setval
|
|
|
|
--------
|
|
|
|
32
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('sequence_test'::regclass);
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
33
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT setval('sequence_test'::text, 99, false);
|
|
|
|
setval
|
|
|
|
--------
|
|
|
|
99
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('sequence_test'::regclass);
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
99
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT setval('sequence_test'::regclass, 32);
|
|
|
|
setval
|
|
|
|
--------
|
|
|
|
32
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('sequence_test'::text);
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
33
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT setval('sequence_test'::regclass, 99, false);
|
|
|
|
setval
|
|
|
|
--------
|
|
|
|
99
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('sequence_test'::text);
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
99
|
|
|
|
(1 row)
|
|
|
|
|
2003-03-20 08:02:11 +01:00
|
|
|
DROP SEQUENCE sequence_test;
|
|
|
|
-- renaming sequences
|
|
|
|
CREATE SEQUENCE foo_seq;
|
|
|
|
ALTER TABLE foo_seq RENAME TO foo_seq_new;
|
2011-06-23 01:21:29 +02:00
|
|
|
SELECT * FROM foo_seq_new;
|
|
|
|
sequence_name | last_value | start_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called
|
|
|
|
---------------+------------+-------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
|
Fix longstanding crash-safety bug with newly-created-or-reset sequences.
If a crash occurred immediately after the first nextval() call for a serial
column, WAL replay would restore the sequence to a state in which it
appeared that no nextval() had been done, thus allowing the first sequence
value to be returned again by the next nextval() call; as reported in
bug #6748 from Xiangming Mei.
More generally, the problem would occur if an ALTER SEQUENCE was executed
on a freshly created or reset sequence. (The manifestation with serial
columns was introduced in 8.2 when we added an ALTER SEQUENCE OWNED BY step
to serial column creation.) The cause is that sequence creation attempted
to save one WAL entry by writing out a WAL record that made it appear that
the first nextval() had already happened (viz, with is_called = true),
while marking the sequence's in-database state with log_cnt = 1 to show
that the first nextval() need not emit a WAL record. However, ALTER
SEQUENCE would emit a new WAL entry reflecting the actual in-database state
(with is_called = false). Then, nextval would allocate the first sequence
value and set is_called = true, but it would trust the log_cnt value and
not emit any WAL record. A crash at this point would thus restore the
sequence to its post-ALTER state, causing the next nextval() call to return
the first sequence value again.
To fix, get rid of the idea of logging an is_called status different from
reality. This means that the first nextval-driven WAL record will happen
at the first nextval call not the second, but the marginal cost of that is
pretty negligible. In addition, make sure that ALTER SEQUENCE resets
log_cnt to zero in any case where it touches sequence parameters that
affect future nextval results. This will result in some user-visible
changes in the contents of a sequence's log_cnt column, as reflected in the
patch's regression test changes; but no application should be depending on
that anyway, since it was already true that log_cnt changes rather
unpredictably depending on checkpoint timing.
In addition, make some basically-cosmetic improvements to get rid of
sequence.c's undesirable intimacy with page layout details. It was always
really trying to WAL-log the contents of the sequence tuple, so we should
have it do that directly using a HeapTuple's t_data and t_len, rather than
backing into it with some magic assumptions about where the tuple would be
on the sequence's page.
Back-patch to all supported branches.
2012-07-25 23:40:36 +02:00
|
|
|
foo_seq | 1 | 1 | 1 | 9223372036854775807 | 1 | 1 | 0 | f | f
|
2008-05-17 01:36:05 +02:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('foo_seq_new');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('foo_seq_new');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
2
|
|
|
|
(1 row)
|
|
|
|
|
2011-06-23 01:21:29 +02:00
|
|
|
SELECT * FROM foo_seq_new;
|
|
|
|
sequence_name | last_value | start_value | increment_by | max_value | min_value | cache_value | log_cnt | is_cycled | is_called
|
|
|
|
---------------+------------+-------------+--------------+---------------------+-----------+-------------+---------+-----------+-----------
|
Fix longstanding crash-safety bug with newly-created-or-reset sequences.
If a crash occurred immediately after the first nextval() call for a serial
column, WAL replay would restore the sequence to a state in which it
appeared that no nextval() had been done, thus allowing the first sequence
value to be returned again by the next nextval() call; as reported in
bug #6748 from Xiangming Mei.
More generally, the problem would occur if an ALTER SEQUENCE was executed
on a freshly created or reset sequence. (The manifestation with serial
columns was introduced in 8.2 when we added an ALTER SEQUENCE OWNED BY step
to serial column creation.) The cause is that sequence creation attempted
to save one WAL entry by writing out a WAL record that made it appear that
the first nextval() had already happened (viz, with is_called = true),
while marking the sequence's in-database state with log_cnt = 1 to show
that the first nextval() need not emit a WAL record. However, ALTER
SEQUENCE would emit a new WAL entry reflecting the actual in-database state
(with is_called = false). Then, nextval would allocate the first sequence
value and set is_called = true, but it would trust the log_cnt value and
not emit any WAL record. A crash at this point would thus restore the
sequence to its post-ALTER state, causing the next nextval() call to return
the first sequence value again.
To fix, get rid of the idea of logging an is_called status different from
reality. This means that the first nextval-driven WAL record will happen
at the first nextval call not the second, but the marginal cost of that is
pretty negligible. In addition, make sure that ALTER SEQUENCE resets
log_cnt to zero in any case where it touches sequence parameters that
affect future nextval results. This will result in some user-visible
changes in the contents of a sequence's log_cnt column, as reflected in the
patch's regression test changes; but no application should be depending on
that anyway, since it was already true that log_cnt changes rather
unpredictably depending on checkpoint timing.
In addition, make some basically-cosmetic improvements to get rid of
sequence.c's undesirable intimacy with page layout details. It was always
really trying to WAL-log the contents of the sequence tuple, so we should
have it do that directly using a HeapTuple's t_data and t_len, rather than
backing into it with some magic assumptions about where the tuple would be
on the sequence's page.
Back-patch to all supported branches.
2012-07-25 23:40:36 +02:00
|
|
|
foo_seq | 2 | 1 | 1 | 9223372036854775807 | 1 | 1 | 31 | f | t
|
2003-03-20 08:02:11 +01:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
DROP SEQUENCE foo_seq_new;
|
2005-10-03 01:50:16 +02:00
|
|
|
-- renaming serial sequences
|
|
|
|
ALTER TABLE serialtest_f2_seq RENAME TO serialtest_f2_foo;
|
|
|
|
INSERT INTO serialTest VALUES ('more');
|
|
|
|
SELECT * FROM serialTest;
|
|
|
|
f1 | f2
|
|
|
|
-------+-----
|
|
|
|
foo | 1
|
|
|
|
bar | 2
|
|
|
|
force | 100
|
|
|
|
more | 3
|
|
|
|
(4 rows)
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Check dependencies of serial and ordinary sequences
|
|
|
|
--
|
|
|
|
CREATE TEMP SEQUENCE myseq2;
|
|
|
|
CREATE TEMP SEQUENCE myseq3;
|
|
|
|
CREATE TEMP TABLE t1 (
|
|
|
|
f1 serial,
|
|
|
|
f2 int DEFAULT nextval('myseq2'),
|
|
|
|
f3 int DEFAULT nextval('myseq3'::text)
|
|
|
|
);
|
|
|
|
-- Both drops should fail, but with different error messages:
|
|
|
|
DROP SEQUENCE t1_f1_seq;
|
2006-08-21 02:57:26 +02:00
|
|
|
ERROR: cannot drop sequence t1_f1_seq because other objects depend on it
|
2008-06-11 23:53:49 +02:00
|
|
|
DETAIL: default for table t1 column f1 depends on sequence t1_f1_seq
|
2006-08-21 02:57:26 +02:00
|
|
|
HINT: Use DROP ... CASCADE to drop the dependent objects too.
|
2005-10-03 01:50:16 +02:00
|
|
|
DROP SEQUENCE myseq2;
|
|
|
|
ERROR: cannot drop sequence myseq2 because other objects depend on it
|
2008-06-11 23:53:49 +02:00
|
|
|
DETAIL: default for table t1 column f2 depends on sequence myseq2
|
2005-10-03 01:50:16 +02:00
|
|
|
HINT: Use DROP ... CASCADE to drop the dependent objects too.
|
|
|
|
-- This however will work:
|
|
|
|
DROP SEQUENCE myseq3;
|
|
|
|
DROP TABLE t1;
|
|
|
|
-- Fails because no longer existent:
|
|
|
|
DROP SEQUENCE t1_f1_seq;
|
|
|
|
ERROR: sequence "t1_f1_seq" does not exist
|
|
|
|
-- Now OK:
|
|
|
|
DROP SEQUENCE myseq2;
|
2003-03-20 08:02:11 +01:00
|
|
|
--
|
|
|
|
-- Alter sequence
|
|
|
|
--
|
2012-01-24 00:25:04 +01:00
|
|
|
ALTER SEQUENCE IF EXISTS sequence_test2 RESTART WITH 24
|
|
|
|
INCREMENT BY 4 MAXVALUE 36 MINVALUE 5 CYCLE;
|
|
|
|
NOTICE: relation "sequence_test2" does not exist, skipping
|
2003-03-20 08:02:11 +01:00
|
|
|
CREATE SEQUENCE sequence_test2 START WITH 32;
|
|
|
|
SELECT nextval('sequence_test2');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
32
|
|
|
|
(1 row)
|
|
|
|
|
2008-05-17 01:36:05 +02:00
|
|
|
ALTER SEQUENCE sequence_test2 RESTART WITH 24
|
|
|
|
INCREMENT BY 4 MAXVALUE 36 MINVALUE 5 CYCLE;
|
|
|
|
SELECT nextval('sequence_test2');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
24
|
|
|
|
(1 row)
|
|
|
|
|
2003-03-20 08:02:11 +01:00
|
|
|
SELECT nextval('sequence_test2');
|
|
|
|
nextval
|
|
|
|
---------
|
2008-05-17 01:36:05 +02:00
|
|
|
28
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('sequence_test2');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
32
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('sequence_test2');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
36
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('sequence_test2');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
5
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
ALTER SEQUENCE sequence_test2 RESTART;
|
|
|
|
SELECT nextval('sequence_test2');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
32
|
2003-03-20 08:02:11 +01:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('sequence_test2');
|
|
|
|
nextval
|
|
|
|
---------
|
2008-05-17 01:36:05 +02:00
|
|
|
36
|
2003-03-20 08:02:11 +01:00
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT nextval('sequence_test2');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
5
|
|
|
|
(1 row)
|
|
|
|
|
2011-01-02 14:08:08 +01:00
|
|
|
-- Information schema
|
2011-06-22 04:57:08 +02:00
|
|
|
SELECT * FROM information_schema.sequences WHERE sequence_name IN
|
|
|
|
('sequence_test2', 'serialtest2_f2_seq', 'serialtest2_f3_seq',
|
|
|
|
'serialtest2_f4_seq', 'serialtest2_f5_seq', 'serialtest2_f6_seq')
|
|
|
|
ORDER BY sequence_name ASC;
|
|
|
|
sequence_catalog | sequence_schema | sequence_name | data_type | numeric_precision | numeric_precision_radix | numeric_scale | start_value | minimum_value | maximum_value | increment | cycle_option
|
|
|
|
------------------+-----------------+--------------------+-----------+-------------------+-------------------------+---------------+-------------+---------------+---------------------+-----------+--------------
|
|
|
|
regression | public | sequence_test2 | bigint | 64 | 2 | 0 | 32 | 5 | 36 | 4 | YES
|
|
|
|
regression | public | serialtest2_f2_seq | bigint | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO
|
|
|
|
regression | public | serialtest2_f3_seq | bigint | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO
|
|
|
|
regression | public | serialtest2_f4_seq | bigint | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO
|
|
|
|
regression | public | serialtest2_f5_seq | bigint | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO
|
|
|
|
regression | public | serialtest2_f6_seq | bigint | 64 | 2 | 0 | 1 | 1 | 9223372036854775807 | 1 | NO
|
|
|
|
(6 rows)
|
2011-01-02 14:08:08 +01:00
|
|
|
|
2003-11-21 23:32:49 +01:00
|
|
|
-- Test comments
|
|
|
|
COMMENT ON SEQUENCE asdf IS 'won''t work';
|
|
|
|
ERROR: relation "asdf" does not exist
|
|
|
|
COMMENT ON SEQUENCE sequence_test2 IS 'will work';
|
|
|
|
COMMENT ON SEQUENCE sequence_test2 IS NULL;
|
2005-06-07 09:08:35 +02:00
|
|
|
-- Test lastval()
|
|
|
|
CREATE SEQUENCE seq;
|
|
|
|
SELECT nextval('seq');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT lastval();
|
|
|
|
lastval
|
|
|
|
---------
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT setval('seq', 99);
|
|
|
|
setval
|
|
|
|
--------
|
|
|
|
99
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT lastval();
|
|
|
|
lastval
|
|
|
|
---------
|
|
|
|
99
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
CREATE SEQUENCE seq2;
|
|
|
|
SELECT nextval('seq2');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT lastval();
|
|
|
|
lastval
|
|
|
|
---------
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
DROP SEQUENCE seq2;
|
|
|
|
-- should fail
|
|
|
|
SELECT lastval();
|
|
|
|
ERROR: lastval is not yet defined in this session
|
|
|
|
CREATE USER seq_user;
|
|
|
|
BEGIN;
|
|
|
|
SET LOCAL SESSION AUTHORIZATION seq_user;
|
|
|
|
CREATE SEQUENCE seq3;
|
|
|
|
SELECT nextval('seq3');
|
|
|
|
nextval
|
|
|
|
---------
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
REVOKE ALL ON seq3 FROM seq_user;
|
|
|
|
SELECT lastval();
|
|
|
|
ERROR: permission denied for sequence seq3
|
|
|
|
ROLLBACK;
|
2011-06-22 04:57:08 +02:00
|
|
|
-- Sequences should get wiped out as well:
|
|
|
|
DROP TABLE serialTest, serialTest2;
|
|
|
|
-- Make sure sequences are gone:
|
|
|
|
SELECT * FROM information_schema.sequences WHERE sequence_name IN
|
|
|
|
('sequence_test2', 'serialtest2_f2_seq', 'serialtest2_f3_seq',
|
|
|
|
'serialtest2_f4_seq', 'serialtest2_f5_seq', 'serialtest2_f6_seq')
|
|
|
|
ORDER BY sequence_name ASC;
|
|
|
|
sequence_catalog | sequence_schema | sequence_name | data_type | numeric_precision | numeric_precision_radix | numeric_scale | start_value | minimum_value | maximum_value | increment | cycle_option
|
|
|
|
------------------+-----------------+----------------+-----------+-------------------+-------------------------+---------------+-------------+---------------+---------------+-----------+--------------
|
|
|
|
regression | public | sequence_test2 | bigint | 64 | 2 | 0 | 32 | 5 | 36 | 4 | YES
|
|
|
|
(1 row)
|
|
|
|
|
2005-06-07 09:08:35 +02:00
|
|
|
DROP USER seq_user;
|
|
|
|
DROP SEQUENCE seq;
|