Skip to content

Instantly share code, notes, and snippets.

@aalvesjr
Created June 22, 2018 17:55
Show Gist options
  • Save aalvesjr/8795cc301347c0d4feccb9568782f278 to your computer and use it in GitHub Desktop.
Save aalvesjr/8795cc301347c0d4feccb9568782f278 to your computer and use it in GitHub Desktop.
Using transaction on Postgresql
psql (9.6.1, server 9.5.10)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

auth_svc=> select id, deleted_at from users order by id desc limit 2;
   id    | deleted_at
---------+------------
 1910482 |
 1910481 |
(2 rows)

auth_svc=> start transaction;
START TRANSACTION
auth_svc=> update users set deleted_at=NOW() where id=1910482;
UPDATE 1
auth_svc=> rollback;
ROLLBACK
auth_svc=> select id, deleted_at from users order by id desc limit 2;
   id    | deleted_at
---------+------------
 1910482 |
 1910481 |
(2 rows)

auth_svc=> start transaction;
START TRANSACTION
auth_svc=> update users set deleted_at=NOW() where id=1910482;
UPDATE 1
auth_svc=> select id, deleted_at from users order by id desc limit 2;
   id    |          deleted_at
---------+-------------------------------
 1910482 | 2018-06-22 17:54:04.841544+00
 1910481 |
(2 rows)

auth_svc=> commit;
COMMIT
auth_svc=> select id, deleted_at from users order by id desc limit 2;
   id    |          deleted_at
---------+-------------------------------
 1910482 | 2018-06-22 17:54:04.841544+00
 1910481 |
(2 rows)

postgresql doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment