Skip to content

pgx.CopyFromRows does not work with tsvector column in destination table #2483

@spongenee

Description

@spongenee

Describe the bug
pgx.CopyFromRows does not work with tsvector column in destination table. No data is copied to destination table.

The error is:

CopyFrom error: ERROR: invalid size of tsvector (SQLSTATE XX000)

If I alter the tsvector column in destination table to TEXT, then Copying from table with a tsvector column works. And I can alter the column to tsvector again. so it's clearly not a size issue of the source data.

To Reproduce
Steps to reproduce the behavior:

package main

import (
	"context"
	"fmt"
	"os"
	"time"
	"github.com/jackc/pgx/v5"
)

func main() {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()

	dsn := os.Getenv("DATABASE_URL")
	if dsn == "" {
		fmt.Fprintln(os.Stderr, "Please set DATABASE_URL, e.g.: export DATABASE_URL=\"postgres://user:pass@host:5432/dbname?sslmode=disable\"")
		os.Exit(1)
	}

	conn, err := pgx.Connect(ctx, dsn)
	if err != nil {
		fmt.Fprintf(os.Stderr, "connect error: %v\n", err)
		os.Exit(1)
	}
	defer conn.Close(ctx)

	_, err = conn.Exec(ctx, `CREATE TEMP TABLE tmp_tsv (id int, t tsvector)`)
	if err != nil {
		fmt.Fprintf(os.Stderr, "create table error: %v\n", err)
		os.Exit(1)
	}

	rows := [][]any{
		{1, "'3c3':4B 'annabel':1A 'm6k':3B 's':2A 'toronto':5"},
	}

	n, err := conn.CopyFrom(ctx, pgx.Identifier{"tmp_tsv"}, []string{"id", "t"}, pgx.CopyFromRows(rows))
	if err != nil {
		fmt.Fprintf(os.Stderr, "CopyFrom error: %v\n", err)
		os.Exit(1)
	}

	fmt.Printf("copy result: n=%d err=%v\n", n, err)
}

This is the test code I used, and it returns:

$ go run -race main.go
CopyFrom error: ERROR: invalid size of tsvector (SQLSTATE XX000)
exit status 1

To run it, export DATABASE_URL=<db_url> before go run -race main.go.

Expected behavior
Copy should be successful and exit with status 0.

Actual behavior
Returns error above and no data copied to destination table.

Version

  • Go: $ go version -> [e.g. go version go1.18.3 darwin/amd64]
    go version go1.25.6 linux/amd64
  • PostgreSQL: $ psql --no-psqlrc --tuples-only -c 'select version()' -> [e.g. PostgreSQL 14.4 on x86_64-apple-darwin21.5.0, compiled by Apple clang version 13.1.6 (clang-1316.0.21.2.5), 64-bit]
    PostgreSQL 15.10 on x86_64-pc-linux-gnu, compiled by x86_64-pc-linux-gnu-gcc (GCC) 9.5.0, 64-bit
  • pgx: $ grep 'github.com/jackc/pgx/v[0-9]' go.mod -> [e.g. v4.16.1]
    github.com/jackc/pgx/v5 v5.8.0

Additional context
Add any other context about the problem here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions