Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Postgresql implicitly wrap select statements in transaction?

Tags:

postgresql

PostgreSQL actually treats every SQL statement as being executed within a transaction. If you do not issue a BEGIN command, then each individual statement has an implicit BEGIN and (if successful) COMMIT wrapped around it.

From Tutorial transactions

Does this mean that even select statement will be implicitly wrapped in transaction and there is no way to work with postgres out of transaction scope?

like image 250
whsv26 Avatar asked Sep 04 '26 21:09

whsv26


1 Answers

Does this mean that even select statement will be implicitly wrapped in transaction and there is no way to work with postgres out of transaction scope?

Yes.

Transactions aren't just about writes, but also about isolation level and what SELECTs can read. For example, at the usual isolation level, a SELECT can't read uncommitted writes. It also can't read the updates done from transactions that commit while the SELECT is running. Otherwise it wouldn't have a coherent view of the database and basically everything would break. For example, it would look up in an index, then go get the corresponding row, and the row could have changed and no longer correspond to the index.

That requires a way to take a "snapshot" of the database right before the SELECT begins to execute, and that is one important function of the transaction mechanism.

like image 167
bobflux Avatar answered Sep 06 '26 19:09

bobflux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!