jackc / jackc/pgx

rows.scan stops when encountering a null value

Open
#2,149 1 comment 0 reactions 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**
The program stops execution without an error message when executin rows.scan which contains results with a null value.
I was able to work around by using coalesce in the sql statement.

*To Reproduce**
I created a table with three columns
column_name | data_type | character_maximum_length
-------------+-------------------+--------------------------
user_id | integer |
first | character varying | 15
last | character varying | 25

sql:
select column_name, data_type, character_maximum_length from information_schema.columns where table_name = 'person';

when using pgx.Query to execute the same query, the execution stops when scanning the resulting rows:

**If possible, please provide runnable example such as:**

```go
package main

import (
"context"
"log"
"os"

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

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

var ColNam, DTyp string
var MaxChars int

tbl := "person"
query := "select column_name, data_type, character_maximum_length from information_schema.columns where table_name=$1;"

rows, err := dbcon.Query(ctx, query, tbl)
if err != nil {
fmt.Printf("error -- query failed: %v\n", err)
os.Exit(1)
}
fmt.Printf("query success\n%v\n", rows)

count :=0
for rows.Next() {
fmt.Printf("scanning row[%d]\n", count)
count++
err = rows.Scan(&ColNam, &DTyp, &MaxChars)
if err != nil {
fmt.Errorf("error row[%d] -- unable to scan row: %w", count, err)
os.Exit(1)
}
fmt.Printf("row[%d]: %-15s %-20s %d\n", count, ColNam, DTyp, MaxChars)
}

}
```

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

**Expected behavior**
I expected a code completion wiyht a nil for the DTyp. Alertnatively a return with an error message.

**Actual behavior**
The program ends in the middle of the code witout returning an error message.

**Version**
- Go: `$ go version` -> [e.g. go version go1.18.3 darwin/amd64] 1.22.5
- 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] postgres 16.04 ubuntu23.4
- pgx: `$ grep 'github.com/jackc/pgx/v[0-9]' go.mod` -> [e.g. v4.16.1]
v5.7.1
**Additional context**
Add any other context about the problem here.

Contributor guide

Open the contributing guide

Research direction

Start with the provided pgx.Query and rows.Scan reproduction, focusing on the character_maximum_length result that can be NULL. Run the example with the race detector as requested and verify that scanning the NULL value either completes with the expected nil representation or returns an error instead of stopping silently.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, postgresql
Domain
databases
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.