Skip to content

Temporary Table Persists After Connection Closure #1330

@wjkoh

Description

@wjkoh

I've encountered an issue where a temporary table is not deleted when its associated database connection is closed. Furthermore, it appears the closed connection is being reused, allowing subsequent users to see the supposedly deleted temporary table from a new database connection. Here's an example to reproduce the problem.

main.go

package main

import (
	"context"
	"database/sql"
	"log"
	"os"

	_ "github.com/mattn/go-sqlite3"
)

func main() {
	err := createTempTableTwice(context.Background())
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Done.")
}

func createTempTableTwice(ctx context.Context) error {
	os.Remove("./foo.db")
	db, err := sql.Open("sqlite3", "./foo.db")
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()
	{
		conn1, err := db.Conn(ctx)
		if err != nil {
			return err
		}
		_, err = conn1.ExecContext(ctx, `CREATE TEMPORARY TABLE temp.documents(document_id TEXT PRIMARY KEY);`)
		if err != nil {
			return err
		}
		err = conn1.Close()
		if err != nil {
			return err
		}
	}
	conn2, err := db.Conn(ctx)
	if err != nil {
		return err
	}
	defer conn2.Close()
	// NOTE: This line causes the following error: `2025/03/24 15:42:40 table documents already exists`
	_, err = conn2.ExecContext(ctx, `CREATE TEMPORARY TABLE temp.documents(document_id TEXT PRIMARY KEY);`)
	return err
}

go.mod:

module example.com/sqlite-conn-temp-table

go 1.24

require github.com/mattn/go-sqlite3 v1.14.24

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions