inputs for pg_get_id() function?

From: Steve Lefevre <lefevre(dot)10(at)osu(dot)edu>
To: pgsql-novice(at)postgresql(dot)org
Subject: inputs for pg_get_id() function?
Date: 2007-06-13 02:43:42
Message-ID: 466F59DE.3070608@osu.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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;
}

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Richard Broersma Jr 2007-06-13 03:56:39 Re: inputs for pg_get_id() function?
Previous Message Tom Lane 2007-06-13 01:14:11 Re: unreasonable run time for vacuum analyze?