diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index 82fba48d5f..bd17d247e2 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -17625,6 +17625,11 @@ $.* ? (@ like_regex "^\\d+$") command. + +SELECT nextval('myseq'::regclass); -- returns 1 + + + This function requires USAGE or UPDATE privilege on the sequence. @@ -17657,11 +17662,11 @@ $.* ? (@ like_regex "^\\d+$") Furthermore, the value reported by currval is not changed in this case. For example, -SELECT setval('myseq', 42); Next nextval will return 43 -SELECT setval('myseq', 42, true); Same as above -SELECT setval('myseq', 42, false); Next nextval will return 42 +SELECT setval('myseq', 42); -- The next nextval('myseq') will return 43 +SELECT setval('myseq', 42, true); -- Same as above +SELECT setval('myseq', 42, false); -- The next nextval('myseq') will return 42 - The result returned by setval is just the value of its + The result returned by setval is the value of its second argument. @@ -17686,6 +17691,9 @@ SELECT setval('myseq', 42, false); Next nextvalnextval since the current session did. + +SELECT currval('myseq'::regclass); + This function requires USAGE @@ -17707,15 +17715,36 @@ SELECT setval('myseq', 42, false); Next nextvalcurrval, except that instead of taking the sequence name as an argument it refers to whichever sequence nextval was most recently applied to - in the current session. It is an error to call - lastval if nextval - has not yet been called in the current session. + in the current session. (An error is reported if nextval has + never been called in this session.) + +SELECT lastval(); + This function requires USAGE or SELECT privilege on the last used sequence. + + + Examples + +CREATE TABLE seq_example ( id bigint PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY ); +-- implicit sequence named seq_example_id_seq is created (\ds in psql will show sequences) + +INSERT INTO seq_example RETURNING id; -- 1 +SELECT currval('seq_example_id_seq'::regclass); -- 1 + +SELECT setval('seq_example_id_seq', 10); +INSERT INTO seq_example (id) + VALUES (nextval('seq_example_id_seq')) -- works as-is due to choosing BY DEFAULT for the identity + RETURNING id; -- 11 + +SELECT lastval(); -- 11 + + +