I've looked on the internet and browse a few articles how differing people approach this issue, but I'm wondering exactly what the standard method of fixing it's as well as, which works perfect for my situation.

I've an AJAX page that produces a brand new question and I have to understand how to retrieve the ID in the place query inside the same php file, around the next line.

It appears something similar to this:

$r = pg_query("INSERT INTO questions (audit_id, type_id, order) VALUES (1,1,1)");
// Fetch ID from $r here...

I've come across the mysql_insert_id() function for MySQL and heard that pg_last_oid() is comparable for PostgreSQL, however the documentation claims that it's deprecated and will also be removed soon. I've also seen using CURRVAL('my_sequence_table.id') but I am unsure if this works using the AJAX since that may raise a race condition.

Can somebody please let me know the standard PHP/PostgreSQL method to solve this issue? I'd greatly appreciate any comments.

P.S. I miss Ruby on Rails!

Most likely your best choice is by using INSERT INTO questions ... VALUES (1,1,1) RETURNING audit_id, as that provides you with the best value whether you plug the worthiness by hand or via a sequence.

Observe that the currval() trick will work when you get exactly the same session -- currval() is certain to return exactly the same value the sequence shipped for your session, no matter the other concurrent periods do. It might only result in a problem for those who have an association pooler that in some way utilizes a different connection for that first query compared to second query, but it might be quite damaged a pooler whether it did that. I understand of no pooler that does stuff that way.

Update: Begin to see the pg_get_serial_sequence() function, that takes a table and column title and returns the connected sequence title. It's more practical to make use of than hardcoding the succession title inside your code.

MySQL has got the perception of autoincrement fields, PostgreSQL has got the perception of sequences. A Postgres sequence is really a named database object whose value could be elevated. Please visit this FAQ.

I would suggest while using CURRVAL('my_sequence_table.id') option.

It should not result in a race condition for you personally because it will return the most recent value for that current session which is going to be two consecutive claims within the same session.