I have python 3.7.3
, psycopg2 version '2.7.6.1 (dt dec pq3 ext lo64)'
, and PostGreSQL version 10.10 (Ubuntu 10.10-0ubuntu0.18.04.1)
installed.
The below query works from the terminal and does indeed delete all duplicate rows by column: id
in table: mydbschema.table1
.
DELETE FROM mydbschema.table1 a USING( SELECT MIN(ctid) as ctid, id FROM mydbschema.table1 GROUP BY id HAVING COUNT(*) > 1) b WHERE a.id = b.id AND a.ctid <> b.ctid;
However, when I want to run it through a python connection and cursor, the deletion does not happen.
import psycopg2
connection = psycopg2.connect(user=conn_details['user'],
password=conn_details['password'],
host=conn_details['host'],
port=conn_details['port'],
database=conn_details['database'])
cursor = connection.cursor()
cursor.execute("DELETE FROM mydbschema.table1 a USING( SELECT MIN(ctid) as ctid, id FROM mydbschema.table1 GROUP BY id HAVING COUNT(*) > 1) b WHERE a.id = b.id AND a.ctid <> b.ctid;")
cursor.close()
I know the problem is not due to the connection
, as the below subsection of the query works completely fine:
cursor.execute("SELECT MIN(ctid) as ctid, id FROM mydbschema.table1 GROUP BY id HAVING COUNT(*) > 1;")
cursor.fetchall()
Out[3]: [('(0,3)', 3), ('(0,4)', 4), ('(0,2)', 2)]
Am I missing something? Is cursor.execute()
not suitable for this type of deletion command?