jackc / jackc/pgx

Scanning multi-dimensional array into `any` or `[]any` results in a flattened slice

Open
#1,968 1 comment 1 reaction 0 assignees View on GitHub
bug
Dominant language
Go
Stars
14.3k
Forks
1.1k
Avg merge
6d 9h
Merged PRs (30d)
11

Description

**Describe the bug**
When scanning a multi-dimensional postgres array into `any` or `[]any` the array will be flattened.

**To Reproduce**
Steps to reproduce the behavior:

- SELECT a multi-dimensional array
- Scan it into `any` or `[]any`
- Print

See example below:

```go
package main

import (
"context"
"log"
"os"
"fmt"

"github.com/jackc/pgx/v5"
)

func main() {
conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
log.Fatal(err)
}
defer conn.Close(context.Background())

var foo any
var pop []any
var zing [][]any

row := conn.QueryRow(context.Background(), "SELECT '{{1,11},{2,22}}'::bigint[], '{{3,33},{4,44}}'::bigint[], '{{5,55},{6,66}}'::bigint[]")
err := row.Scan(&foo, &pop, &zing)
if err != nil {
t.Error(err)
}

fmt.Printf("foo: %+v", foo) // The nested array is flattened (type = interface{} | []interface{} | interface{} | int64)
fmt.Printf("pop: %+v", pop) // The nested array is flattened (type = []interface{} | interface{} | int64)
fmt.Printf("zing: %+v", zing) // Only zing has the desired form (type = [][]interface{} | []interface{} | interface{} | int64)

// PRINTS:
// foo: [1 11 2 22] <- FLATTENED, NOT OK
// pop: [3 33 4 44] <- FLATTENED, NOT OK
// zing: [[5 55] [6 66]] <- GOOD
}
```

**Expected behavior**
Regardless of destination type (any, []any or [][]any) the array should NOT be flattened

**Actual behavior**
When scanning into any or []any the array is flattened

**Version**
- Go: 1.22
- PostgreSQL: PostgreSQL 16.1 on x86_64-pc-linux-musl, compiled by gcc (Alpine 13.2.1_git20231014) 13.2.1 20231014, 64-bit
- pgx: v5.5.5

Contributor guide

Open the contributing guide

Research direction

No repository file or test is named. Start by running the provided Go reproduction against PostgreSQL and compare scanning into any, []any, and [][]any. Done means multi-dimensional arrays retain their nested shape for all three destination types, with coverage for the reported behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.