module ActiveRecord::ConnectionAdapters::PostgreSQL::DatabaseStatements

def set_constraints(deferred, *constraints)

See https://www.postgresql.org/docs/current/sql-set-constraints.html

Valid values are +:deferred+ or +:immediate+.
[deferred]

Not passing any specific constraint names will set the value for all deferrable constraints.

Set when constraints will be checked for the current transaction.
def set_constraints(deferred, *constraints)
  unless %i[deferred immediate].include?(deferred)
    raise ArgumentError, "deferred must be :deferred or :immediate"
  end
  constraints = if constraints.empty?
    "ALL"
  else
    constraints.map { |c| quote_table_name(c) }.join(", ")
  end
  execute("SET CONSTRAINTS #{constraints} #{deferred.to_s.upcase}")
end