mattn / mattn/go-sqlite3

Temporary Table Persists After Connection Closure

Open
#1,330 13 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
9.2k
Forks
1.2k
Avg merge
19m
Merged PRs (30d)
4

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the main.go reproduction, especially createTempTableTwice, and run it with github.com/mattn/go-sqlite3 v1.14.24 to confirm the temporary table persists across db.Conn calls. Trace how database/sql connections are closed and reused by the sqlite3 driver; done means the second CREATE TEMPORARY TABLE succeeds without seeing the first connection's table.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, sqlite
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.