pgx icon indicating copy to clipboard operation
pgx copied to clipboard

unable to encode -25518 into binary format for date (OID 1082)

Open antonkuiper opened this issue 6 months ago • 1 comments

Describe the bug a bit of context, copy of a table from Adventureworks database , in one of the tables there is a date which is before 1970-1-1 (unix time) , so this delivers a minus number and there it crashes ? to write data to postgres table.

To Reproduce Steps to reproduce the behavior:

If possible, please provide runnable example such as:


import (
	"context"
	"fmt"
	"log"
	"time"

	"github.com/apache/arrow/go/v14/arrow"
	"github.com/apache/arrow/go/v14/arrow/array"
	"github.com/apache/arrow/go/v14/arrow/memory"
	"github.com/jackc/pgx/v5"
)

func main() {
	conn, err := pgx.Connect(context.Background(), "postgres://<username>:<password:5432/<dbname>?sslmode=disable")
	if err != nil {
		log.Fatalf("connect: %v", err)
	}
	defer conn.Close(context.Background())

	// Voorbeeld datum vóór 1970
	daysSinceEpoch := int32((time.Date(1960, 1, 1, 0, 0, 0, 0, time.UTC).Unix()) / 86400)

	// Arrow array maken met 1 element: 1960-01-01
	pool := memory.NewGoAllocator()
	b := array.NewDate32Builder(pool)
	b.Append(arrow.Date32(daysSinceEpoch))
	arr := b.NewArray()
	defer arr.Release()

	rows := pgx.CopyFromRows([][]interface{}{
		{arr.(*array.Date32).Value(0)},
	})

	_, err = conn.Exec(context.Background(), `DROP TABLE IF EXISTS bug`)
	if err != nil {
		log.Fatalf("drop table: %v", err)
	}

	_, err = conn.Exec(context.Background(), `CREATE TABLE bug (before1970 DATE)`)
	if err != nil {
		log.Fatalf("create table: %v", err)
	}

	// Invoegen met pgx.CopyFrom
	_, err = conn.CopyFrom(context.Background(),
		pgx.Identifier{"bug"},
		[]string{"before1970"},
		rows,
	)
	if err != nil {
		log.Fatalf("copyfrom: %v", err)
	}

	fmt.Println("✅ Insert succeeded")
}

Please run your example with the race detector enabled. For example, go run -race main.go or go test -race.

Expected behavior the row (field) should be inserted into the target tabel (bug(before1970))

Actual behavior ERROR : unable to encode -25518 into binary format for date (OID 1082)

Version

  • Go: $ go version -> [e.g. go version go1.18.3 darwin/amd64]
  • PostgreSQL: PostgreSQL 17.5 on x86_64-pc-linux-musl, compiled by gcc (Alpine 14.2.0) 14.2.0, 64-bit
  • pgx: $ grep 'github.com/jackc/pgx/v[0-9]' go.mod -> [e.g. v4.16.1]

module bug

go 1.24.2

require ( github.com/apache/arrow/go/arrow v0.0.0-20211112161151-bc219186db40 github.com/apache/arrow/go/v14 v14.0.2 github.com/jackc/pgx/v5 v5.7.5 )

require ( github.com/goccy/go-json v0.10.2 // indirect github.com/google/flatbuffers v23.5.26+incompatible // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/klauspost/cpuid/v2 v2.2.5 // indirect github.com/zeebo/xxh3 v1.0.2 // indirect golang.org/x/crypto v0.37.0 // indirect golang.org/x/mod v0.17.0 // indirect golang.org/x/sync v0.13.0 // indirect golang.org/x/sys v0.32.0 // indirect golang.org/x/text v0.24.0 // indirect golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect )

Additional context Add any other context about the problem here.

antonkuiper avatar Jun 14 '25 09:06 antonkuiper

Can you use a normal time.Time? pgx doesn't know anything about "arrow" types.

jackc avatar Jul 12 '25 17:07 jackc