psycopg2: How to execute vacuum postgresql query in python script
Question:
I am using Python with psycopg2 and I’m trying to run a full VACUUM in python script. The problem is that when I try to run the VACUUM command within my code I get the following error:
psycopg2.InternalError: VACUUM cannot run inside a transaction block
The line which am trying to execute is:
sql=”vacuum full table_name;”
cur.execute(sql)
How to resolve this error?
Answers:
Psycopg2 starts a new transaction for each call to .execute().
Open an autocommit connection to handle a vacuum.
Read about autocommit
in the documentation.
I am using Python with psycopg2 and I’m trying to run a full VACUUM in python script. The problem is that when I try to run the VACUUM command within my code I get the following error:
psycopg2.InternalError: VACUUM cannot run inside a transaction block
The line which am trying to execute is:
sql=”vacuum full table_name;”
cur.execute(sql)
How to resolve this error?
Psycopg2 starts a new transaction for each call to .execute().
Open an autocommit connection to handle a vacuum.
Read about autocommit
in the documentation.