code: def _copy_trials(
      cur: psycopg.Cursor,
      suffix: str = "",
      exclude_single_searcher: bool = True,
  ) -> Generator[dict, None, None]:
      affected_rows: Dict[str, int] = {}
      table = "trials"
      trial_suffix = f"""
  JOIN experiments e ON e.id = {table}.experiment_id
  -- CROSS JOIN generate_series(1, multiplier) AS g
  WHERE e.config->'searcher'->>'name' <> 'single'
  {suffix};
      """
      if not exclude_single_searcher:
          trial_suffix = f"""
  WHERE 1 = 1
  {suffix};
          """
      with duplicate_table_rows(
          cur, table, suffix=trial_suffix
      ) as added_trials:
          affected_rows["trials"] = added_trials
          if added_trials == 0:
              yield affected_rows
              return
  
          steps_cols = (
              get_table_col_names("raw_steps") - {"id"} - {"trial_id"}
          )
          steps_cols_str = ", ".join(steps_cols)
          prefixed_steps_cols = ", ".join(
              [f"rs.{col}" for col in steps_cols]
          )
          # replicate raw_steps and update trial_id
          steps_query = f"""
  
  -- replicate raw_steps and keep the new step ids
  INSERT INTO raw_steps( {steps_cols_str}, trial_id )
  SELECT {prefixed_steps_cols}, {table}_id_map.id
  FROM raw_steps rs
  INNER JOIN {table}_id_map ON {table}_id_map.og_id = rs.trial_id
  -- WHERE {table}_id_map.og_id IS NOT NULL; -- all {table}_id_map with og_id are target trials.
  """
          cur.execute(steps_query)
          affected_rows["steps"] = cur.rowcount
  
          validations_cols = get_table_col_names("raw_validations") - {
              "id",
              "trial_id",
          }
          validations_cols_str = ", ".join(validations_cols)
          prefixed_validations_cols = ", ".join(
              [f"rv.{col}" for col in validations_cols]
          )
          validations_query = f"""
  -- replicate raw_validations and keep the new validation ids
  INSERT INTO raw_validations( {validations_cols_str}, trial_id )
  SELECT {prefixed_validations_cols}, {table}_id_map.id
  FROM raw_validations rv
  INNER JOIN {table}_id_map ON {table}_id_map.og_id = rv.trial_id
  -- WHERE {table}_id_map.og_id IS NOT NULL;
  """
  
          cur.execute(validations_query)
          affected_rows["validations"] = cur.rowcount
          yield affected_rows
  
      cur.execute("COMMIT")
  
file_path: determined/tools/scripts/replicate-metrics.py
imports: import concurrent.futures
  import contextlib
  import os
  import time
  from typing import Any, Dict, Generator, List, Set, Tuple, Union
  import psycopg
  from psycopg import sql
label_desc: Use image node_id 7 for calling a global function with example usage: _copy_trials(cur, suffix, exclude_single_searcher) without return types