inputs for pg_get_id() function? - Mailing list pgsql-novice

From Steve Lefevre
Subject inputs for pg_get_id() function?
Date
Msg-id 466F59DE.3070608@osu.edu
Whole thread Raw
Responses Re: inputs for pg_get_id() function?
List pgsql-novice
Hello all --

I'm migrating from the MySQL world to Postgres. I'm used to having the
mysql_insert_id function to get the id of a row I just inserted. Several
people on the list turned me on to using nextval or currval to get an id
*before* I do an insert, to avoid race conditions .

Now I'm working on a PHP function that I'm calling pg_get_id(). When I
first wrote it, my inputs were $table_name and $column_name, but I
quickly found out that I have to reference the name of the columns'
sequence, not the column itself, to get the id. Now it's working.

But, I'm wondering it I can even get rid of the $sequence_name input. If
I assume that a user will only call the function to get an number for a
primary key column that is a sequence, is there a query I can do to find
out the primary key of a table, and the name of it's sequence? In
psuedo-code:

function pg_get_id ( $table ) {

   "SELECT primary_key_column_name FROM $table"

   "SELECT column_sequence_name FROM $table "

   "SELECT nextval('column_sequence_name') FROM $table"

   return $nextval;
}



pgsql-novice by date:

Previous
From: Tom Lane
Date:
Subject: Re: unreasonable run time for vacuum analyze?
Next
From: Richard Broersma Jr
Date:
Subject: Re: inputs for pg_get_id() function?