提交 fa6cbd47 编写于 作者: N Nikos Armenatzoglou 提交者: Nikos Armenatzoglou

Error out when a sequence with NO CYCLE specified reaches maxvalue.

In a sequence, if NO CYCLE is specified, any calls to nextval after the sequence has reached its maximum value will return an error.
上级 8d2fa96c
......@@ -873,7 +873,18 @@ nextval_internal(Oid relid)
&is_overflow);
last_used_seq = elm;
relation_close(seqrel, NoLock);
if(is_overflow)
{
relation_close(seqrel, NoLock);
ereport(ERROR,
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
errmsg("nextval: reached %s value of sequence \"%s\" (" INT64_FORMAT ")",
elm->increment>0 ? "maximum":"minimum",
RelationGetRelationName(seqrel), elm->last)));
}
relation_close(seqrel, NoLock);
return elm->last;
}
......
---
--- Test Overflow with NO CYCLE
---
CREATE TABLE tmp_table (a int);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
INSERT INTO tmp_table VALUES (0),(1),(2),(3);
-- Test execution of nextval on master with CACHE 1
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2 START 1 CACHE 1 NO CYCLE;
SELECT nextval('tmp_seq');
nextval
---------
1
(1 row)
SELECT nextval('tmp_seq');
nextval
---------
2
(1 row)
-- Fails because it reaches MAXVALUE
SELECT nextval('tmp_seq');
ERROR: nextval: reached maximum value of sequence "tmp_seq" (2)
DROP SEQUENCE tmp_seq;
-- Test that ORCA and Planner return the same results although they produce different execution plans.
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 4 START 1 CACHE 1 NO CYCLE;
SELECT val from (SELECT nextval('tmp_seq'), a as val FROM tmp_table ORDER BY a) as val ORDER BY val;
val
-----
0
1
2
3
(4 rows)
DROP SEQUENCE tmp_seq;
-- Test execution of nextval on master with CACHE > 1
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2 START 1 CACHE 20 NO CYCLE;
SELECT nextval('tmp_seq');
nextval
---------
1
(1 row)
SELECT nextval('tmp_seq');
nextval
---------
2
(1 row)
-- Fails because it reaches MAXVALUE
SELECT nextval('tmp_seq');
ERROR: nextval: reached maximum value of sequence "tmp_seq" (2)
DROP SEQUENCE tmp_seq;
-- Test execution of nextval on master (when optimizer = on) and segments (when optimizer=off) with CACHE > 1
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 4 START 1 CACHE 20 NO CYCLE;
SELECT nextval('tmp_seq'), a FROM tmp_table ORDER BY a;
ERROR: nextval: reached maximum value of sequence "tmp_seq" (4) (seg0 slice1 nikos-mac:40001 pid=78074)
DROP SEQUENCE tmp_seq;
DROP TABLE tmp_table;
---
--- Test Overflow with NO CYCLE
---
CREATE TABLE tmp_table (a int);
NOTICE: Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Greenplum Database data distribution key for this table.
HINT: The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
INSERT INTO tmp_table VALUES (0),(1),(2),(3);
-- Test execution of nextval on master with CACHE 1
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2 START 1 CACHE 1 NO CYCLE;
SELECT nextval('tmp_seq');
nextval
---------
1
(1 row)
SELECT nextval('tmp_seq');
nextval
---------
2
(1 row)
-- Fails because it reaches MAXVALUE
SELECT nextval('tmp_seq');
ERROR: nextval: reached maximum value of sequence "tmp_seq" (2)
DROP SEQUENCE tmp_seq;
-- Test that ORCA and Planner return the same results although they produce different execution plans.
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 4 START 1 CACHE 1 NO CYCLE;
SELECT val from (SELECT nextval('tmp_seq'), a as val FROM tmp_table ORDER BY a) as val ORDER BY val;
val
-----
0
1
2
3
(4 rows)
DROP SEQUENCE tmp_seq;
-- Test execution of nextval on master with CACHE > 1
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2 START 1 CACHE 20 NO CYCLE;
SELECT nextval('tmp_seq');
nextval
---------
1
(1 row)
SELECT nextval('tmp_seq');
nextval
---------
2
(1 row)
-- Fails because it reaches MAXVALUE
SELECT nextval('tmp_seq');
ERROR: nextval: reached maximum value of sequence "tmp_seq" (2)
DROP SEQUENCE tmp_seq;
-- Test execution of nextval on master (when optimizer = on) and segments (when optimizer=off) with CACHE > 1
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 4 START 1 CACHE 20 NO CYCLE;
SELECT nextval('tmp_seq'), a FROM tmp_table ORDER BY a;
nextval | a
---------+---
1 | 0
2 | 1
3 | 2
4 | 3
(4 rows)
DROP SEQUENCE tmp_seq;
DROP TABLE tmp_table;
......@@ -17,6 +17,8 @@ test: column_compression rangefuncs_cdb
test: gpcopy appendonly filter gpctas gpdist matrix gpdtm_plpgsql
test: sequence_gp
# FIXME: Temporarily disabled, because it trips an assertion. It's probably
# harmless, but need to investigate and fix. Also, the number of errors put
# in the error table, and hence the output, varies between runs.
......
---
--- Test Overflow with NO CYCLE
---
CREATE TABLE tmp_table (a int);
INSERT INTO tmp_table VALUES (0),(1),(2),(3);
-- Test execution of nextval on master with CACHE 1
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2 START 1 CACHE 1 NO CYCLE;
SELECT nextval('tmp_seq');
SELECT nextval('tmp_seq');
-- Fails because it reaches MAXVALUE
SELECT nextval('tmp_seq');
DROP SEQUENCE tmp_seq;
-- Test that ORCA and Planner return the same results although they produce different execution plans.
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 4 START 1 CACHE 1 NO CYCLE;
SELECT val from (SELECT nextval('tmp_seq'), a as val FROM tmp_table ORDER BY a) as val ORDER BY val;
DROP SEQUENCE tmp_seq;
-- Test execution of nextval on master with CACHE > 1
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2 START 1 CACHE 20 NO CYCLE;
SELECT nextval('tmp_seq');
SELECT nextval('tmp_seq');
-- Fails because it reaches MAXVALUE
SELECT nextval('tmp_seq');
DROP SEQUENCE tmp_seq;
-- Test execution of nextval on master (when optimizer = on) and segments (when optimizer=off) with CACHE > 1
CREATE SEQUENCE tmp_seq INCREMENT 1 MINVALUE 1 MAXVALUE 4 START 1 CACHE 20 NO CYCLE;
SELECT nextval('tmp_seq'), a FROM tmp_table ORDER BY a;
DROP SEQUENCE tmp_seq;
DROP TABLE tmp_table;
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册