Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rows.scan stops when encountering a null value #2149

Open
prr123 opened this issue Oct 12, 2024 · 1 comment
Open

rows.scan stops when encountering a null value #2149

prr123 opened this issue Oct 12, 2024 · 1 comment
Labels

Comments

@prr123
Copy link

prr123 commented Oct 12, 2024

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.
@prr123 prr123 added the bug label Oct 12, 2024
@jackc
Copy link
Owner

jackc commented Oct 12, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants