Various fixes for bugs found in use of psycopg2.