nextval(regclass) — Advance a sequence and return its next value

Last updated: 2026-11-04

nextval(regclass) — Advance a sequence and return its next value

nextval() advances a sequence by one and returns the new value — the mechanism behind auto-incrementing identifiers in PostgreSQL. SERIAL and IDENTITY columns call it for you, and you can also use it directly for primary keys. A sequence lives independently of any table once created, so dropping the table does not drop it.

Kindfunction
Example LanguageSQL

📝 Syntax

nextval(sequence regclass) → bigint

⚙️ Parameters

sequence regclassThe sequence name, which can be given as a quoted string literal such as 'order_id_seq'. Type regclass.

Returns:bigint — the next value of the sequence. It errors if the sequence does not exist, and concurrent callers always receive distinct values.

▶ Example

Runs real PostgreSQL 18 right in your browser (PGlite WASM, nothing to install). Press ▶ Run and the result appears in the panel below.

Output:

 id | ticket_no | title
----+-----------+------------------
  1 |      1001 | Login failed
  2 |      1002 | Disk almost full
(2 rows)

📌 Key Points

More case studies coming soon.

📚 Related Reference

❓ FAQ

QShould I use nextval() or a SERIAL column?
APrefer IDENTITY for new tables: it follows the SQL standard and keeps the sequence ownership explicit. nextval() is better when several tables must share one numbering scheme.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

Content reviewed by MOTO · Found an error? Report it

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏