#!/usr/bin/python import itertools import psycopg2 columns = ['a', 'b', 'c', 'd', 'e', 'f', 'g']; orderings = itertools.permutations(columns) conn = psycopg2.connect('host=localhost user=postgres dbname=test') cur = conn.cursor() cur.execute('set enable_incrementalsort = off') for o in orderings: if len(o) < 4: continue for r in range(1,10): cur.execute('EXPLAIN SELECT * FROM t ORDER BY ' + (','.join(o)))