code: def _batch_postgres_query(table, records):
      
      if not records:
          return
  
      POSTGRESQL_MAX = 0x3FFFFFFF
  
      # Create preamble and measure its length
      preamble = (
          "INSERT INTO "
          + table.__tablename__
          + " ("
          + ", ".join(records[0].keys())
          + ") VALUES ("
          + ", ".join(["?"] * len(records[0].keys()))
          + ")\n"
      )
      start = 0
      end = 0
      total_len = len(preamble)
      while end < len(records):
          record_len = sum([len(str(v)) for v in records[end].values()])
  
          # Pre-increment to include the end element in the slice
          end += 1
  
          if total_len + record_len >= POSTGRESQL_MAX:
              logger.debug(f"Splitting query due to length ({total_len} chars).")
              yield records[start:end]
              start = end
              # Reset the total query length
              total_len = len(preamble)
          else:
              total_len += record_len
  
      yield records[start:end]
docs: Break the list into chunks that can be processed as a single statement.
  
      Postgres query cannot be too long or it will fail.
      See: https://dba.stackexchange.com/questions/131399/is-there-a-maximum-
       length-constraint-for-a-postgres-query
  
      :param records: The full list of records to batch.
      :type records: iterable
      :param table: The sqlalchemy table.
      :return: A generator of lists of records.