postgrespresets

package
v0.30.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CreateSchema is the format string for the statement that creates a schema.
	// Schema names cannot be bound as query parameters, so the name is
	// interpolated into the statement.
	CreateSchema = "CREATE SCHEMA IF NOT EXISTS %s;"

	// MaxIdleConnsDefault is the number of idle connections a pool keeps when the
	// caller expresses no preference.
	//
	// database/sql keeps two, so past the second concurrent query every call opens
	// a fresh connection — a TCP round trip, a TLS handshake and a PostgreSQL
	// authentication exchange — and discards it on release, putting a silent
	// latency floor under every query. Idle connections cost a file descriptor
	// here and a backend on the server; ten stays well under a stock
	// max_connections of 100 once multiplied by a service's replica count.
	MaxIdleConnsDefault = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Default

type Default struct {

	// MaxIdleConns overrides how many idle connections the pools keep. Zero means
	// MaxIdleConnsDefault; a negative value keeps none, matching database/sql.
	MaxIdleConns int

	// MaxOpenConns bounds how many connections the pools may open. Zero means
	// unlimited, matching database/sql.
	//
	// No default: the right number depends on how many replicas share the database,
	// which a library cannot know.
	//
	// The limit applies as a pool opens. Setting it on the handle afterwards stops
	// working once anything has taken a connection, because the handle is cached;
	// past that point it applies to nothing and reports nothing.
	MaxOpenConns int
	// contains filtered or unexported fields
}

Default is the standard postgres.Config implementation, backed by pgdriver. It opens connections lazily and caches them: one for the primary database and one per requested schema, all guarded by a single mutex.

func NewDefault

func NewDefault(options ...pgdriver.Option) *Default

NewDefault returns a Default that connects using the given pgdriver options.

func (*Default) DB

func (config *Default) DB(ctx context.Context) (*bun.DB, error)

DB returns the main database connection.

func (*Default) DBSchema

func (config *Default) DBSchema(ctx context.Context, schema string, create bool) (*bun.DB, error)

DBSchema returns a database connection scoped to the named schema, caching one connection per schema name. When create is true and the schema has no cached connection yet, the schema is created before the connection is returned.

func (*Default) DropSchema added in v0.27.0

func (config *Default) DropSchema(ctx context.Context, schema string) error

DropSchema removes a schema created through DBSchema and releases the pool cached for it. It is a test-support operation — production schemas are not dropped — and pairs with the throwaway schema RunIsolatedTransactionalTest stands up.

Without it, a randomly named schema and its pool are both kept forever: the schema accumulates in the database, and the pool holds idle connections against a schema nothing will query again.

func (*Default) Options

func (config *Default) Options() []pgdriver.Option

Options returns a copy of the driver options the config was built with. postgres.RunDBTest reads them to derive sibling connections to other databases in the same cluster.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL