views:

104

answers:

2

I'm trying to better understand the concept of 'autocommit' when working with a Postgres (psycopg) connection. Let's say I have a fresh connection, set its isolation level to ISOLATION_LEVEL_AUTOCOMMIT, then run this SQL directly, without using the cursor begin/rollback methods (as an exercise; not saying I actually want to do this):

INSERT A
INSERT B
BEGIN
    INSERT C
    INSERT D
ROLLBACK

What happens to INSERTs C & D?

Is autocommit is purely an internal setting in psycopg that affects how it issues BEGINs? In that case, the above SQL is unafected; INSERTs A & B are committed as soon as they're done, while C & D are run in a transaction and rolled back. What isolation level is that transaction run under?

Or is autocommit a real setting on the connection itself? In that case, how does it affect the handling of BEGIN? Is it ignored, or does it override the autocommit setting to actually start a transaction? What isolation level is that transaction run under?

Or am I completely off-target?

+1  A: 

Autocommit mode means that each statement implicitly begins and ends the transaction.

In your case, if autocommit is off:

  • The client will implicitly start the transaction for the first statement
  • The BEGIN will issue a warning saying that the transaction is already started
  • The ROLLBACK will roll back all four statements

When autocommit is on, only the c and d are rolled back.

Note that PostgreSQL has no internal AUTOCOMMIT behavior since 8.0: all autocommit features are relied upon the clients.

Quassnoi
I'm asking about the behavior when autocommit is on.
DNS
@DNS: Take a look at the Postgres documentation (linked to a comment on the question above). It very clearly states the behavior when autocommit is on.
ar
@ar: It doesn't; it explains what happens when you execute, i.e. an INSERT all by itself, *without* BEGIN. That's straightforward. My question asks about the behavior of a psycopg connection, set to autocommit mode, when executing something *within* a BEGIN + ROLLBACK/COMMIT block.
DNS
@Quassnoi: just noticed your edit regarding autocommit being a client feature since 8.0. I'll leave this open a bit in case other answers can add more, but that's what I was looking for.
DNS
A: 

By default, PostgreSQL has autocommit on, meaning that each statement is handled as a transaction. If you explicitly tell it to start a transaction, as in your example, those items are in a new transaction.

In your example, A and B would be committed, C and D would be rolled back.

Grant Johnson