postgrespresets

package
v0.65.0 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package postgrespresets builds the PostgreSQL connection pools behind github.com/a-novel-kit/golib/postgres.Config, from a URL or from discrete connection settings.

Index

Constants

View Source
const (

	// 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 and discards it on release. Each opening costs a TCP round
	// trip, a TLS handshake and an authentication exchange. An idle connection costs
	// a file descriptor here and a backend on the server, and ten stays well under a
	// stock max_connections of 100 once multiplied by a service's replica count.
	MaxIdleConnsDefault = 10
)

Variables

View Source
var ErrConnectionPassword = errors.New("postgres password is empty")

ErrConnectionPassword is returned by Connection.Options when the discrete fields are selected without a password.

Functions

This section is empty.

Types

type Connection added in v0.40.0

type Connection struct {
	// DSN is the connection URL, read only when Host is empty.
	DSN string

	// Host is the server hostname or IP address.
	Host string
	// Port is the server port.
	Port int
	// User is the login role.
	User string
	// Password is the login credential. It is required when Host is set.
	Password string
	// Database is the database name.
	Database string
	// TLSEnabled encrypts the connection. Disable it only when another trusted boundary
	// protects the database link.
	TLSEnabled bool

	// DialTimeout bounds how long establishing one connection may take, whichever fields
	// select the server. Zero keeps the driver default.
	DialTimeout time.Duration
}

A Connection describes how to reach PostgreSQL, for NewDefault. A non-empty Host selects the discrete fields, so an orchestrator can inject the password from a secret store without assembling a credential-bearing URL. An empty Host falls back to DSN.

func (Connection) Options added in v0.40.0

func (connection Connection) Options() ([]pgdriver.Option, error)

Options returns the driver options that connect as described, ready for NewDefault.

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 when a pool opens. Pools are cached, so changing the field
	// after the first connection has no effect.
	MaxOpenConns int
	// contains filtered or unexported fields
}

Default is the standard postgres.Config implementation, backed by pgdriver. It opens pools lazily and caches them, one for the primary database and one per requested schema. It is safe for concurrent use.

PostgreSQL ends every session when it restarts or fails over. The pools drop each connection the server closed while it sat idle, so the next query runs on a live one.

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.

The name is a quoted identifier, taken as written: case included, and no character of it can end the statement.

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 closes the pool cached for it. postgrestest.RunIsolatedTransactionalTest drops its throwaway schema with it, so neither the schema nor its idle connections outlive the test.

func (*Default) Options

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

Options returns a copy of the driver options the config was built with. `postgrestest.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