This article is half-done without your Comment! *** Please share your thoughts via Comment ***
PostgreSQL 10 introduced many useful features that we can use in day to day work.
Now, we can trace the status of the running transaction in PostgreSQL 10. If we have transaction IDs of our sessions and suddenly database crashed or shutdown, we can check the status of those transactions.
We can check the status like Committed, In Progress, Rollback or Aborted. This facility will help a lot for taking a necessary decision on crashed transactions.
PostgreSQL 10 introduced two functions:
txid_current() – will return the current transaction id of a session
txid_status() – will return the status of transaction
Please check the below sample:
Create a sample table:
1 2 |
CREATE TABLE tbl_stud (Rno INT, Name Character Varying); |
Open a transaction, and insert a record:
1 2 3 4 |
BEGIN TRANSACTION; INSERT INTO tbl_stud VALUES (1,'Anvesh'); |
Check the current transaction id:
1 2 3 4 5 |
SELECT *FROM txid_current(); txid_current ------------ 587 |
Check the status of above transaction id:
It is showing “in progress” because I didn’t commit the transaction yet.
1 2 3 4 5 |
SELECT txid_status(BIGINT '587'); txid_status ----------- in progress |
Now, execute the COMMIT:
1 |
COMMIT TRANSACTION; |
Now, check the status again:
1 2 3 4 5 |
SELECT txid_status(BIGINT '587'); txid_status ----------- committed |
You can also return transaction id, with the insert statement: (Using RETURNING)
1 2 3 4 5 |
SELECT txid_status(BIGINT '587'); txid_status ----------- committed |
Leave a Reply