ankane/pgsync · error · Error

Primary key required to sync specific rows

Error message

Primary key required to sync specific rows

What it means

Error "Primary key required to sync specific rows" thrown in ankane/pgsync.

Source

Thrown at lib/pgsync/task.rb:131

          # TODO be smarter for advance sql clauses
          batch_sql_clause = " #{sql_clause.length > 0 ? "#{sql_clause} AND" : "WHERE"} #{where}"

          batch_copy_to_command = "COPY (SELECT #{copy_fields} FROM #{quoted_table}#{batch_sql_clause}) TO STDOUT"
          copy(batch_copy_to_command, dest_table: table, dest_fields: fields)

          starting_id += batch_size
          i += 1

          if opts[:sleep] && starting_id <= from_max_id
            sleep(opts[:sleep])
          end
        end
      elsif !opts[:truncate] && (opts[:overwrite] || opts[:preserve] || !sql_clause.empty?)
        if primary_key.empty?
          raise Error, "Primary key required for --overwrite" if opts[:overwrite]
          raise Error, "Primary key required for --preserve" if opts[:preserve]
          raise Error, "Primary key required to sync specific rows"
        end

        # create a temp table
        temp_table = "pgsync_#{rand(1_000_000_000)}"
        destination.execute("CREATE TEMPORARY TABLE #{quote_ident_full(temp_table)} AS TABLE #{quoted_table} WITH NO DATA")

        # load data
        copy(copy_to_command, dest_table: temp_table, dest_fields: fields)

        on_conflict = primary_key.map { |pk| quote_ident(pk) }.join(", ")
        action =
          if opts[:preserve]
            "NOTHING"
          else # overwrite or sql clause
            setter = shared_fields.reject { |f| primary_key.include?(f) }.map { |f| "#{quote_ident(f)} = EXCLUDED.#{quote_ident(f)}" }
            if setter.any?
              "UPDATE SET #{setter.join(", ")}"
            else

View on GitHub (pinned to 57bdddf5db)

When it happens

Trigger: Thrown at lib/pgsync/task.rb:131 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of ankane/pgsync@57bdddf5db (2026-08-23). Data as JSON: /api/errors/202400249008b034. Report an issue: GitHub.