def get_indexes(cursor, table_name):
    """
    Returns a dictionary of fieldname -> infodict for the given table,
    where each infodict is in the format:
        {'primary_key': boolean representing whether it's the primary key,
         'unique': boolean representing whether it's a unique index}
    """
    # This query retrieves each index on the given table, including the
    # first associated field name
    # "We were in the nick of time; you were in great peril!"
    sql = """
WITH primarycols AS (
 SELECT user_cons_columns.table_name, user_cons_columns.column_name, 1 AS PRIMARYCOL
 FROM   user_cons_columns, user_constraints
 WHERE  user_cons_columns.constraint_name = user_constraints.constraint_name AND
        user_constraints.constraint_type = 'P' AND
        user_cons_columns.table_name = %s),
 uniquecols AS (
 SELECT user_ind_columns.table_name, user_ind_columns.column_name, 1 AS UNIQUECOL
 FROM   user_indexes, user_ind_columns
 WHERE  uniqueness = 'UNIQUE' AND
        user_indexes.index_name = user_ind_columns.index_name AND
        user_ind_columns.table_name = %s)
SELECT allcols.column_name, primarycols.primarycol, uniquecols.UNIQUECOL
FROM   (SELECT column_name FROM primarycols UNION SELECT column_name FROM
uniquecols) allcols,
      primarycols, uniquecols
WHERE  allcols.column_name = primarycols.column_name (+) AND
      allcols.column_name = uniquecols.column_name (+)
    """
    cursor.execute(sql, [table_name, table_name])
    indexes = {}
    for row in cursor.fetchall():
        # row[1] (idx.indkey) is stored in the DB as an array. It comes out as
        # a string of space-separated integers. This designates the field
        # indexes (1-based) of the fields that have indexes on the table.
        # Here, we skip any indexes across multiple fields.
        indexes[row[0]] = {'primary_key': row[1], 'unique': row[2]}
    return indexes