def update_sequence(session, table_name, sequence_name=None):
    """
    On insert with given id, PostgreSQL does not update the sequence 
    for autoincrement. Thus tables with defaults cannot use autoincremented
    PK anymore. Thus, the sequence is set to the current value
    """
    if sequence_name is None:
        sequence_name = '%s_id_seq' % table_name
    sql = "SELECT setval('%s', (SELECT MAX(id) from %s), true);" % (
        sequence_name, table_name)
    res = session.execute(sql)
    return res.scalar()
