title: Running psycopg2 COPY_FROM for an empty table for multiple datasets in a loop will copy only the last entry
body: <p>I have multiple datasets with no conflicting primary keys or constraints, for which I need to run copy_from in a loop for every dataset to add them in the same table (the table is empty). Only the last dataset from the loop will be inserted.<br />
  This can be reproduced only if the table for witch is run is empty. If the table has even a single row, all the datasets will be inserted normally.<br />
  Note: For my use-case, I need to run multiple copy_from on the same table because I need to process the datasets independently on different subprocesses, running a single copy_from for all the merged datasets won't be a solution due to design considerations.
  Postgres log is not showing anything. Also, the order of datasets in the loop does not matter. No matter the order, always only the last dataset in the loop will be inserted into the table.</p>
  <pre><code>for df, client_id in clients_data: #iteration trough a map of DataFrame objects and client ids
      with conn:
            with conn.cursor() as cursor:
                for chunk in df:
                    df = pipeline.clean_df(chunk, cursor, client_id) #doing some preprocessing irrelevant to the problem
                    copy_from_df(df, 'mytable', cursor, chunksize=50000)
        conn.close()
          
  def copy_from_df(df: DataFrame,
                   table: str,
                   cursor,
                   chunk_size: int = __chunksize
                   ):
      try:
          for i in range(0, df.shape[0], chunk_size):
              buffer = StringIO()
              chunk = df.iloc[i:(i + chunk_size)]
              chunk.to_csv(buffer, index=False, header=False, sep='\t', na_rep='\\N', quoting=None)
              buffer.seek(0)
              cursor.execute(f'SET search_path TO {SCHEMA}')
              cursor.copy_from(buffer, table, columns=list(df.columns))
      except psycopg2.Error as e:
          logger.error('COPY FROM Error: ' + str(e))
  </code></pre>
  <ul>
  <li>OS: MacOS 11.6 Running psycopg2 from Docker 3.6.0</li>
  <li>Psycopg2: 2.9.2</li>
  <li>Python version: 3.9</li>
  <li>PostgreSQL version: 14.0-1.pgdg110+1</li>
  </ul>