I am interested in the implementation details of sequences in PostgreSQL, the reason being that they're used in the background for the SERIAL
type. I have reviewed the source file here (found in Google search: "postgresql source code sequence"), although it is not clear to which version of PG this file relates to. I understand the general code flow in the function nextval_internal
, which is the heart of the issue, but PG's code base is large and I just can't afford the time to research this issue to its fullest.
I'm interested to know:
- What is the persistence policy with regard to sequences? How are they loaded when the PG server starts, and how are they persisted during the server's lifetime to prevent mishaps after a server crash/power failure?
- What are the runtime costs associated with using sequences? Do they always incur some kind of disk I/O, or never at all?
- What are the deficiencies in using sequences which I should attend to when defining tables and writing application code?