jackc / pgx

PostgreSQL driver and toolkit for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`raise exception` does not result in an error

parihaaraka opened this issue · comments

Describe the bug
raise exception does not result in an error.

To Reproduce

package main

import (
	"context"
	"flag"
	"log"
	"os"

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

func main() {
	flag.Parse()
	infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
	errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)

	cs := os.Getenv("TEST_CS") // connection string
	if cs == "" {
		cs = "user=postgres password=qwe host=127.0.0.1 port=5432 dbname=test connect_timeout=5"
	}

	ctx := context.Background()
	conn, err := pgx.Connect(ctx, cs)
	if err != nil {
		errorLog.Print(err.Error())
		return
	}
	txDB, err := conn.Begin(ctx)
	if err != nil {
		errorLog.Print(err.Error())
		return
	}
	defer txDB.Rollback(ctx)

	_, err = txDB.Exec(ctx, `
	create or replace function public.test_fn(arg text)
	returns table(id integer)
	language plpgsql security definer as
	$$
	begin
		raise exception 'hello, %', arg;
	end
	$$`)
	if err != nil {
		errorLog.Print(err.Error())
		return
	}

	rows, err := txDB.Query(ctx, `select * from public.test_fn($1)`, "world")
	if err != nil {
		errorLog.Print(err.Error())
		return
	}
	infoLog.Print("no errors acquired")
	rows.Close()
}

Expected behavior
txDB.Query must return the error

Actual behavior
INFO 2023/12/04 15:30:38 no errors acquired

Version

  • Go: go version go1.21.4 linux/amd64
  • PostgreSQL: PostgreSQL 12.17 (Ubuntu 12.17-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0, 64-bit
  • pgx: v5.5.0

See https://pkg.go.dev/github.com/jackc/pgx/v5#Conn.Query - in particular the first paragraph:

Query sends a query to the server and returns a Rows to read the results. Only errors encountered sending the query and initializing Rows will be returned. Err() on the returned Rows must be checked after the Rows is closed to determine if the query executed successfully.

i beg your pardon