code: def batch_upsert_records(session, table, records):
      
      if not records:
          return
      for record_batch in _batch_postgres_query(table, records):
          stmt = insert(table.__table__)
          stmt = stmt.on_conflict_do_update(
              constraint=table.__table__.primary_key,
              set_={
                  "keys": stmt.excluded.get("keys"),
                  "values": stmt.excluded.get("values"),
              },
          )
          session.execute(stmt, record_batch)
          session.commit()
docs: Batch upsert records into postgresql database.