func tableExists(t *testing.T, tableName string) bool { connConfig, err := readConfig("testdata/tern.conf") if err != nil { t.Fatal(err) } connConfig.TLSConfig = &tls.Config{InsecureSkipVerify: true} conn, err := pgx.Connect(*connConfig) if err != nil { connConfig.TLSConfig = nil conn, err = pgx.Connect(*connConfig) } if err != nil { t.Fatal(err) } defer conn.Close() var exists bool err = conn.QueryRow( "select exists(select 1 from information_schema.tables where table_catalog=$1 and table_name=$2)", connConfig.Database, tableName, ).Scan(&exists) if err != nil { t.Fatal(err) } return exists }
func TestConnectWithTLSFallback(t *testing.T) { t.Parallel() if tlsConnConfig == nil { t.Skip("Skipping due to undefined tlsConnConfig") } connConfig := *tlsConnConfig connConfig.TLSConfig = &tls.Config{ServerName: "bogus.local"} // bogus ServerName should ensure certificate validation failure conn, err := pgx.Connect(connConfig) if err == nil { t.Fatal("Expected failed connection, but succeeded") } connConfig.UseFallbackTLS = true connConfig.FallbackTLSConfig = &tls.Config{InsecureSkipVerify: true} conn, err = pgx.Connect(connConfig) if err != nil { t.Fatal("Unable to establish connection: " + err.Error()) } err = conn.Close() if err != nil { t.Fatal("Unable to close connection") } }
func totalrep(wg *sync.WaitGroup) { var snapshot int64 conn1, err := pgx.Connect(cfg1) checkErr(err) defer conn1.Close() conn2, err := pgx.Connect(cfg2) checkErr(err) defer conn2.Close() var prevSum int64 = 0 for running { exec(conn1, "begin transaction") exec(conn2, "begin transaction") snapshot = execQuery(conn1, "select dtm_extend()") snapshot = execQuery(conn2, "select dtm_access($1)", snapshot) sum1 := execQuery(conn1, "select sum(v) from t") sum2 := execQuery(conn2, "select sum(v) from t") exec(conn1, "commit") exec(conn2, "commit") sum := sum1 + sum2 if sum != prevSum { fmt.Printf("Total=%d snapshot=%d\n", sum, snapshot) prevSum = sum } } wg.Done() }
func prepare_db() { var snapshot int64 var csn int64 var gtid string = "init" conn1, err := pgx.Connect(cfg1) checkErr(err) defer conn1.Close() conn2, err := pgx.Connect(cfg2) checkErr(err) defer conn2.Close() exec(conn1, "drop extension if exists pg_dtm") exec(conn1, "create extension pg_dtm") exec(conn1, "drop table if exists t") exec(conn1, "create table t(u int primary key, v int)") exec(conn2, "drop extension if exists pg_dtm") exec(conn2, "create extension pg_dtm") exec(conn2, "drop table if exists t") exec(conn2, "create table t(u int primary key, v int)") exec(conn1, "begin transaction") exec(conn2, "begin transaction") snapshot = execQuery(conn1, "select dtm_extend($1)", gtid) snapshot = execQuery(conn2, "select dtm_access($1, $2)", snapshot, gtid) //for i := 0; i < N_ACCOUNTS; i++ { // exec(conn1, "insert into t values($1, $2)", i, INIT_AMOUNT) // exec(conn2, "insert into t values($1, $2)", i, INIT_AMOUNT) //} exec(conn1, "insert into t (select generate_series(0,$1-1), $2)", N_ACCOUNTS, 0) exec(conn2, "insert into t (select generate_series(0,$1-1), $2)", N_ACCOUNTS, 0) exec(conn1, "prepare transaction '"+gtid+"'") exec(conn2, "prepare transaction '"+gtid+"'") exec(conn1, "select dtm_begin_prepare($1)", gtid) exec(conn2, "select dtm_begin_prepare($1)", gtid) csn = execQuery(conn1, "select dtm_prepare($1, 0)", gtid) csn = execQuery(conn2, "select dtm_prepare($1, $2)", gtid, csn) exec(conn1, "select dtm_end_prepare($1, $2)", gtid, csn) exec(conn2, "select dtm_end_prepare($1, $2)", gtid, csn) exec(conn1, "commit prepared '"+gtid+"'") exec(conn2, "commit prepared '"+gtid+"'") }
func transfer(id int, wg *sync.WaitGroup) { var err error var snapshot int64 var csn int64 nGlobalTrans := 0 conn1, err := pgx.Connect(cfg1) checkErr(err) defer conn1.Close() conn2, err := pgx.Connect(cfg2) checkErr(err) defer conn2.Close() for i := 0; i < N_ITERATIONS; i++ { gtid := strconv.Itoa(id) + "." + strconv.Itoa(i) amount := 2*rand.Intn(2) - 1 account1 := 2*id + 1 account2 := 2*id + 2 exec(conn1, "begin transaction") exec(conn2, "begin transaction") snapshot = execQuery(conn1, "select dtm_extend($1)", gtid) snapshot = execQuery(conn2, "select dtm_access($1, $2)", snapshot, gtid) exec(conn1, "update t set v = v - $1 where u=$2", amount, account1) exec(conn2, "update t set v = v + $1 where u=$2", amount, account2) exec(conn1, "prepare transaction '"+gtid+"'") exec(conn2, "prepare transaction '"+gtid+"'") exec(conn1, "select dtm_begin_prepare($1)", gtid) exec(conn2, "select dtm_begin_prepare($1)", gtid) csn = execQuery(conn1, "select dtm_prepare($1, 0)", gtid) csn = execQuery(conn2, "select dtm_prepare($1, $2)", gtid, csn) exec(conn1, "select dtm_end_prepare($1, $2)", gtid, csn) exec(conn2, "select dtm_end_prepare($1, $2)", gtid, csn) exec(conn1, "commit prepared '"+gtid+"'") exec(conn2, "commit prepared '"+gtid+"'") nGlobalTrans++ } fmt.Printf("Test completed, performed %d global transactions\n", nGlobalTrans) wg.Done() }
func main() { // get the database name as the first command-line argument if len(os.Args) <= 1 { fmt.Fprintf(os.Stderr, "Expected the name of a database as a command-line argument, but got nothing\n") os.Exit(1) } database_name := os.Args[1] pgxConfig := extractConfig() pgxConfig.Database = database_name var err error conn, err = pgx.Connect(pgxConfig) if err != nil { fmt.Fprintf(os.Stderr, "Unable to connect to the database: %v\n", err) os.Exit(1) } db := AnalyzeDatabase(conn) initiateDeepAnalysis(db.name) // print a bunch of stats about the database fmt.Printf("database name: %s\n", db.name) fmt.Printf("database size: %s\n", db.size) publicTables := selectTablesBySchema("public") fmt.Printf("the 'public' schema contains %d tables\n", len(publicTables)) printTables(publicTables) fmt.Printf("\n-----\n") printTableColumns("public", publicTables) }
func Example_OnStartMigrationProgressLogging() { conn, err := pgx.Connect(*defaultConnectionParameters) if err != nil { fmt.Printf("Unable to establish connection: %v", err) return } // Clear any previous runs if _, err = conn.Exec("drop table if exists schema_version"); err != nil { fmt.Printf("Unable to drop schema_version table: %v", err) return } var m *migrate.Migrator m, err = migrate.NewMigrator(conn, "schema_version") if err != nil { fmt.Printf("Unable to create migrator: %v", err) return } m.OnStart = func(_ int32, name, direction, _ string) { fmt.Printf("Migrating %s: %s", direction, name) } m.AppendMigration("create a table", "create temporary table foo(id serial primary key)", "") if err = m.Migrate(); err != nil { fmt.Printf("Unexpected failure migrating: %v", err) return } // Output: // Migrating up: create a table }
func (s *MigrateSuite) SetUpTest(c *C) { var err error s.conn, err = pgx.Connect(*defaultConnectionParameters) c.Assert(err, IsNil) s.cleanupSampleMigrator(c) }
func (t TransfersFDW) prepare_master() { dbconf, err := pgx.ParseDSN(cfg.ConnStrs[0]) checkErr(err) conn, err := pgx.Connect(dbconf) checkErr(err) defer conn.Close() exec(conn, "CREATE EXTENSION postgres_fdw") for i, connstr := range cfg.ConnStrs[1:] { conf, _ := pgx.ParseDSN(connstr) server_sql := fmt.Sprintf( "CREATE SERVER dtm%d FOREIGN DATA WRAPPER postgres_fdw options(dbname '%s', host '%s', port '%d')", i, conf.Database, conf.Host, conf.Port) foreign_sql := fmt.Sprintf( "CREATE FOREIGN TABLE t_fdw%d() inherits (t) server dtm%d options(table_name 't')", i, i) mapping_sql := fmt.Sprintf( "CREATE USER MAPPING for %s SERVER dtm%d options (user '%s')", conf.User, i, conf.User) exec(conn, server_sql) exec(conn, foreign_sql) exec(conn, mapping_sql) } if dbconf.User != "" { } }
func Example_CustomType() { conn, err := pgx.Connect(*defaultConnConfig) if err != nil { fmt.Printf("Unable to establish connection: %v", err) return } var p NullPoint err = conn.QueryRow("select null::point").Scan(&p) if err != nil { fmt.Println(err) return } fmt.Println(p) err = conn.QueryRow("select point(1.5,2.5)").Scan(&p) if err != nil { fmt.Println(err) return } fmt.Println(p) // Output: // null point // 1.5, 2.5 }
func TestFatalTxError(t *testing.T) { t.Parallel() // Run timing sensitive test many times for i := 0; i < 50; i++ { func() { conn := mustConnect(t, *defaultConnConfig) defer closeConn(t, conn) otherConn, err := pgx.Connect(*defaultConnConfig) if err != nil { t.Fatalf("Unable to establish connection: %v", err) } defer otherConn.Close() _, err = otherConn.Exec("select pg_terminate_backend($1)", conn.Pid) if err != nil { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } _, err = conn.Query("select 1") if err == nil { t.Fatal("Expected error but none occurred") } if conn.IsAlive() { t.Fatalf("Connection should not be live but was. Previous Query err: %v", err) } }() } }
func mustConnect(t testing.TB, config pgx.ConnConfig) *pgx.Conn { conn, err := pgx.Connect(config) if err != nil { t.Fatalf("Unable to establish connection: %v", err) } return conn }
func (t TransfersFDW) reader(wg *sync.WaitGroup, cFetches chan int, inconsistency *bool) { var sum int64 var prevSum int64 = 0 dbconf, err := pgx.ParseDSN(cfg.ConnStrs[0]) checkErr(err) conn, err := pgx.Connect(dbconf) checkErr(err) defer conn.Close() for running { exec(conn, "select dtm_begin_transaction()") exec(conn, "begin transaction isolation level "+cfg.Isolation) sum = execQuery64(conn, "select sum(v) from t") if sum != prevSum { fmt.Printf("Total=%d\n", sum) fmt.Printf("inconsistency!\n") *inconsistency = true prevSum = sum } exec(conn, "commit") } conn.Close() wg.Done() }
func (c *Config) Connect() (*pgx.Conn, error) { if c.SSHConnConfig.Host != "" { client, err := NewSSHClient(&c.SSHConnConfig) if err != nil { return nil, err } c.ConnConfig.Dial = client.Dial } // If sslmode was set in config file or cli argument, set it in the // environment so we can use pgx.ParseEnvLibpq to use pgx's built-in // functionality. switch c.SslMode { case "disable", "allow", "prefer", "require", "verify-ca", "verify-full": if err := os.Setenv("PGHOST", c.ConnConfig.Host); err != nil { return nil, err } if err := os.Setenv("PGSSLMODE", c.SslMode); err != nil { return nil, err } if cc, err := pgx.ParseEnvLibpq(); err == nil { c.ConnConfig.TLSConfig = cc.TLSConfig c.ConnConfig.UseFallbackTLS = cc.UseFallbackTLS c.ConnConfig.FallbackTLSConfig = cc.FallbackTLSConfig } else { return nil, err } } return pgx.Connect(c.ConnConfig) }
func TestConnectCustomDialer(t *testing.T) { t.Parallel() if customDialerConnConfig == nil { t.Skip("Skipping due to undefined customDialerConnConfig") } dialled := false conf := *customDialerConnConfig conf.Dial = func(network, address string) (net.Conn, error) { dialled = true return net.Dial(network, address) } conn, err := pgx.Connect(conf) if err != nil { t.Fatalf("Unable to establish connection: %s", err) } if !dialled { t.Fatal("Connect did not use custom dialer") } err = conn.Close() if err != nil { t.Fatal("Unable to close connection") } }
func (api *Api) InitDB() { var err error api.DB, err = pgx.Connect(extractConfig()) if err != nil { log.Fatalf("Got error when connect database, the error is '%v'", err) } }
func TestStressTLSConnection(t *testing.T) { t.Parallel() if tlsConnConfig == nil { t.Skip("Skipping due to undefined tlsConnConfig") } if testing.Short() { t.Skip("Skipping due to testing -short") } conn, err := pgx.Connect(*tlsConnConfig) if err != nil { t.Fatalf("Unable to establish connection: %v", err) } defer conn.Close() for i := 0; i < 50; i++ { sql := `select * from generate_series(1, $1)` rows, err := conn.Query(sql, 2000000) if err != nil { t.Fatal(err) } var n int32 for rows.Next() { rows.Scan(&n) } if rows.Err() != nil { t.Fatalf("queryCount: %d, Row number: %d. %v", i, n, rows.Err()) } } }
func TestConnectWithRuntimeParams(t *testing.T) { t.Parallel() connConfig := *defaultConnConfig connConfig.RuntimeParams = map[string]string{ "application_name": "pgxtest", "search_path": "myschema", } conn, err := pgx.Connect(connConfig) if err != nil { t.Fatalf("Unable to establish connection: %v", err) } defer conn.Close() var s string err = conn.QueryRow("show application_name").Scan(&s) if err != nil { t.Fatalf("QueryRow Scan unexpectedly failed: %v", err) } if s != "pgxtest" { t.Errorf("Expected application_name to be %s, but it was %s", "pgxtest", s) } err = conn.QueryRow("show search_path").Scan(&s) if err != nil { t.Fatalf("QueryRow Scan unexpectedly failed: %v", err) } if s != "myschema" { t.Errorf("Expected search_path to be %s, but it was %s", "myschema", s) } }
func (t Readers) writer(id int, cCommits chan int, cAborts chan int, wg *sync.WaitGroup) { var updates = 0 var conns []*pgx.Conn for _, connstr := range cfg.ConnStrs { dbconf, err := pgx.ParseDSN(connstr) checkErr(err) conn, err := pgx.Connect(dbconf) checkErr(err) defer conn.Close() conns = append(conns, conn) } for updates < cfg.IterNum { acc := rand.Intn(cfg.AccountsNum) if cfg.UseDtm { xid := execQuery(conns[0], "select dtm_begin_transaction()") for i := 1; i < len(conns); i++ { exec(conns[i], "select dtm_join_transaction($1)", xid) } } for _, conn := range conns { exec(conn, "begin transaction isolation level "+cfg.Isolation) exec(conn, "update t set v = v + 1 where u=$1", acc) } commit(conns...) updates++ } // cCommits <- updates wg.Done() }
func TestFatalRxError(t *testing.T) { t.Parallel() conn := mustConnect(t, *defaultConnConfig) defer closeConn(t, conn) var wg sync.WaitGroup wg.Add(1) go func() { defer wg.Done() var n int32 var s string err := conn.QueryRow("select 1::int4, pg_sleep(10)::varchar").Scan(&n, &s) if err, ok := err.(pgx.PgError); !ok || err.Severity != "FATAL" { t.Fatalf("Expected QueryRow Scan to return fatal PgError, but instead received %v", err) } }() otherConn, err := pgx.Connect(*defaultConnConfig) if err != nil { t.Fatalf("Unable to establish connection: %v", err) } defer otherConn.Close() if _, err := otherConn.Exec("select pg_terminate_backend($1)", conn.Pid); err != nil { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } wg.Wait() if conn.IsAlive() { t.Fatal("Connection should not be live but was") } }
func main() { var err error conn, err = pgx.Connect(extractConfig()) if err != nil { fmt.Fprintf(os.Stderr, "Unable to connection to database: %v\n", err) os.Exit(1) } if len(os.Args) == 1 { printHelp() os.Exit(0) } switch os.Args[1] { case "list": err = listTasks() if err != nil { fmt.Fprintf(os.Stderr, "Unable to list tasks: %v\n", err) os.Exit(1) } case "add": err = addTask(os.Args[2]) if err != nil { fmt.Fprintf(os.Stderr, "Unable to add task: %v\n", err) os.Exit(1) } case "update": n, err := strconv.ParseInt(os.Args[2], 10, 32) if err != nil { fmt.Fprintf(os.Stderr, "Unable convert task_num into int32: %v\n", err) os.Exit(1) } err = updateTask(int32(n), os.Args[3]) if err != nil { fmt.Fprintf(os.Stderr, "Unable to update task: %v\n", err) os.Exit(1) } case "remove": n, err := strconv.ParseInt(os.Args[2], 10, 32) if err != nil { fmt.Fprintf(os.Stderr, "Unable convert task_num into int32: %v\n", err) os.Exit(1) } err = removeTask(int32(n)) if err != nil { fmt.Fprintf(os.Stderr, "Unable to remove task: %v\n", err) os.Exit(1) } default: fmt.Fprintln(os.Stderr, "Invalid command") printHelp() os.Exit(1) } }
func (t TransfersTS) reader(wg *sync.WaitGroup, cFetches chan int, inconsistency *bool) { var prevSum int64 = 0 var snapshot int64 var conns []*pgx.Conn for _, connstr := range cfg.ConnStrs { dbconf, err := pgx.ParseDSN(connstr) checkErr(err) conn, err := pgx.Connect(dbconf) checkErr(err) defer conn.Close() conns = append(conns, conn) } for running { var sum int64 = 0 var xid int32 for _, conn := range conns { exec(conn, "begin transaction") } for i, conn := range conns { if cfg.UseDtm { if i == 0 { // xid = execQuery(conn, "select dtm_begin_transaction()") snapshot = _execQuery(conn, "select dtm_extend()") } else { // exec(conn, "select dtm_join_transaction($1)", xid) snapshot = _execQuery(conn, "select dtm_access($1)", snapshot) } } exec(conn, "begin transaction isolation level " + cfg.Isolation) sum += _execQuery(conn, "select sum(v) from t") } for _, conn := range conns { sum += _execQuery(conn, "select sum(v) from t") } commit(conns...) if (sum != prevSum) { fmt.Printf("Total=%d xid=%d\n", sum, xid) if (prevSum != 0) { fmt.Printf("inconsistency!\n") *inconsistency = true } prevSum = sum } } wg.Done() }
func (t TransfersTS) writer(id int, cCommits chan int, cAborts chan int, wg *sync.WaitGroup) { var conns []*pgx.Conn var nGlobalTrans = 0 var snapshot int64 var csn int64 if len(cfg.ConnStrs) == 1 { cfg.ConnStrs.Set(cfg.ConnStrs[0]) } for _, connstr := range cfg.ConnStrs { dbconf, err := pgx.ParseDSN(connstr) checkErr(err) conn, err := pgx.Connect(dbconf) checkErr(err) defer conn.Close() conns = append(conns, conn) } for i := 0; i < cfg.IterNum; i++ { gtid := strconv.Itoa(id) + "." + strconv.Itoa(i) amount := 2*rand.Intn(2) - 1 from_acc := cfg.Writers.StartId + 2*id + 1 to_acc := cfg.Writers.StartId + 2*id + 2 conn1 := conns[rand.Intn(len(conns))] conn2 := conns[rand.Intn(len(conns))] if conn1 == conn2 { continue } exec(conn1, "begin transaction") exec(conn2, "begin transaction") snapshot = _execQuery(conn1, "select dtm_extend($1)", gtid) snapshot = _execQuery(conn2, "select dtm_access($1, $2)", snapshot, gtid) exec(conn1, "update t set v = v - $1 where u=$2", amount, from_acc) exec(conn2, "update t set v = v + $1 where u=$2", amount, to_acc) exec(conn1, "prepare transaction '" + gtid + "'") exec(conn2, "prepare transaction '" + gtid + "'") exec(conn1, "select dtm_begin_prepare($1)", gtid) exec(conn2, "select dtm_begin_prepare($1)", gtid) csn = _execQuery(conn1, "select dtm_prepare($1, 0)", gtid) csn = _execQuery(conn2, "select dtm_prepare($1, $2)", gtid, csn) exec(conn1, "select dtm_end_prepare($1, $2)", gtid, csn) exec(conn2, "select dtm_end_prepare($1, $2)", gtid, csn) exec(conn1, "commit prepared '" + gtid + "'") exec(conn2, "commit prepared '" + gtid + "'") nGlobalTrans++ } cCommits <- nGlobalTrans cAborts <- 0 wg.Done() }
func connect(c *C, s state.Database, db string) *pgx.Conn { port, _ := strconv.Atoi(s.(*Postgres).port) conn, err := pgx.Connect(pgx.ConnConfig{ Host: "127.0.0.1", Port: uint16(port), User: "******", Password: "******", Database: db, }) c.Assert(err, IsNil) return conn }
func TestConnectWithConnectionRefused(t *testing.T) { t.Parallel() // Presumably nothing is listening on 127.0.0.1:1 bad := *defaultConnConfig bad.Host = "127.0.0.1" bad.Port = 1 _, err := pgx.Connect(bad) if !strings.Contains(err.Error(), "connection refused") { t.Fatal("Unable to establish connection: " + err.Error()) } }
func TestConnectWithConnectionRefused(t *testing.T) { t.Parallel() // Presumably nothing is listening on 127.0.0.1:1 bad := *defaultConnConfig bad.Host = "127.0.0.1" bad.Port = 1 _, err := pgx.Connect(bad) if err == nil { t.Fatal("Expected error establishing connection to bad port") } }
func (t Readers) prepare_one(connstr string, wg *sync.WaitGroup) { dbconf, err := pgx.ParseDSN(connstr) checkErr(err) conn, err := pgx.Connect(dbconf) checkErr(err) defer conn.Close() if cfg.UseDtm { exec(conn, "drop extension if exists pg_dtm") exec(conn, "create extension pg_dtm") } exec(conn, "drop table if exists t cascade") exec(conn, "create table t(u int primary key, v int)") exec(conn, "insert into t (select generate_series(0,$1-1), $2)", cfg.AccountsNum, 0) wg.Done() }
func checkConnection() { var err error for db == nil || !db.IsAlive() { if db, err = pgx.Connect(dbConfig); err != nil { log.Println("Could not open connection to database:", err) } else if err = db.Listen(quoteIdentifier(config.Postgres.UpdatesChannelName)); err != nil { log.Println("Could not listen to channel:", err) db.Close() } else if process() { break } time.Sleep(time.Second) } }
func TestConnectWithInvalidUser(t *testing.T) { t.Parallel() if invalidUserConnConfig == nil { t.Skip("Skipping due to undefined invalidUserConnConfig") } _, err := pgx.Connect(*invalidUserConnConfig) pgErr, ok := err.(pgx.PgError) if !ok { t.Fatalf("Expected to receive a PgError with code 28000, instead received: %v", err) } if pgErr.Code != "28000" && pgErr.Code != "28P01" { t.Fatalf("Expected to receive a PgError with code 28000 or 28P01, instead received: %v", pgErr) } }
func TestConnectWithMD5Password(t *testing.T) { t.Parallel() if md5ConnConfig == nil { t.Skip("Skipping due to undefined md5ConnConfig") } conn, err := pgx.Connect(*md5ConnConfig) if err != nil { t.Fatal("Unable to establish connection: " + err.Error()) } err = conn.Close() if err != nil { t.Fatal("Unable to close connection") } }