Skip to content

Commit c23f2b0

Browse files
authored
fix: close (intead of release) the duckdb connection (#301)
1 parent f873750 commit c23f2b0

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

adapter/adapter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type ConnectionHolder interface {
1515
GetCatalogTxn(ctx context.Context, options *stdsql.TxOptions) (*stdsql.Tx, error)
1616
TryGetTxn() *stdsql.Tx
1717
CloseTxn()
18-
CloseBackendConn()
18+
CloseConn()
1919
}
2020

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

29-
func CloseBackendConn(ctx *sql.Context) {
30-
ctx.Session.(ConnectionHolder).CloseBackendConn()
29+
func CloseConn(ctx *sql.Context) {
30+
ctx.Session.(ConnectionHolder).CloseConn()
3131
}
3232

3333
func GetTxn(ctx *sql.Context, options *stdsql.TxOptions) (*stdsql.Tx, error) {

backend/connpool.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package backend
1616
import (
1717
"context"
1818
stdsql "database/sql"
19+
"database/sql/driver"
1920
"errors"
2021
"fmt"
2122
"strings"
@@ -110,7 +111,16 @@ func (p *ConnectionPool) CloseConn(id uint32) error {
110111
entry, ok := p.conns.Load(id)
111112
if ok {
112113
conn := entry.(*stdsql.Conn)
113-
if err := conn.Close(); err != nil {
114+
if err := conn.Raw(func(driverConn any) error {
115+
// When driver.ErrBadConn is returned here,
116+
// the connection will not be put back into
117+
// the pool and will be closed instead.
118+
return driver.ErrBadConn
119+
}); err != nil && !errors.Is(err, driver.ErrBadConn) {
120+
logrus.WithError(err).Warn("Failed to close connection during Raw function call")
121+
return err
122+
}
123+
if err := conn.Close(); err != nil && !errors.Is(err, stdsql.ErrConnDone) {
114124
logrus.WithError(err).Warn("Failed to close connection")
115125
return err
116126
}

backend/session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,8 @@ func (sess *Session) CloseTxn() {
231231
sess.pool.CloseTxn(sess.ID())
232232
}
233233

234-
// CloseBackendConn implements adapter.ConnectionHolder.
235-
func (sess *Session) CloseBackendConn() {
234+
// CloseConn implements adapter.ConnectionHolder.
235+
func (sess *Session) CloseConn() {
236236
sess.pool.CloseConn(sess.ID())
237237
}
238238

pgserver/connection_handler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,10 @@ func NewConnectionHandler(conn net.Conn, handler mysql.Handler, engine *gms.Engi
117117
func (h *ConnectionHandler) closeBackendConn() {
118118
ctx, err := h.duckHandler.NewContext(context.Background(), h.mysqlConn, "")
119119
if err != nil {
120-
fmt.Println(err.Error())
120+
h.logger.WithError(err).Error("Failed to create context for closing backend connection")
121+
return
121122
}
122-
adapter.CloseBackendConn(ctx)
123+
adapter.CloseConn(ctx)
123124
}
124125

125126
// HandleConnection handles a connection's session, reading messages, executing queries, and sending responses.

test/bats/postgres/cli.bats

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,19 @@ load helper
44

55
@test "cli_show_all_tables" {
66
psql_exec "SHOW ALL TABLES;" | grep -q '__sys__'
7-
}
7+
}
8+
9+
# `DISCARD ALL` should clear all temp tables
10+
@test "discard_all_clears_temp_tables" {
11+
# Run all SQL statements in a single session
12+
run psql_exec_stdin <<-EOF
13+
CREATE TEMP TABLE tt (id int);
14+
INSERT INTO tt VALUES (1), (2);
15+
SELECT COUNT(*) FROM tt;
16+
DISCARD ALL;
17+
SELECT COUNT(*) FROM tt;
18+
EOF
19+
20+
[ "$status" -ne 0 ]
21+
[[ "${output}" == *"Table with name tt does not exist"* ]]
22+
}

test/bats/postgres/helper.bash

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ PG_USER=${PG_USER:-"postgres"}
88
psql_exec() {
99
local query="$1"
1010
shift
11-
psql -h "$PG_HOST" -U "$PG_USER" -F ',' --no-align --field-separator ',' --pset footer=off "$@" -c "$query"
11+
psql -h "$PG_HOST" -U "$PG_USER" -F ',' --no-align --field-separator ',' -t --pset footer=off -v "ON_ERROR_STOP=1" "$@" -c "$query"
1212
}
1313

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

0 commit comments

Comments
 (0)