Skip to content

Postgres compatibility gotcha with SERIAL columns and lastval() #14877

Open
@HonoreDB

Description

@HonoreDB

Community slack thread here has more details.

The short version is that we differ in our default implementation of the SERIAL keyword. This is usually transparent, but one way it can break libraries written for postgres is if they use lastval() to get the last value inserted into a SERIAL column. This will work by default in Postgres, but in CockroachDB this will only work if the table was created while the serial_normalization session variable was set to something other than its default value. Otherwise, you'll get the error "lastval is not yet defined in this session".

We have great documentation on this in https://www.cockroachlabs.com/docs/stable/serial.html, but googling that error message won't get you there. So maybe we should call it out and link it in our lastval documentation? I'll also look into making lastval() work with our default SERIAL implementation but that may not be possible. Edit: Someone's been here before me, clearly, as we have the SET serial_normalization=virtual_sequence option which should be second-most preferred fix after "use RETURNING id if possible".

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions