The problem is not reporducible, you have to investigate more. You must share more details about your database table, your python code and server OS.
You can also share with us the strace
attached to Python, so we can see what actually happens during the query.
wait_event_type = Client: The server process is waiting for some activity on a socket from user applications, and that the server expects something to happen that is independent from its internal processes. wait_event
will identify the specific wait point.
wait_event = ClientRead: A session that waits for ClientRead
is done processing the last query and waits for the client to send the next request. The only way that such a session can block anything is if its state is idle in transaction
. All locks are held until the transaction ends, and no locks are held once the transaction finishes.
Idle in transaction: The activity can be idle
(i.e., waiting for a client command), idle in transaction
(waiting for client inside a BEGIN
block), or a command type name such as SELECT
. Also, waiting is appended if the server process is presently waiting on a lock held by another session.
The problem could be related to:
- Network problems
- Uncommitted transaction someplace that has created the same table name.
- The transaction is not committed
You pointed out that is not a commit problem because the SQL editor do the same, but in your question you specify that the editor succesfully create the table.
In pgModeler you see idle
, that means the session is idle, not the query.
If the session is idle, the "query" column of pg_stat_activity
shows the last executed statement in that session.
So this simply means all those sessions properly ended their transaction using a ROLLBACK statement.
If sessions remain in state idle in transaction
for a longer time, that is always an application bug where the application is not ending the transaction.
You can do two things:
Set the idle_in_transaction_session_timeout
so that these transactions are automatically rolled back by the server after a while. This will keep locks from being held indefinitly, but your application will receive an error.
Fix the application as shown below
.commit()
solution
The only way that I found to reproduce the problem is to omit the commit
action.
The module psycopg2
is Python DB API-compliant, so the auto-commit feature is off by default.
Whit this option set to False
you need to call conn.commit
to commit any pending transaction to the database.
Enable auto-commit
You can enable the auto-commit as follow:
import psycopg2
connection = None
try:
connection = psycopg2.connect("dbname='myDB' user='myUser' host='localhost' password='myPassword'")
connection.autocommit = True
except:
print "Connection failed."
if(connection != None):
cursor = connection.cursor()
try:
cursor.execute("""CREATE TABLE tbl AS (SELECT (row_number() over())::integer 'id', 'col' FROM tbl2)""")
except:
print("Failed to create table.")
with
statement
You can also use the with
statement to auto-commit a transaction:
with connection, connection.cursor() as cursor: # start a transaction and create a cursor
cursor.execute("""CREATE TABLE tbl AS (SELECT (row_number() over())::integer 'id', 'col' FROM tbl2)""")
Traditional way
If you don't want to auto-commit the transaction you need to do it manually calling .commit()
after your execute
.