Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ DATA = snowflake--1.0.sql \
snowflake--2.2.sql \
snowflake--2.2--2.3.sql \
snowflake--2.3.sql \
snowflake--2.3--2.4.sql
snowflake--2.3--2.4.sql \
snowflake--2.4--2.5.sql
PGFILEDESC = "snowflake - snowflake style IDs for PostgreSQL"

REGRESS = conversion
REGRESS = conversion maxvalue

ifdef USE_PGXS
PG_CONFIG = pg_config
Expand Down
18 changes: 9 additions & 9 deletions expected/conversion.out
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ INSERT INTO t3 VALUES (DEFAULT);
SELECT snowflake.convert_sequence_to_snowflake('t1'); -- ERROR, not a sequence
ERROR: Input value "public.t1" is not a valid convertable sequence
SELECT snowflake.convert_sequence_to_snowflake('seq_1'); -- No associated relation found
NOTICE: ALTER SEQUENCE public.seq_1 NO CYCLE MAXVALUE 43
NOTICE: ALTER SEQUENCE public.seq_1 AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
0
Expand All @@ -26,7 +26,7 @@ NOTICE: ALTER SEQUENCE public.seq_1 NO CYCLE MAXVALUE 43
SELECT snowflake.convert_sequence_to_snowflake('t1_x_seq');
NOTICE: EXECUTE ALTER TABLE public.t1 ALTER COLUMN x SET DATA TYPE int8
NOTICE: EXECUTE ALTER TABLE public.t1 ALTER COLUMN x SET DEFAULT snowflake.nextval('public.t1_x_seq'::regclass)
NOTICE: ALTER SEQUENCE public.t1_x_seq NO CYCLE MAXVALUE 2
NOTICE: ALTER SEQUENCE public.t1_x_seq AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
1
Expand All @@ -36,7 +36,7 @@ SELECT snowflake.convert_sequence_to_snowflake('t2_x_seq');
NOTICE: Update pg_attribute: reset attidentity value for table public.t2, column x
NOTICE: EXECUTE ALTER TABLE public.t2 ALTER COLUMN x SET DATA TYPE int8
NOTICE: EXECUTE ALTER TABLE public.t2 ALTER COLUMN x SET DEFAULT snowflake.nextval('public.t2_x_seq'::regclass)
NOTICE: ALTER SEQUENCE public.t2_x_seq NO CYCLE MAXVALUE 2
NOTICE: ALTER SEQUENCE public.t2_x_seq AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
1
Expand All @@ -46,7 +46,7 @@ SELECT snowflake.convert_sequence_to_snowflake('t3_x_seq');
NOTICE: Update pg_attribute: reset attidentity value for table public.t3, column x
NOTICE: EXECUTE ALTER TABLE public.t3 ALTER COLUMN x SET DATA TYPE int8
NOTICE: EXECUTE ALTER TABLE public.t3 ALTER COLUMN x SET DEFAULT snowflake.nextval('public.t3_x_seq'::regclass)
NOTICE: ALTER SEQUENCE public.t3_x_seq NO CYCLE MAXVALUE 2
NOTICE: ALTER SEQUENCE public.t3_x_seq AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
1
Expand All @@ -55,7 +55,7 @@ NOTICE: ALTER SEQUENCE public.t3_x_seq NO CYCLE MAXVALUE 2
SELECT snowflake.convert_sequence_to_snowflake('t3_z_seq'); -- non-default attnum
NOTICE: EXECUTE ALTER TABLE public.t3 ALTER COLUMN z SET DATA TYPE int8
NOTICE: EXECUTE ALTER TABLE public.t3 ALTER COLUMN z SET DEFAULT snowflake.nextval('public.t3_z_seq'::regclass)
NOTICE: ALTER SEQUENCE public.t3_z_seq NO CYCLE MAXVALUE 2
NOTICE: ALTER SEQUENCE public.t3_z_seq AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
1
Expand Down Expand Up @@ -114,7 +114,7 @@ ALTER TABLE t3 ALTER COLUMN y SET DEFAULT nextval('t3_y_seq'::regclass);
SELECT snowflake.convert_sequence_to_snowflake('t3_y_seq');
NOTICE: EXECUTE ALTER TABLE public.t3 ALTER COLUMN y SET DATA TYPE int8
NOTICE: EXECUTE ALTER TABLE public.t3 ALTER COLUMN y SET DEFAULT snowflake.nextval('public.t3_y_seq'::regclass)
NOTICE: ALTER SEQUENCE public.t3_y_seq NO CYCLE MAXVALUE 3
NOTICE: ALTER SEQUENCE public.t3_y_seq AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
1
Expand Down Expand Up @@ -162,7 +162,7 @@ SELECT * FROM t4;
SELECT snowflake.convert_sequence_to_snowflake('favorite_seq');
NOTICE: EXECUTE ALTER TABLE public.t4 ALTER COLUMN x SET DATA TYPE int8
NOTICE: EXECUTE ALTER TABLE public.t4 ALTER COLUMN x SET DEFAULT snowflake.nextval('public.favorite_seq'::regclass)
NOTICE: ALTER SEQUENCE public.favorite_seq NO CYCLE MAXVALUE 2
NOTICE: ALTER SEQUENCE public.favorite_seq AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
2
Expand Down Expand Up @@ -201,7 +201,7 @@ NOTICE: EXECUTE ALTER TABLE public.t4 ALTER COLUMN x SET DATA TYPE int8
NOTICE: EXECUTE ALTER TABLE public.t4 ALTER COLUMN x SET DEFAULT snowflake.nextval('public.favorite_seq'::regclass)
NOTICE: EXECUTE ALTER TABLE public.t5 ALTER COLUMN x SET DATA TYPE int8
NOTICE: EXECUTE ALTER TABLE public.t5 ALTER COLUMN x SET DEFAULT snowflake.nextval('public.favorite_seq'::regclass)
NOTICE: ALTER SEQUENCE public.favorite_seq NO CYCLE MAXVALUE 3
NOTICE: ALTER SEQUENCE public.favorite_seq AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
4
Expand Down Expand Up @@ -238,7 +238,7 @@ SELECT * FROM t6;

SELECT snowflake.convert_sequence_to_snowflake('t6_x_seq');
NOTICE: EXECUTE ALTER TABLE public.t6 ALTER COLUMN x SET DEFAULT snowflake.nextval('public.t6_x_seq'::regclass)
NOTICE: ALTER SEQUENCE public.t6_x_seq NO CYCLE MAXVALUE 4
NOTICE: ALTER SEQUENCE public.t6_x_seq AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
0
Expand Down
100 changes: 100 additions & 0 deletions expected/maxvalue.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* maxvalue.sql - regression for the SUP-140 dump/restore failure.
*
* Before the fix, convert_sequence_to_snowflake() set
* ALTER SEQUENCE ... MAXVALUE (last_value + 1)
* which is fine for the snowflake.nextval() path (it bypasses MAXVALUE)
* but fatal for pg_dump/restore: the dump captures the resulting
* snowflake-sized last_value via pg_catalog.setval(...) and the
* restore is rejected with
* ERROR: setval: value <big_snowflake_id> is out of bounds for
* sequence "<seq>" (1..<small>)
*
* This test asserts that after conversion the sequence's max_value is
* the full bigint range (2^63-1).
*/
\set VERBOSITY terse
SET snowflake.node = 1;
CREATE EXTENSION snowflake;
-- ----------------------------------------------------------------------
-- 1. Customer reproducer: bigserial table, INSERT, convert.
-- ----------------------------------------------------------------------
CREATE TABLE orders (
id bigserial PRIMARY KEY,
customer text NOT NULL,
amount numeric NOT NULL
);
INSERT INTO orders (customer, amount) VALUES
('Alice', 100.00),
('Bob', 250.00),
('Carol', 75.50);
SELECT snowflake.convert_sequence_to_snowflake('orders_id_seq'::regclass);
NOTICE: EXECUTE ALTER TABLE public.orders ALTER COLUMN id SET DEFAULT snowflake.nextval('public.orders_id_seq'::regclass)
NOTICE: ALTER SEQUENCE public.orders_id_seq AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
0
(1 row)

-- After conversion max_value MUST be the bigint ceiling, not (last_value+1).
SELECT max_value = 9223372036854775807 AS max_is_bigint_max
FROM pg_sequences WHERE sequencename = 'orders_id_seq';
max_is_bigint_max
-------------------
t
(1 row)

-- ----------------------------------------------------------------------
-- 2. Generate a real snowflake-sized id; setval to that value must succeed
-- (this is what pg_dump emits in the restore script).
-- ----------------------------------------------------------------------
INSERT INTO orders (customer, amount) VALUES ('Dave', 999) RETURNING id > 9999;
?column?
----------
t
(1 row)

-- A pg_catalog.setval() with a snowflake-sized value must NOT raise
-- "out of bounds for sequence" any more.
SELECT pg_catalog.setval('orders_id_seq', 4446196691613229056, true);
setval
---------------------
4446196691613229056
(1 row)

SELECT last_value FROM orders_id_seq;
last_value
---------------------
4446196691613229056
(1 row)

-- ----------------------------------------------------------------------
-- 3. Plain (un-owned) sequence path.
-- ----------------------------------------------------------------------
CREATE SEQUENCE standalone_seq START 100;
SELECT snowflake.convert_sequence_to_snowflake('standalone_seq'::regclass);
NOTICE: ALTER SEQUENCE public.standalone_seq AS bigint NO CYCLE MAXVALUE 9223372036854775807
convert_sequence_to_snowflake
-------------------------------
0
(1 row)

SELECT max_value = 9223372036854775807 AS max_is_bigint_max
FROM pg_sequences WHERE sequencename = 'standalone_seq';
max_is_bigint_max
-------------------
t
(1 row)

SELECT pg_catalog.setval('standalone_seq', 4446196691613229056, true);
setval
---------------------
4446196691613229056
(1 row)

-- ----------------------------------------------------------------------
-- Cleanup
-- ----------------------------------------------------------------------
DROP TABLE orders CASCADE;
DROP SEQUENCE standalone_seq;
DROP EXTENSION snowflake;
21 changes: 6 additions & 15 deletions snowflake--1.2--2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ DECLARE
v_seq record;
v_cmd text;
v_num_altered integer := 0;
v_last_value bigint;
BEGIN
-- ----
-- We are looking for column defaults that use the requested
Expand Down Expand Up @@ -175,25 +174,17 @@ BEGIN
END LOOP;

-- ----
-- If we found something, we need to change the sequence itself to
-- settings that prevent pg_catalog.nextval() from working. We do this
-- by setting the sequence's MAXVAL to its current last_value + 1,
-- then invoke our own nextval() function to bump it.
-- If we found something, bump the sequence past its current
-- last_value by invoking snowflake.nextval(). The MAXVALUE is
-- left at the full bigint range (2^63-1) so subsequent
-- snowflake IDs - and the pg_catalog.setval() calls that pg_dump
-- emits during a restore - all stay within bounds.
-- ----
IF v_num_altered > 0 THEN
v_cmd = 'SELECT last_value FROM ' ||
pg_catalog.quote_ident(N.nspname) || '.' ||
pg_catalog.quote_ident(C.relname)
FROM pg_catalog.pg_class C
JOIN pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE C.oid = p_seqid;
EXECUTE v_cmd INTO v_last_value;

v_cmd = 'ALTER SEQUENCE ' ||
pg_catalog.quote_ident(N.nspname) || '.' ||
pg_catalog.quote_ident(C.relname) ||
' NO CYCLE MAXVALUE ' ||
v_last_value + 1
' AS bigint NO CYCLE MAXVALUE 9223372036854775807'
FROM pg_catalog.pg_class C
JOIN pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE C.oid = p_seqid;
Expand Down
21 changes: 6 additions & 15 deletions snowflake--2.0--2.2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ DECLARE
v_seq record;
v_cmd text;
v_num_altered integer := 0;
v_last_value bigint;

v_seqname1 text;
v_seqname2 text;
Expand Down Expand Up @@ -109,24 +108,16 @@ BEGIN
END LOOP;

-- ----
-- Finally we need to change the sequence itself to settings that
-- prevent pg_catalog.nextval() from working. We do this by setting
-- the sequence's MAXVAL to its current last_value + 1, then invoke
-- our own nextval() function to bump it.
-- Finally bump the sequence past its current last_value by
-- invoking snowflake.nextval(). The MAXVALUE is left at the
-- full bigint range (2^63-1) so subsequent snowflake IDs - and
-- the pg_catalog.setval() calls that pg_dump emits during a
-- restore - all stay within bounds.
-- ----
v_cmd = 'SELECT last_value FROM ' ||
pg_catalog.quote_ident(N.nspname) || '.' ||
pg_catalog.quote_ident(C.relname)
FROM pg_catalog.pg_class C
JOIN pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE C.oid = p_seqid;
EXECUTE v_cmd INTO v_last_value;

v_cmd = 'ALTER SEQUENCE ' ||
pg_catalog.quote_ident(N.nspname) || '.' ||
pg_catalog.quote_ident(C.relname) ||
' NO CYCLE MAXVALUE ' ||
v_last_value + 1
' AS bigint NO CYCLE MAXVALUE 9223372036854775807'
FROM pg_catalog.pg_class C
JOIN pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE C.oid = p_seqid;
Expand Down
21 changes: 6 additions & 15 deletions snowflake--2.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ DECLARE
v_seq record;
v_cmd text;
v_num_altered integer := 0;
v_last_value bigint;
BEGIN
-- ----
-- We are looking for column defaults that use the requested
Expand Down Expand Up @@ -220,25 +219,17 @@ BEGIN
END LOOP;

-- ----
-- If we found something, we need to change the sequence itself to
-- settings that prevent pg_catalog.nextval() from working. We do this
-- by setting the sequence's MAXVAL to its current last_value + 1,
-- then invoke our own nextval() function to bump it.
-- If we found something, bump the sequence past its current
-- last_value by invoking snowflake.nextval(). The MAXVALUE is
-- left at the full bigint range (2^63-1) so subsequent
-- snowflake IDs - and the pg_catalog.setval() calls that pg_dump
-- emits during a restore - all stay within bounds.
-- ----
IF v_num_altered > 0 THEN
v_cmd = 'SELECT last_value FROM ' ||
pg_catalog.quote_ident(N.nspname) || '.' ||
pg_catalog.quote_ident(C.relname)
FROM pg_catalog.pg_class C
JOIN pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE C.oid = p_seqid;
EXECUTE v_cmd INTO v_last_value;

v_cmd = 'ALTER SEQUENCE ' ||
pg_catalog.quote_ident(N.nspname) || '.' ||
pg_catalog.quote_ident(C.relname) ||
' NO CYCLE MAXVALUE ' ||
v_last_value + 1
' AS bigint NO CYCLE MAXVALUE 9223372036854775807'
FROM pg_catalog.pg_class C
JOIN pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE C.oid = p_seqid;
Expand Down
21 changes: 6 additions & 15 deletions snowflake--2.2--2.3.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ DECLARE
v_attrdef record;
v_cmd text;
v_num_altered integer;
v_last_value bigint;

v_seqname1 text;
extseqname text;
Expand Down Expand Up @@ -133,24 +132,16 @@ BEGIN
EXECUTE v_cmd;

-- ----
-- Finally we need to change the sequence itself to settings that
-- prevent pg_catalog.nextval() from working. We do this by setting
-- the sequence's MAXVAL to its current last_value + 1, then invoke
-- our own nextval() function to bump it.
-- Finally bump the sequence past its current last_value by
-- invoking snowflake.nextval(). The MAXVALUE is left at the
-- full bigint range (2^63-1) so subsequent snowflake IDs - and
-- the pg_catalog.setval() calls that pg_dump emits during a
-- restore - all stay within bounds.
-- ----
v_cmd = 'SELECT last_value FROM ' ||
pg_catalog.quote_ident(N.nspname) || '.' ||
pg_catalog.quote_ident(C.relname)
FROM pg_catalog.pg_class C
JOIN pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE C.oid = p_seqid;
EXECUTE v_cmd INTO v_last_value;

v_cmd = 'ALTER SEQUENCE ' ||
pg_catalog.quote_ident(N.nspname) || '.' ||
pg_catalog.quote_ident(C.relname) ||
' NO CYCLE MAXVALUE ' ||
v_last_value + 1
' AS bigint NO CYCLE MAXVALUE 9223372036854775807'
FROM pg_catalog.pg_class C
JOIN pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE C.oid = p_seqid;
Expand Down
21 changes: 6 additions & 15 deletions snowflake--2.2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ DECLARE
v_seq record;
v_cmd text;
v_num_altered integer := 0;
v_last_value bigint;

v_seqname1 text;
v_seqname2 text;
Expand Down Expand Up @@ -235,24 +234,16 @@ BEGIN
END LOOP;

-- ----
-- Finally we need to change the sequence itself to settings that
-- prevent pg_catalog.nextval() from working. We do this by setting
-- the sequence's MAXVAL to its current last_value + 1, then invoke
-- our own nextval() function to bump it.
-- Finally bump the sequence past its current last_value by
-- invoking snowflake.nextval(). The MAXVALUE is left at the
-- full bigint range (2^63-1) so subsequent snowflake IDs - and
-- the pg_catalog.setval() calls that pg_dump emits during a
-- restore - all stay within bounds.
-- ----
v_cmd = 'SELECT last_value FROM ' ||
pg_catalog.quote_ident(N.nspname) || '.' ||
pg_catalog.quote_ident(C.relname)
FROM pg_catalog.pg_class C
JOIN pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE C.oid = p_seqid;
EXECUTE v_cmd INTO v_last_value;

v_cmd = 'ALTER SEQUENCE ' ||
pg_catalog.quote_ident(N.nspname) || '.' ||
pg_catalog.quote_ident(C.relname) ||
' NO CYCLE MAXVALUE ' ||
v_last_value + 1
' AS bigint NO CYCLE MAXVALUE 9223372036854775807'
FROM pg_catalog.pg_class C
JOIN pg_catalog.pg_namespace N ON N.oid = C.relnamespace
WHERE C.oid = p_seqid;
Expand Down
Loading
Loading