def upsert_keys(session, key_table, keys):
<EOL>if not keys:<EOL><INDENT>return<EOL><DEDENT>for key_batch in _batch_postgres_query(<EOL>key_table, [{"<STR_LIT:name>": k[<NUM_LIT:0>], "<STR_LIT>": k[<NUM_LIT:1>]} for k in keys.items()]<EOL>):<EOL><INDENT>stmt = insert(key_table.__table__)<EOL>stmt = stmt.on_conflict_do_update(<EOL>constraint=key_table.__table__.primary_key,<EOL>set_={<EOL>"<STR_LIT:name>": stmt.excluded.get("<STR_LIT:name>"),<EOL>"<STR_LIT>": stmt.excluded.get("<STR_LIT>"),<EOL>},<EOL>)<EOL>while True:<EOL><INDENT>try:<EOL><INDENT>session.execute(stmt, key_batch)<EOL>session.commit()<EOL>break<EOL><DEDENT>except Exception as e:<EOL><INDENT>logger.debug(e)<EOL><DEDENT><DEDENT><DEDENT>
Bulk add annotation keys to the specified table.

    :param key_table: The sqlalchemy class to insert into.
    :param keys: A map of {name: [candidate_classes]}.