PostgreSQL: getting the new primary key value after an insert (serial or sequence versus a GUID)

Applies to: as reported, PostgreSQL with a NetTalk web server NetWebForm. Versions are not named in the thread.

Problem

A serial or sequence column worked, except that in a NetWebForm insert the poster could not read the value of the auto-incremented column in the PostInsert embed. He needed it to write an audit log row.

Solution

Ranked by what the thread offered:

  1. Call the sequence directly (seq.nextval) when the form starts and use that value in the header and the detail rows. A column declared serial has nextval as its default, and a value you supply does not call the sequence a second time. (Suggested by one poster; the original poster did not try it.)
  2. Use a GUID or UUID as the key. The original poster switched his main tables to a 24-character string built by a GetGuid() function placed in the Initial Value of the key column in the dictionary. The value exists before the insert, so the audit log can use it. He reported that it works as is.
  3. A third poster uses both sequences and UUIDs, prefers UUIDs for portable data and found no significant performance difference.

Caveats

  • The GetGuid() author warned that it assumes the field is 24 characters (date, time, random letters and a two-digit random number); another field size needs a change to the function.
  • The thread does not say why the value was unavailable after the insert. One poster concluded it was on the NetTalk side.

Today (2026)

Not re-tested. Fetching the key before the insert needs no feature of the driver, so it likely still applies. Current PostgreSQL identity columns were not discussed in the thread.

Sources: Johan Z. (Nov 2014, the question), Andre L., JohanR, Jon W., Joe T. (Nov 2014) in softvelocity.clarion.databasedrivers.