Storage adapters
src/adapters/ holds the storage adapters a service built on ahto can use. The ahto
command opens the ones in its adapters config section only for ahto migrate
(Migrations).
| Kind | Package | What it opens | Library |
|---|---|---|---|
postgres | src/adapters/postgres | a PostgreSQL server | pgx v5 |
clickhouse | src/adapters/clickhouse | a ClickHouse server, native or HTTP protocol | clickhouse-go v2 |
duckdb | src/adapters/duckdb | a DuckDB file, or a database in memory (needs cgo) | duckdb-go v2 |
parquet | src/adapters/parquet | a directory of Parquet files | parquet-go |
The three databases give a *sql.DB with the same pool settings. The Parquet store has
typed parquet.Write and parquet.Read, and DuckDB can query the files it writes with
read_parquet.
duckdb.DB adds what DuckDB has beyond database/sql:
db, err := duckdb.Open(ctx, cfg.DuckDB)
n, err := db.Append(ctx, "", "events", rows) // bulk insert through the appender
n, err = db.ExportParquet(ctx, "SELECT * FROM events", "out/events.parquet",
duckdb.ParquetOptions{Compression: "zstd"}) // or PartitionBy for a Hive directory
n, err = db.ImportParquet(ctx, "", "events_copy", "out/*.parquet")
err = db.Attach(ctx, duckdb.Attachment{Name: "other", Path: "other.duckdb", ReadOnly: true})Also Appender (row by row, with Flush), Detach, LoadExtension, CreateSecret,
Extensions, Databases, Version and Checkpoint.
Functions written in Go are given at open, so the init statements can use them, or
registered later with db.Register; DuckDB keeps them for the whole database, so every
connection of the pool sees them:
double := duckdb.Scalar1("double", func(ctx context.Context, x int64) (int64, error) {
return 2 * x, nil
}) // Scalar0 to Scalar3; Volatile, NullsIn
db, err := duckdb.OpenWith(ctx, cfg.DuckDB, duckdb.Options{Functions: []duckdb.Function{double}})
err = db.Register(ctx, duckdb.Table("numbers", duckdb.TableSpec{
Columns: []duckdb.Column{{Name: "n", Type: driver.TYPE_BIGINT}},
Args: []duckdb.Column{{Type: driver.TYPE_BIGINT}},
Rows: rows, // returns an iter.Seq2[[]any, error]
}))
info, err := db.Profile(ctx, "SELECT sum(n) FROM numbers(1000)") // the tree of operatorsScalarRaw and TableRaw take the driver's own function types, and
Options.ReplacementScan (or db.ReplacementScan) maps an unknown table name to a table
function.
With -tags=duckdb_arrow the DuckDB adapter also speaks Arrow: db.QueryArrow returns an
array.RecordReader, db.ImportArrow writes one into a table, and db.Arrow gives the
driver's Arrow interface on one connection. src/adapters/arrow (pure Go, no tag) writes and
reads the Arrow IPC stream (arrow.WriteIPC, arrow.ReadIPC). Without the tag the Arrow
methods return duckdb.ErrArrowNotBuilt. Since 2026-11-23 the tag is the default:
scripts/build.sh and every mise task build with it (GOFLAGS in mise.toml), and
mise run vet:plain, lint:plain and test:plain check the build without it, so a plain
go build ./... keeps working.
A service uses them in one of two ways. With a fixed field per store:
type Settings struct {
config.Config `yaml:",inline"`
Postgres postgres.Settings `yaml:"postgres"`
}
db, err := postgres.Open(ctx, cfg.Postgres)Or with a named set, so the config decides how many there are and of which kind:
adapters:
main:
kind: postgres
host: 127.0.0.1
port: 5432
database: ahto
user: ahto
password: {env: AHTO_POSTGRES_PASSWORD} # or {file: /run/secrets/pg}
sslmode: disable
pool: {max_open: 20, max_idle: 5}
events:
kind: clickhouse
addresses: ["127.0.0.1:9000"]
protocol: native # or http
database: ahto
user: ahto
password: {env: AHTO_CLICKHOUSE_PASSWORD}
compression: lz4
local:
kind: duckdb
path: data/ahto.duckdb # empty: in memory
threads: 4
memory_limit: 2GB
temp_directory: data/tmp
options: {preserve_insertion_order: "false"} # any other DuckDB option
extensions: # loaded at open, in order
- {name: httpfs, install: true} # install downloads it once
secrets: # made at open, kept in memory only
- name: lake
type: s3
params: {region: eu-central-1, scope: "s3://my-bucket"}
secret_params:
key_id: {env: LAKE_KEY_ID}
secret: {env: LAKE_SECRET}
attach: # other databases, read as name.schema.table
- {name: archive, path: data/archive.duckdb, read_only: true}
init: # run on every new connection
- USE archive
lake:
kind: parquet
dir: data/lake
compression: zstd- A later config layer merges into an entry by name and key, so
production.yamlcan change onlyhost. A differentkindunder the same name replaces the entry. - An unknown key or an unknown kind fails the load.
- A password is never written in the config: it names an environment variable or a file.
- Entries of the
adapterssection have noAHTO_*variables of their own; use a layer file. - A new kind is one call,
adapters.Register("mykind", open), from your own package. Its settings type hasSetDefaults()andValidate().
Migrations
Schema migrations are SQL files in migrations/<kind>/, embedded in the binary and run by
goose v3. Each file has a -- +goose Up and a
-- +goose Down part and a sequential name, 00001_schema_info.sql. A kind with no
directory (Parquet) has nothing to migrate: up and status return without an error.
adapters:
main:
kind: postgres
# ...
migrations:
auto: true # run up when the adapter opens (default false)
table: ahto_schema_migrations # the record table (the default)ahto migrate status [adapter ...] # every migration of each adapter, applied or pending
ahto migrate up [adapter ...] # apply what is pending; all adapters when none is named
ahto migrate down <adapter> # roll back the latest one of one adapter
ahto migrate create <kind> <name> # write migrations/<kind>/0000N_<name>.sql in the source treeFrom Go, set.Migrator(name) gives the same Up, Down, Status and Version, and
adapters.NewMigrator works on a database opened with a typed field.
- Two runners never apply one migration twice. PostgreSQL takes a session advisory lock
(
pg_try_advisory_lock, the id from the table name). ClickHouse and DuckDB lock inside the process only, so run their migrations from one deploy step, not from every replica. - ClickHouse runs each file without a transaction, since it has none; write its statements
with
IF [NOT] EXISTSso a file that stopped halfway can run again.migrate create clickhouseadds-- +goose NO TRANSACTIONfor you. - An adapter with
auto: truerunsupwhenever it opens, also whenahto migrate statusordownopens it. - A new file needs a rebuild: the binary carries the migrations it was built with. A new kind
adds its directory to the
go:embedline inmigrations/migrations.go.
Local servers
compose.yaml starts PostgreSQL 19.0 and ClickHouse 26.9 for work on this machine, both on
127.0.0.1 only. DuckDB and Parquet are files and need no server.
mise run compose:up # makes .env with random passwords when missing, waits until healthy
mise run test:adapters # the adapter tests against both servers
mise run compose:down # stop; compose:reset also deletes the dataPorts and passwords are in .env (not in git; .env.example lists the keys).