jackc/pgx

rows.scan stops when encountering a null value

prr123 opened this issue · 1 comments

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:

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.
jackc commented

You need to check rows.Err() after the rows.Next() loop completes.