docs: def _get_upsert_sql(queryset, model_objs, unique_fields, update_fields, returning,
                      ignore_duplicate_updates=True, return_untouched=False):
      """
      Generates the postgres specific sql necessary to perform an upsert (ON CONFLICT)
      INSERT INTO table_name (field1, field2)
      VALUES (1, 'two')
      ON CONFLICT (unique_field) DO UPDATE SET field2 = EXCLUDED.field2;
      """
      model = queryset.model
  
      # Use all fields except pk unless the uniqueness constraint is the pk field
      all_fields = [
          field for field in model._meta.fields
          if field.column != model._meta.pk.name or not field.auto_created
      ]
  
      all_field_names = [field.column for field in all_fields]
      returning = returning if returning is not True else [f.column for f in model._meta.fields]
      all_field_names_sql = ', '.join([_quote(field) for field in all_field_names])
  
      # Convert field names to db column names
      unique_fields = [
          model._meta.get_field(unique_field)
          for unique_field in unique_fields
      ]
      update_fields = [
          model._meta.get_field(update_field)
          for update_field in update_fields
      ]
  
      unique_field_names_sql = ', '.join([
          _quote(field.column) for field in unique_fields
      ])
      update_fields_sql = ', '.join([
          '{0} = EXCLUDED.{0}'.format(_quote(field.column))
          for field in update_fields
      ])
  
      row_values, sql_args = _get_values_for_rows(model_objs, all_fields)
  
      return_sql = 'RETURNING ' + _get_return_fields_sql(returning, return_status=True) if returning else ''
      ignore_duplicates_sql = ''
      if ignore_duplicate_updates:
          ignore_duplicates_sql = (
              ' WHERE ({update_fields_sql}) IS DISTINCT FROM ({excluded_update_fields_sql}) '
          ).format(
              update_fields_sql=', '.join(
                  '{0}.{1}'.format(model._meta.db_table, _quote(field.column))
                  for field in update_fields
              ),
              excluded_update_fields_sql=', '.join(
                  'EXCLUDED.' + _quote(field.column)
                  for field in update_fields
              )
          )
  
      on_conflict = (
          'DO UPDATE SET {0} {1}'.format(update_fields_sql, ignore_duplicates_sql) if update_fields else 'DO NOTHING'
      )
  
      if return_untouched:
          row_values_sql = ', '.join([
              '(\'{0}\', {1})'.format(i, row_value[1:-1])
              for i, row_value in enumerate(row_values)
          ])
          sql = (
              ' WITH input_rows("temp_id_", {all_field_names_sql}) AS ('
              '     VALUES {row_values_sql}'
              ' ), ins AS ( '
              '     INSERT INTO {table_name} ({all_field_names_sql})'
              '     SELECT {all_field_names_sql} FROM input_rows ORDER BY temp_id_'
              '     ON CONFLICT ({unique_field_names_sql}) {on_conflict} {return_sql}'
              ' )'
              ' SELECT DISTINCT ON ({table_pk_name}) * FROM ('
              '     SELECT status_, {return_fields_sql}'
              '     FROM   ins'
              '     UNION  ALL'
              '     SELECT \'n\' AS status_, {aliased_return_fields_sql}'
              '     FROM input_rows'
              '     JOIN {table_name} c USING ({unique_field_names_sql})'
              ' ) as results'
              ' ORDER BY results."{table_pk_name}", CASE WHEN(status_ = \'n\') THEN 1 ELSE 0 END;'
          ).format(
              all_field_names_sql=all_field_names_sql,
              row_values_sql=row_values_sql,
              table_name=model._meta.db_table,
              unique_field_names_sql=unique_field_names_sql,
              on_conflict=on_conflict,
              return_sql=return_sql,
              table_pk_name=model._meta.pk.name,
              return_fields_sql=_get_return_fields_sql(returning),
              aliased_return_fields_sql=_get_return_fields_sql(returning, alias='c')
          )
      else:
          row_values_sql = ', '.join(row_values)
          sql = (
              ' INSERT INTO {table_name} ({all_field_names_sql})'
              ' VALUES {row_values_sql}'
              ' ON CONFLICT ({unique_field_names_sql}) {on_conflict} {return_sql}'
          ).format(
              table_name=model._meta.db_table,
              all_field_names_sql=all_field_names_sql,
              row_values_sql=row_values_sql,
              unique_field_names_sql=unique_field_names_sql,
              on_conflict=on_conflict,
              return_sql=return_sql
          )
  
      return sql, sql_args