Python Forum
How psycopg2 autocommit works ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How psycopg2 autocommit works ?
#1
Hi,

I'm working on a "simple" script that makes use of psycopg2.
The script takes some time to finish (not the problem), while I was trying to do some optimizations, I discovered that my queries were marked as "idle" in the pg_stat_activity table.

SELECT
    query,state, state_change
   FROM pg_stat_activity;
+------------------------------+---------+
| query                        | state   |
|------------------------------+---------+
| select * from test           | idle    | 
After a quick search, I found the following on the psycopg website:

Quote:If you are writing a long-living program, you should probably make sure to call one of the transaction closing methods before leaving the connection unused for a long time (which may also be a few seconds, depending on the concurrency level in your database). Alternatively you can use a connection in autocommit mode to avoid a new transaction to be started at the first command.

From this, I understand that I can use connection.commit() after each call of the execute() method (that's works fine BTW) or set the connection in autocommit mode. I'm having hard time with the last option.

I wrote a small script to reproduce my problem. It connects to a db, make a simple query and wait for
10s, so I can check the state of the query in pg_stat_activity.

import psycopg2
from time import sleep

con = psycopg2.connect('dbname=test user=test password=test')
#con.autocommit = True
cur = con.cursor()
cur.execute('select * from test')
con.commit()

sleep(10)
print('DONE')
When running with autocommit=False and manual commit, the logs look like this:

LOG:  statement: BEGIN
LOG:  statement: select * from test
LOG:  statement: COMMIT
The query is executed inside a transaction as it should be and the query does not show an idle state.

import psycopg2
from time import sleep

con = psycopg2.connect('dbname=test user=test password=test')
con.autocommit = True
cur = con.cursor()
cur.execute('select * from test')
#con.commit()

sleep(10)
print('DONE')
When running with autocommit=True, , the logs look like this:

LOG:  statement: select * from test
No transaction as expected, but now the query is marked as idle.

To me, this does not match with the psycopg website. Is this how autocommit is supposed to work ? Am I missing something here ?

I know I can force commit or use a context manager, but I'm trying to learn about the autocommit mode.


Regards
Reply
#2
selects only require commit in very rare cases, like joining database links, where a transaction will be created.
Almost never need a transaction on select, and I would expect that auto-commit will not.
Reply
#3
(Oct-07-2021, 11:32 PM)Larz60+ Wrote: selects only require commit in very rare cases, like joining database links, where a transaction will be created.
Almost never need a transaction on select, and I would expect that auto-commit will not.

Thank you for your answer.

I'm not sure to understand what you mean by "joining database links". A query using joins ?
If so, I made a quick test with joins and also with INSERT/UPDATE/DELETE.
And the results are the same : no DB transaction (which is OK) but queries marked idle.
Reply
#4
Hmm,

then I wonder if it's because of explicit locking which PostgreSQL allows, doc: https://www.postgresql.org/docs/9.1/expl...cking.html
Perhaps ask the authors, whose email address can be found here: https://pypi.org/project/psycopg2/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Psycopg2 doesn't work with python2 MedianykEugene 3 2,943 Aug-10-2021, 07:00 AM
Last Post: ndc85430
  PostgreSQL psycopg2.errors.DuplicateColumn: column specified more than once rajnish_nationfirst 2 3,770 Jun-21-2020, 08:17 AM
Last Post: ibreeden
  tornado psycopg2 Nikosznb 1 2,313 Feb-23-2020, 10:49 PM
Last Post: scidam
  Frequency and timing of psycopg2 commits acecase 0 2,077 Nov-01-2019, 05:50 PM
Last Post: acecase
  Create table with psycopg2 on postgreSQL DB yhecohen 2 3,318 Aug-23-2019, 05:56 AM
Last Post: massimo_m
  psycopg2 insert error Wonder_women 0 2,664 Jun-10-2019, 11:56 AM
Last Post: Wonder_women
  Is there a standard for autocommit In PEP 249 zatlas1 10 5,210 Feb-06-2019, 04:56 PM
Last Post: buran
  import psycopg2 issue bhuvneshdogra 1 2,982 Dec-27-2018, 04:03 PM
Last Post: Larz60+
  Need help on a psycopg2 question badfish 1 5,477 Nov-28-2018, 10:16 PM
Last Post: badfish
  Error when Inserting CSV file values into a postgreSQL using psycopg2 klllmmm 8 13,115 Oct-07-2016, 06:48 PM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020