Q: cleaning a Postgres table of bad rows I have inherited a Postgres database, and am currently in the process of cleaning it. I have created an algorithm to find the rows where the data is bad. The algorithm is encoded into the function called checkProblems(). Using this, I am able to select the rows that contains the bad rows, as shown below ...
schema = findTables(dbName)

conn = psycopg2.connect("dbname='%s' user='postgres' host='localhost'"%dbName)
cur  = conn.cursor()

results = []
for t in tqdm(sorted(schema.keys())):

    n = 0
    cur.execute('select * from %s'%t)
    for i, cs in enumerate(tqdm(cur)):
        if checkProblem(cs):
            n += 1

    results.append({
        'tableName': t,
        'totalRows': i+1,
        'badRows'  : n,
    })

cur.close()
conn.close()

print pd.DataFrame(results)[['tableName', 'badRows', 'totalRows']]

Now, I need to delete the rows that are bad. I have two different ways of doing it. First, I can write the clean rows in a temporary table, and rename the table. I think that this option is too memory-intensive. It would be much better if I would be able to just delete the specific record at the cursor. Is this even an option? 
Otherwise, what is the best way of deleting a record under such circumstances? I am guessing that this should be a relatively common thing that database administrators do ...

A: Of course that delete the specific record at the cursor is better. You can do something like:
for i, cs in enumerate(tqdm(cur)):
    if checkProblem(cs):
        # if cs is a tuple with cs[0] being the record id.
        cur.execute('delete from %s where id=%d'%(t, cs[0]))

Or you can store the ids of the bad records and then do something like
DELETE FROM table WHERE id IN (id1,id2,id3,id4)
