Documentation
¶
Overview ¶
Package pglite runs PostgreSQL 17.5 in-process: PGlite's WASI build of PostgreSQL, executed with wasmtime, behind a Unix socket that any PostgreSQL client in the process can connect to.
Each instance is one PostgreSQL single-user backend, so it serves one session at a time; a second concurrent connection is refused with SQLSTATE 53300. When a connection closes, the next one gets a clean session. Instances are independent: a test can start one per worker, copied from a template made with CreateTemplate.
pg, err := pglite.New(pglite.Config{})
if err != nil { ... }
defer pg.Close()
pool, err := pg.Pool(ctx) // *pgxpool.Pool with one connection
Index ¶
Constants ¶
const ( User = "postgres" Database = "postgres" )
User and Database are the only role and database the backend serves: the module runs initdb as this user and starts the single-user backend on this database.
Variables ¶
This section is empty.
Functions ¶
func CreateTemplate ¶
func CreateTemplate(ctx context.Context, dir string, cfg Config, setup func(ctx context.Context, pg *PGlite) error) error
CreateTemplate makes a template for Config.Template at dir: it starts a fresh instance (cfg.DataDir and cfg.Template must be empty), runs setup on it (nil for none), for example to apply migrations, and shuts it down. The instance is built in a temporary sibling directory that is renamed to dir only on success, so dir is either absent or a complete template; if dir already exists the rename fails and nothing is changed.
func DefaultCacheDir ¶
DefaultCacheDir returns the directory New uses for Config.CacheDir when it is empty: go-pglite under the user's cache directory.
Types ¶
type Config ¶
type Config struct {
// DataDir is the instance directory, holding the cluster under
// pglite/base. It is created if missing; a directory without a cluster
// is initialised. Empty means a temporary directory removed by Close.
DataDir string
// Template is a directory made by CreateTemplate whose cluster is
// copied into DataDir before the start. DataDir must not hold a cluster
// yet.
Template string
// CacheDir holds what every instance shares: the extracted installation
// and wasmtime's compiled code. Empty means DefaultCacheDir.
CacheDir string
// Settings are session settings every connection starts with, applied
// before the client's own startup parameters. A setting PostgreSQL
// rejects fails the first connection. For example,
// synchronous_commit=off makes a commit return before its WAL reaches
// disk, which on macOS (where every fsync is a full flush) takes
// several milliseconds; a crash can then lose the last commits, but
// never corrupt the cluster.
Settings map[string]string
// StdoutFile and StderrFile receive the module's standard output and
// PostgreSQL's log. Empty discards them.
StdoutFile string
StderrFile string
// Logger receives the bridge's own messages, such as a crash of the
// module. nil means slog.Default.
Logger *slog.Logger
}
Config configures an instance. The zero value starts a fresh cluster in a temporary directory that Close removes.
type PGlite ¶
type PGlite struct {
// contains filtered or unexported fields
}
PGlite is one embedded PostgreSQL instance.
func New ¶
New starts an instance. A fresh directory is initialised, which takes a few seconds; a clone of a template (Config.Template) or an existing cluster starts in a fraction of that.
func (*PGlite) Close ¶
Close ends the sessions, stops PostgreSQL and releases the instance; a temporary DataDir is removed.
func (*PGlite) ConnString ¶
ConnString returns a connection string for the instance, for pgx, database/sql drivers and other clients in the process. For a pgxpool, add pool_max_conns=1 or use Pool.
func (*PGlite) DB ¶
DB returns a database/sql handle on the instance (driver pgx) with one connection. It is closed by Close.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
pglite-repl
command
Command pglite-repl provides a simple REPL for interacting with an embedded PGlite PostgreSQL instance.
|
Command pglite-repl provides a simple REPL for interacting with an embedded PGlite PostgreSQL instance. |