You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: pgsync.go
    path of file: ./repos/mergestat-lite/pkg/pgsync
    
    the code of the file until where you have to start completion: package pgsync

import (
	"bytes"
	"context"
	"database/sql"
	"fmt"
	"text/template"

	"github.com/lib/pq"
	"github.com/rs/zerolog"

	_ "github.com/mattn/go-sqlite3"
	_ "github.com/mergestat/mergestat-lite/pkg/sqlite"
)

type SyncOptions struct {
	Postgres   *sql.DB
	MergeStat  *sql.DB
	SchemaName string
	TableName  string
	Query      string
	Logger     *zerolog.Logger
}

// Sync imports the results of an mergestat query into a postgres table.
// CAUTION: will overwrite (DROP!) the specified table and replace it.
func Sync(ctx context.Context, options *SyncOptions) error {
	l := options.Logger.With().Str("pgTable", options.TableName).Logger()

	select {
	default:
	case <-ctx.Done():
		return ctx.Err()
	}

	rows, err := options.MergeStat.QueryContext(ctx, options.Query)
	if err != nil {
		return err
	}
	defer rows.Close()

	colTypes, err := rows.ColumnTypes()
	if err != nil {
		return err
	}

	colNames := make([]string, len(colTypes))
	for c := 0; c < len(colTypes); c++ 
    
func Sync(ctx context.Context, options *SyncOptions) error {
	l := options.Logger.With().Str("pgTable", options.TableName).Logger()

	select {
	default:
	case <-ctx.Done():
		return ctx.Err()
	}

	rows, err := options.MergeStat.QueryContext(ctx, options.Query)
	if err != nil {
		return err
	}
	defer rows.Close()

	colTypes, err := rows.ColumnTypes()
	if err != nil {
		return err
	}

	colNames := make([]string, len(colTypes))
	for c := 0; c < len(colTypes); c++ {
		colNames[c] = colTypes[c].Name()
	}

	select {
	default:
	case <-ctx.Done():
		return ctx.Err()
	}

	tx, err := options.Postgres.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelRepeatableRead})
	if err != nil {
		return err
	}

	handleErr := func(err error) {
		if err == nil {
			return
		}
		l.Error().AnErr("error", err).Msg("error")
		if err := tx.Rollback(); err != nil {
			l.Error().AnErr("error", err).Msg("error")
		}
	}

	schemaName := options.SchemaName
	if schemaName == "" {
		schemaName = "public"
	}
	tempNameNew := fmt.Sprintf("%s_temp", options.TableName)
	tempNameDrop := fmt.Sprintf("%s_drop", options.TableName)

	// create a new temp table
	createSQL, err := createTableFromSQLiteTypes(schemaName, tempNameNew, colTypes)
	handleErr(err)

	select {
	default:
	case <-ctx.Done():
		return ctx.Err()
	}

	l.Info().Msg(createSQL)

	_, err = tx.ExecContext(ctx, createSQL)
	handleErr(err)

	select {
	default:
	case <-ctx.Done():
		return ctx.Err()
	}

	stmt, err := tx.PrepareContext(ctx, pq.CopyInSchema(schemaName, tempNameNew, colNames...))
	handleErr(err)

	for rows.Next() {
		select {
		default:
		case <-ctx.Done():
			return ctx.Err()
		}

		values := make([]interface{}, len(colTypes))
		pointers := make([]interface{}, len(colTypes))

		for i := 0; i < len(values); i++ {
			pointers[i] = &values[i]
		}

		err := rows.Scan(pointers...)
		handleErr(err)

		_, err = stmt.ExecContext(ctx, values...)
		handleErr(err)
	}

	select {
	default:
	case <-ctx.Done():
		return ctx.Err()
	}

	_, err = stmt.ExecContext(ctx)
	handleErr(err)

	err = stmt.Close()
	handleErr(err)

	select {
	default:
	case <-ctx.Done():
		return ctx.Err()
	}

	s := fmt.Sprintf(`
	ALTER TABLE IF EXISTS %s RENAME TO %s;
	ALTER TABLE IF EXISTS %s RENAME TO %s;
	DROP TABLE IF EXISTS %s;
`,
		fmt.Sprintf("%s.%s", pq.QuoteIdentifier(schemaName), pq.QuoteIdentifier(options.TableName)),
		pq.QuoteIdentifier(tempNameDrop),
		fmt.Sprintf("%s.%s", pq.QuoteIdentifier(schemaName), pq.QuoteIdentifier(tempNameNew)),
		pq.QuoteIdentifier(options.TableName),
		fmt.Sprintf("%s.%s", pq.QuoteIdentifier(schemaName), pq.QuoteIdentifier(tempNameDrop)),
	)

	l.Info().Msg(s)

	_, err = tx.ExecContext(ctx, s)
	handleErr(err)

	select {
	default:
	case <-ctx.Done():
		return ctx.Err()
	}

	err = tx.Commit()
	if err != nil {
		return err
	}

	return nil
}