Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions duckdbservice/duckdb_pair.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"log/slog"
"time"

duckdb "github.com/duckdb/duckdb-go/v2"
Expand Down Expand Up @@ -86,6 +87,12 @@ type workerRequiredExtensionExecer interface {
Exec(query string, args ...any) (sql.Result, error)
}

// workerLateMaterializationMaxRows caps how many rows DuckDB late-materializes
// for LIMIT/SAMPLE queries. late_materialization_max_rows is session-scoped
// (LOCAL) in DuckDB, so it must be applied with SET GLOBAL for the value to
// reach every session connection on the shared connector.
const workerLateMaterializationMaxRows = 6000

func loadWorkerRequiredExtensions(db workerRequiredExtensionExecer) error {
if _, err := db.Exec("LOAD postgres_scanner"); err != nil {
return fmt.Errorf("load required worker extension postgres_scanner: %w", err)
Expand Down Expand Up @@ -147,6 +154,9 @@ func OpenDuckDBPair(cfg server.Config, username string) (*DuckDBPair, error) {
_ = connector.Close()
return nil, err
}
if _, err := mainDB.Exec(fmt.Sprintf("SET GLOBAL late_materialization_max_rows = %d", workerLateMaterializationMaxRows)); err != nil {
slog.Warn("Failed to set DuckDB late_materialization_max_rows.", "late_materialization_max_rows", workerLateMaterializationMaxRows, "error", err)
}
if err := loadWorkerRequiredExtensions(mainDB); err != nil {
_ = mainDB.Close()
_ = controlDB.Close()
Expand Down
35 changes: 35 additions & 0 deletions duckdbservice/duckdb_pair_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package duckdbservice

import (
"database/sql"
"testing"

"github.com/posthog/duckgres/server"
)

// TestOpenDuckDBPairSetsLateMaterializationMaxRows is the regression guard for
// the worker rollout that caps late materialization: every worker DB opened via
// OpenDuckDBPair must have late_materialization_max_rows = 6000.
//
// The assertion runs on a *fresh* connection from the same connector — the
// same shape as a session-pool connection serving user queries — so it also
// proves the setting is visible beyond the warmup connection that applied it.
func TestOpenDuckDBPairSetsLateMaterializationMaxRows(t *testing.T) {
cfg := server.Config{DataDir: t.TempDir()}
pair, err := OpenDuckDBPair(cfg, "worker")
if err != nil {
t.Fatalf("OpenDuckDBPair: %v", err)
}
defer func() { _ = pair.Close() }()

sessionDB := sql.OpenDB(pair.connector)
defer func() { _ = sessionDB.Close() }()

var got string
if err := sessionDB.QueryRow("SELECT current_setting('late_materialization_max_rows')").Scan(&got); err != nil {
t.Fatalf("read late_materialization_max_rows: %v", err)
}
if got != "6000" {
t.Errorf("late_materialization_max_rows = %q, want %q", got, "6000")
}
}
Loading