-
Notifications
You must be signed in to change notification settings - Fork 110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to manually commit the contents of the WAL file to the main database file before db closed? #179
Comments
You do not need to create an appender for every row you want to insert. Please create the appender beforehand and use this to append rows to it.
|
then the program will panic with
Thanks for your reply! |
The focus is on how to commit the contents of the WAL file to the main database file before closing the database connection. This is because when inserting a large amount of data (10+ GB CSV) into DuckDB, it is necessary to commit the contents of the WAL file to the main database file after inserting a certain amount of data, rather than committing all at once at the end. If the program exits unexpectedly, this approach prevents data loss. |
Whats happens when you call |
Unfortunately, neither appender.close nor appender.flush has been able to commit the data from the WAL file to the main database file. I also gave appender.flush a try, but it didn't seem to have any effect. Just to clarify, my concern is about committing data from the WAL file to the main database file, rather than from memory to disk. |
@coloraven try |
no effect,you can test yourself package main
import (
"context"
"database/sql"
"database/sql/driver"
"fmt"
"sync"
"time"
"github.com/marcboeker/go-duckdb"
)
func main() {
connector, err := duckdb.NewConnector("test.db", func(execer driver.ExecerContext) error {
bootQueries := []string{
"CREATE TABLE IF NOT EXISTS info (ids int,name varchar)",
}
for _, query := range bootQueries {
// Use ExecContext instead of Exec
_, err := execer.ExecContext(context.Background(), query, nil)
if err != nil {
return err
}
}
return nil
})
if err != nil {
fmt.Println(err)
}
// defer connector.Close()
conn, err := connector.Connect(context.Background())
if err != nil {
fmt.Println(err)
}
// defer conn.Close()
var wg sync.WaitGroup
for i := int32(0); i < 10000; i++ {
wg.Add(1)
go func(i int32) {
defer wg.Done()
appender, err := duckdb.NewAppenderFromConn(conn, "", "info")
if err != nil {
fmt.Println(err)
}
defer appender.Close()
err = appender.AppendRow(i, "d")
if err != nil {
fmt.Println(err)
}
appender.Flush()
}(i)
}
db := sql.OpenDB(connector)
defer db.Close()
fmt.Println("\n[Please observe the changes in the WAL file and database file before and after.]\n")
fmt.Println("Pausing for 3 seconds, Then exec CHECKPOINT.\n")
db.Exec("CHECKPOINT")
wg.Wait()
fmt.Println("Pausing for 3 seconds, Then close conn\n")
time.Sleep(3 * time.Second)
conn.Close()
fmt.Println("Pausing for 3 seconds, Then close connector\n")
time.Sleep(3 * time.Second)
connector.Close() // Then the contents of the WAL file commited to the main database file
fmt.Println("Pausing for 5 seconds, Then exit program\n")
time.Sleep(5 * time.Second)
} And I tested it in CLI, it effect. CREATE TABLE IF NOT EXISTS info (
ids INT,
name VARCHAR
);
INSERT INTO info (ids, name)
SELECT
gs,
'Name_' || CAST(gs AS VARCHAR)
FROM
generate_series(1, 10000) AS t(gs);
-- now the wal file size : 137kb
-- and the main db file size: 12kb
-- then we exec checkpoint
checkpoint;
-- after that
-- the wal file size : 0kb
-- and the main db file size: 524kb but if change the insert sql to: INSERT INTO info (ids, name)
SELECT
gs,
'Name_' || CAST(gs AS VARCHAR)
FROM
generate_series(1, 100000000) AS t(gs); no need exec checkpoint ,the duckdb will sync data from wal to db file. |
When inserting a lot of data, DuckDB triggers Your scenario sounds like we're not monitoring this automatic checkpoint threshold when using the |
How to manually commit the contents of the WAL file to the main database file while using the appender method for parallel insert data before db closed.
The text was updated successfully, but these errors were encountered: