PostgreSQL how to get last transaction?

Hello all,

I want to get the last record in the table. What is the best way to do this?

The last record… inserted? By id? By some date?

I suppose it would be either the last inserted, or ID. Probably inserted.


Easiest way is to use the RETURNING clause upon INSERT.

var sql as string = "INSERT INTO t (a, b, c) VALUES ($1, $2, $3) RETURNING *"
var rs as RowSet = db.SelectSQL(sql, var1, var2, var3)
// rs now has the inserted record

(Not tested, just to give you an idea.)

1 Like

Thanks Kem,
So that will return the last record?


It will return the just-created record.because of the RETURNING clause. You can also choose to return some or one column, for example RETURNING id.

Thank you Kem,

It will return the just-created record… in the current session

1 Like