Converting SQL Server queries to PostgreSQL I am trying to convert the following SQL Server T-SQL block to a PostgreSQL compatible one using, preferably CTE or a function. The issue with converting is I am not able to figure out how I return the last inserted id (scope_identity() in SQL Server returns the last inserted id in the identity column).
 IF (
        NOT EXISTS (
            SELECT p_id
            FROM qr_request
            WHERE q_ota = 'Done'
                            )
        )
BEGIN
    INSERT INTO qr_request (
        u_id
        ,c_id
        ,r_id
        ,q_ota
        )
    VALUES (
        1
        ,2
        ,3
        ,'P'
        )

    COMMIT

    SELECT Scope_Identity() AS id
        ,p.email AS n
    FROM person p
    WHERE p.uid = 33
        AND deleted = 0
        
END
ELSE
    SELECT NULL AS id
        ,p.email AS n
    FROM person p
    WHERE p.uid = 33
        AND deleted = 0

Any help would be appreciated.
Since PostgreSQL doesn't use the same traditional &quot;ID&quot; concept on Tables like SQL Server, rather it uses Sequences, getting the last value is a little different but there are a multitude of ways to do so as noticed in this well versed StackOverflow answer.
Specifically the functions you can leverage are CURRVAL and LASTVAL.
Relevant info from the aforementioned and linked StackOverflow answer:
CURRVAL():
INSERT INTO persons (lastname,firstname) VALUES ('Smith', 'John');
  SELECT currval('persons_id_seq');


The name of the sequence must be known. Caveat: currval() only works
after an INSERT (which has executed nextval() ), in the same session.

LASTVAL():

This is similar to the previous, only that you don't need to specify
the sequence name: it looks for the most recent modified sequence
(always inside your session, same caveat as above.)