Skip to content

Instantly share code, notes, and snippets.

@xivSolutions
Created February 28, 2014 23:05
Show Gist options
  • Save xivSolutions/9281856 to your computer and use it in GitHub Desktop.
Save xivSolutions/9281856 to your computer and use it in GitHub Desktop.
currval() Issue in PG
-- A table, "turtles":
CREATE TABLE turtles
(
id serial NOT NULL,
name text,
lastid integer,
CONSTRAINT pk_turtles PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE turtles
OWNER TO john;
-- INSERT, say, 2103 records.
-- then take curval:
SELECT currval('turtles_id_seq') as newId;
-- returns 2103. Then clear the table:
DELETE FROM turtles
-- Close the session. Come back tomorrow, try curval:
SELECT currval('turtles_id_seq') as newId;
-- Get this:
ERROR: currval of sequence "turtles_id_seq" is not yet defined in this session
SQL state: 55000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment