Reputation: 2343
As title say, it is possible to issue a query on psql with a "begin", query, and "commit".
What I want to know is what happens if I don't use a "begin" command?
Upvotes: 0
Views: 296
Reputation: 939
If you don't use BEGIN/COMMIT
, it's the same as wrapping each individual query in a BEGIN/COMMIT
block. You can use BEGIN/COMMIT
to group multiple queries into a single transaction. A few reasons you might want to do so include
UPDATE
s to clobber the other.Postgres gives you the first two of these by default. The last one would require a higher transaction isolation level, and makes your query run the risk of raising a serialization error. Transaction isolation levels are a fairly complicated topic, so if you want more info on them the best place to go is the documentation.
Upvotes: 0
Reputation: 6473
Some database engine will allow you to execute modifications (INSERT, UPDATE, DELETE
) without an open transaction. It's basically assumed that you have an instant BEGIN / COMMIT
around each of your instructions, which is a bad practice in case something goes wrong in a batch of many instructions.
You can still make a SELECT
, but no INSERT, UPDATE, DELETE
without a BEGIN
to enforces the good practice. That way, if something goes wrong, a ROLLBACK
is instantly executed, canceling all your modifications as if they never existed.
Using a transaction around a batch of various SELECT
will guarantee that the data you get for each SELECT
matches the same version of the database at the instant you open the transaction depending on your ISOLATION
level.
Please read this for more information :
http://www.postgresql.org/docs/9.5/static/sql-start-transaction.html
and
http://www.postgresql.org/docs/9.5/static/tutorial-transactions.html
Upvotes: 1