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

gorm postgres v1.5.0 return panic: sql: Register called twice for driver pgx #1414

Open
fsilva1985 opened this issue Apr 19, 2023 · 7 comments
Labels

Comments

@fsilva1985
Copy link

Describe the bug

Basically, I am trying to instrument my database but it is returning an error.

To Reproduce
Steps to reproduce the behavior:

  1. Use this config '...'

go.elastic.co/apm/module/apmgormv2/v2 v2.3.0
gorm.io/driver/postgres v1.5.0 // indirect

  1. Then call '....'
  2. Then do '....'
  3. See error
    panic: sql: Register called twice for driver pgx

goroutine 1 [running]:
database/sql.Register({0x12b187f, 0x3}, {0x1519b00, 0xc00011e288})
/usr/local/go/src/database/sql/sql.go:54 +0x185
github.com/jackc/pgx/v4/stdlib.init.0()
/home/felipe/go/pkg/mod/github.com/jackc/pgx/[email protected]/stdlib/sql.go:87 +0xa7
exit status 2
make: *** [Makefile:5: start] Erro 1

Expected behavior
A clear and concise description of what you expected to happen.

@dmathieu
Copy link
Member

Could you provide a code sample which reproduces this issue?

@hongry18
Copy link

hongry18 commented May 5, 2023

me too

panic: sql: Register called twice for driver pgx

goroutine 1 [running]:
database/sql.Register({0x10313f7eb, 0x3}, {0x103675b40, 0x1400000e258})
	/opt/homebrew/Cellar/go/1.20.1/libexec/src/database/sql/sql.go:51 +0x124
github.com/jackc/pgx/v4/stdlib.init.0()
	/usr/local/go/pkg/mod/github.com/jackc/pgx/[email protected]/stdlib/sql.go:87 +0xb4
exit status 2

my code

import (
	postgres "go.elastic.co/apm/module/apmgormv2/v2/driver/postgres"
	"gorm.io/gorm"
)

func Conn() (*gorm.DB, error) {
	db, err := gorm.Open(postgres.Open("host=localhost port=5432 user=postgres password=1234 dbname=test"), &gorm.Config{})

	if err != nil {
		panic("db conn error")
	}

	return db, err
}

go.mod file

	github.com/jackc/chunkreader/v2 v2.0.1 // indirect
	github.com/jackc/pgconn v1.12.1 // indirect
	github.com/jackc/pgio v1.0.0 // indirect
	github.com/jackc/pgpassfile v1.0.0 // indirect
	github.com/jackc/pgproto3/v2 v2.3.1 // indirect
	github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
	github.com/jackc/pgtype v1.12.0 // indirect
	github.com/jackc/pgx/v4 v4.16.1 // indirect
	github.com/jackc/pgx/v5 v5.3.0 // indirect

	gorm.io/driver/postgres v1.5.0 // indirect
	go.elastic.co/apm/module/apmgormv2/v2 v2.4.1
	gorm.io/gorm v1.25.0

@dmathieu
Copy link
Member

dmathieu commented May 5, 2023

I'm sorry, but I'm unable to reproduce your issue with the code you provided.

package main

import (
	"fmt"
	"os"

	postgres "go.elastic.co/apm/module/apmgormv2/v2/driver/postgres"
	"gorm.io/gorm"
)

func Conn() (*gorm.DB, error) {
	db, err := gorm.Open(postgres.Open("host=localhost port=5432 user=dmathieu dbname=test"), &gorm.Config{})

	if err != nil {
		panic("db conn error")
	}

	return db, err
}

func main() {
	conn, err := Conn()
	fmt.Fprintf(os.Stdout, "%#v\n%#v\n", conn, err)
}
go run main.go
&gorm.DB{Config:(*gorm.Config)(0x140001c2510), Error:error(nil), RowsAffected:0, Statement:(*gorm.Statement)(0x14000366000), clone:1}
<nil>

go.mod has all libraries on their latest versions (generated with go mod tidy)

@hongry18
Copy link

hongry18 commented May 5, 2023

thanks for you

@shanjunmei
Copy link

shanjunmei commented Jul 15, 2023

I have the same problem.

postgres "go.elastic.co/apm/module/apmgormv2/v2/driver/postgres" "gorm.io/gorm" "gorm.io/gorm/logger"

@akwanmaroso
Copy link

is there any update about this issue ?
I got the same problem but it seems weird cause when run the app local it works well,
but when I deployed it to ECS I got this error

panic: sql: Register called twice for driver pgx
database/sql.Register({0x139a5af, 0x3}, {0x1766120, 0xc0004b6558})
/usr/local/go/src/database/sql/sql.go:51 +0x13d
/go/pkg/mod/github.com/jackc/pgx/[email protected]/stdlib/sql.go:87 +0xa7

@akwanmaroso
Copy link

akwanmaroso commented Oct 15, 2023

just found out, i imported it manually the module pgxv4 without using the driver Postgres from Gorm, and that solve my problem

package db

import (

	apmpgxv4 "go.elastic.co/apm/module/apmsql/v2/pgxv4"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

func NewPostgreConnection(cfg *config.Config) (*gorm.DB, error) {
	dialect := &postgres.Dialector{
		Config: &postgres.Config{
			DriverName: apmpgxv4.DriverName,
			DSN:        cfg.DB_DSN,
		},
	}
	db, err := gorm.Open(dialect, &gorm.Config{})
	if err != nil {
		return nil, err
	}

	return db, nil
}

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

5 participants