Skip to content

Commit

Permalink
fix: close (intead of release) the duckdb connection (#301)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyang01 authored Dec 19, 2024
1 parent f873750 commit c23f2b0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 11 deletions.
6 changes: 3 additions & 3 deletions adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type ConnectionHolder interface {
GetCatalogTxn(ctx context.Context, options *stdsql.TxOptions) (*stdsql.Tx, error)
TryGetTxn() *stdsql.Tx
CloseTxn()
CloseBackendConn()
CloseConn()
}

func GetConn(ctx *sql.Context) (*stdsql.Conn, error) {
Expand All @@ -26,8 +26,8 @@ func GetCatalogConn(ctx *sql.Context) (*stdsql.Conn, error) {
return ctx.Session.(ConnectionHolder).GetCatalogConn(ctx)
}

func CloseBackendConn(ctx *sql.Context) {
ctx.Session.(ConnectionHolder).CloseBackendConn()
func CloseConn(ctx *sql.Context) {
ctx.Session.(ConnectionHolder).CloseConn()
}

func GetTxn(ctx *sql.Context, options *stdsql.TxOptions) (*stdsql.Tx, error) {
Expand Down
12 changes: 11 additions & 1 deletion backend/connpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package backend
import (
"context"
stdsql "database/sql"
"database/sql/driver"
"errors"
"fmt"
"strings"
Expand Down Expand Up @@ -110,7 +111,16 @@ func (p *ConnectionPool) CloseConn(id uint32) error {
entry, ok := p.conns.Load(id)
if ok {
conn := entry.(*stdsql.Conn)
if err := conn.Close(); err != nil {
if err := conn.Raw(func(driverConn any) error {
// When driver.ErrBadConn is returned here,
// the connection will not be put back into
// the pool and will be closed instead.
return driver.ErrBadConn
}); err != nil && !errors.Is(err, driver.ErrBadConn) {
logrus.WithError(err).Warn("Failed to close connection during Raw function call")
return err
}
if err := conn.Close(); err != nil && !errors.Is(err, stdsql.ErrConnDone) {
logrus.WithError(err).Warn("Failed to close connection")
return err
}
Expand Down
4 changes: 2 additions & 2 deletions backend/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ func (sess *Session) CloseTxn() {
sess.pool.CloseTxn(sess.ID())
}

// CloseBackendConn implements adapter.ConnectionHolder.
func (sess *Session) CloseBackendConn() {
// CloseConn implements adapter.ConnectionHolder.
func (sess *Session) CloseConn() {
sess.pool.CloseConn(sess.ID())
}

Expand Down
5 changes: 3 additions & 2 deletions pgserver/connection_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,10 @@ func NewConnectionHandler(conn net.Conn, handler mysql.Handler, engine *gms.Engi
func (h *ConnectionHandler) closeBackendConn() {
ctx, err := h.duckHandler.NewContext(context.Background(), h.mysqlConn, "")
if err != nil {
fmt.Println(err.Error())
h.logger.WithError(err).Error("Failed to create context for closing backend connection")
return
}
adapter.CloseBackendConn(ctx)
adapter.CloseConn(ctx)
}

// HandleConnection handles a connection's session, reading messages, executing queries, and sending responses.
Expand Down
17 changes: 16 additions & 1 deletion test/bats/postgres/cli.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,19 @@ load helper

@test "cli_show_all_tables" {
psql_exec "SHOW ALL TABLES;" | grep -q '__sys__'
}
}

# `DISCARD ALL` should clear all temp tables
@test "discard_all_clears_temp_tables" {
# Run all SQL statements in a single session
run psql_exec_stdin <<-EOF
CREATE TEMP TABLE tt (id int);
INSERT INTO tt VALUES (1), (2);
SELECT COUNT(*) FROM tt;
DISCARD ALL;
SELECT COUNT(*) FROM tt;
EOF

[ "$status" -ne 0 ]
[[ "${output}" == *"Table with name tt does not exist"* ]]
}
4 changes: 2 additions & 2 deletions test/bats/postgres/helper.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ PG_USER=${PG_USER:-"postgres"}
psql_exec() {
local query="$1"
shift
psql -h "$PG_HOST" -U "$PG_USER" -F ',' --no-align --field-separator ',' --pset footer=off "$@" -c "$query"
psql -h "$PG_HOST" -U "$PG_USER" -F ',' --no-align --field-separator ',' -t --pset footer=off -v "ON_ERROR_STOP=1" "$@" -c "$query"
}

psql_exec_stdin() {
psql -h "$PG_HOST" -U "$PG_USER" -F ',' --no-align --field-separator ',' --pset footer=off "$@"
psql -h "$PG_HOST" -U "$PG_USER" -F ',' --no-align --field-separator ',' -t --pset footer=off -v "ON_ERROR_STOP=1" "$@"
}

0 comments on commit c23f2b0

Please sign in to comment.