postgres

package
v1.16.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: GPL-3.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package postgres connects to and interacts with a PostgreSQL database server. The functions are specific to the Postgres platform rather than more generic or interchangeable SQL statements.

The postgres/models directory is generated by SQLBoiler and should not be modified.

Index

Constants

View Source
const (
	// DefaultURL is an example PostgreSQL connection string, it must not be used in production.
	DefaultURL = "postgres://root:example@localhost:5432/defacto2_ps" //nolint: gosec
	// DriverName of the database.
	DriverName = "pgx"
	// Protocol of the database driver.
	Protocol = "postgres"
)
View Source
const (
	// TotalCnt is a partial SQL statement to count the number of records.
	TotalCnt = "COUNT(*) AS count_total"
	// SumSize is a partial SQL statement to sum the file size values of multiple records.
	SumSize = "SUM(filesize) AS size_total"
	// MinYear is a partial SQL statement to select the minimum year value.
	MinYear = "MIN(date_issued_year) AS min_year"
	// MaxYear is a partial SQL statement to select the maximum year value.
	MaxYear = "MAX(date_issued_year) AS max_year"
	// SelVersion is a SQL statement to select the version of the PostgreSQL database server in use.
	SelVersion = "SELECT version();"
)

Variables

View Source
var ErrEnvValue = errors.New("postgres: environment variable probably contains an invalid value")

Functions

func Connections added in v0.8.0

func Connections(ctx context.Context, db *sql.DB) (active, maxConn int64, err error)

Connections returns the number of active connections and the maximum allowed connections. Negative values are returned if there is an error.

func NumSuffixes added in v1.14.0

func NumSuffixes() string

NumSuffixes is an SQL statement to list files with numeric suffixes.

func Open added in v0.10.0

func Open() (*sql.DB, error)

Open a new connection to the PostgreSQL database. Only one connection is needed for the entire application as it is thread-safe and can be used repeatedly.

The connection should be closed after the application exits.

func ScenerSQL

func ScenerSQL(name string) (string, []any)

ScenerSQL returns a parameterized SQL query for getting sceners and their parameters. This prevents SQL injection by separating the query structure from user input.

func SetFilesize0

func SetFilesize0() string

SetFilesize0 is an SQL statement to update filesize column NULLs with 0 values. This is a fix for the error: failed to bind pointers to obj: sql: Scan error on column index 2, name "size_total": converting NULL to int is unsupported.

func SetUpper

func SetUpper(column string) string

SetUpper is an SQL statement to update a column with trimmed uppercase values.

Types

type Connection

type Connection struct {
	URL string `env:"D2_DATABASE_URL"` // removing this value will cause the default to be used after a single use
}

Connection details of the PostgreSQL database connection.

func New

func New() (Connection, error)

New initializes the connection with default values or values from the environment.

func (Connection) Validate

func (c Connection) Validate(sl *slog.Logger) error

Validate the connection URL and print any issues to the logger.

type IndexStat added in v1.16.1

type IndexStat struct {
	Name       string
	Method     string
	Primary    bool
	Valid      bool
	Partial    bool
	SizeBytes  int64
	Size       string
	Scans      int64
	TupRead    int64
	TupFetch   int64
	BlocksHit  int64
	BlocksRead int64
	LastScan   *time.Time
	Definition string
}

IndexStat holds live PostgreSQL catalog and usage statistics for a single index.

func (IndexStat) CacheHitPct added in v1.16.1

func (s IndexStat) CacheHitPct() string

CacheHitPct returns the shared-buffer cache hit percentage for this index.

func (IndexStat) ExprSummary added in v1.16.1

func (s IndexStat) ExprSummary() string

ExprSummary strips the CREATE INDEX ... USING <method> prefix and noisy PostgreSQL internal type casts from pg_get_indexdef for compact display in admin tables.

func (IndexStat) LastScanString added in v1.16.1

func (s IndexStat) LastScanString() string

LastScanString returns the formatted timestamp of the last index scan, or "never".

func (IndexStat) ScansFormatted added in v1.16.1

func (s IndexStat) ScansFormatted() string

ScansFormatted returns Scans formatted with thousand separators.

func (IndexStat) TupFetchFormatted added in v1.16.1

func (s IndexStat) TupFetchFormatted() string

TupFetchFormatted returns TupFetch formatted with thousand separators.

func (IndexStat) TupReadFormatted added in v1.16.1

func (s IndexStat) TupReadFormatted() string

TupReadFormatted returns TupRead formatted with thousand separators.

type Role

type Role string // Role is a scener attribution used in the database.
const (
	Writer   Role = "(upper(credit_text))"         // Writer or author of a document.
	Artist   Role = "(upper(credit_illustration))" // Artist or illustrator of an image or artwork.
	Coder    Role = "(upper(credit_program))"      // Coder or programmer of a program or application.
	Musician Role = "(upper(credit_audio))"        // Musician or composer of a music or audio track.
)

func Roles

func Roles() Role

Roles returns all of the sceners reguardless of the attribution.

func (Role) Distinct

func (r Role) Distinct() SQL

type SQL

type SQL string // SQL is a raw query statement for PostgreSQL.

func AllReleasersCounts added in v1.15.4

func AllReleasersCounts() SQL

AllReleasersCounts selects all distinct releasers (groups, BBS, FTP) and their public file counts.

func Artists

func Artists() SQL

Artists selects a list of distinct artists.

func BBSsAlphabetical

func BBSsAlphabetical() SQL

BBSsAlphabetical selects a list of distinct BBS names.

func BBSsAlphabeticalTwo added in v1.15.4

func BBSsAlphabeticalTwo() SQL

BBSsAlphabeticalTwo selects a list of distinct BBS names with 2 or more artifacts.

func BBSsOldest

func BBSsOldest() SQL

BBSsOldest selects a list of distinct releasers or groups, only showing BBS sites and ordered by the file count.

func BBSsProlific

func BBSsProlific() SQL

BBSsProlific selects a list of distinct releasers or groups, only showing BBS sites with 2 or more artifacts and ordered by the file count.

func Coders

func Coders() SQL

Coders selects a list of distinct coders.

func FTPsAlphabetical

func FTPsAlphabetical() SQL

FTPsAlphabetical selects a list of distinct FTP site names.

func MagazinesAlphabetical

func MagazinesAlphabetical() SQL

MagazinesAlphabetical selects a list of distinct magazine titles.

func MagazinesOldest

func MagazinesOldest() SQL

MagazinesOldest selects a list of distinct releasers or groups, only showing magazines and ordered by the file count.

func Musicians

func Musicians() SQL

Musicians selects a list of distinct musicians.

func Releasers

func Releasers() SQL

func ReleasersAlphabetical

func ReleasersAlphabetical() SQL

ReleasersAlphabetical selects a list of distinct releasers or groups, excluding BBS and FTP sites.

func ReleasersAlphabeticalTwo added in v1.15.4

func ReleasersAlphabeticalTwo() SQL

ReleasersAlphabeticalTwo selects a list of distinct releasers or groups with 2 or more artifacts, excluding BBS and FTP sites.

func ReleasersOldest

func ReleasersOldest() SQL

ReleasersOldest selects a list of distinct releasers or groups, excluding BBS and FTP sites and ordered by the oldest year.

func ReleasersProlific

func ReleasersProlific() SQL

ReleasersProlific selects a list of distinct releasers or groups, excluding BBS and FTP sites and ordered by the file count (2 or more artifacts).

func Sceners

func Sceners() SQL

Sceners selects a list of distinct sceners.

func SimilarToExact added in v1.7.7

func SimilarToExact(like ...string) (SQL, []any)

SimilarToExact returns a parameterized SQL query for exact releaser matches.

func SimilarToMagazine

func SimilarToMagazine(like ...string) (SQL, []any)

SimilarToMagazine returns a parameterized SQL query for finding similar magazines.

func SimilarToReleaser

func SimilarToReleaser(like ...string) (SQL, []any)

SimilarToReleaser returns a parameterized SQL query for finding similar releasers.

func SuggestMagazinesCounts added in v1.16.1

func SuggestMagazinesCounts() SQL

SuggestMagazinesCounts selects distinct magazine titles and counts across public and pending-approval records.

func SuggestReleasersCounts added in v1.16.1

func SuggestReleasersCounts() SQL

SuggestReleasersCounts selects distinct releasers and counts across public and pending-approval records (excluding records explicitly deleted by a user).

func SumGroup

func SumGroup() SQL

SumGroup is an SQL statement to sum the filesizes of public records matching the group.

func SumPlatform

func SumPlatform() SQL

SumPlatform is an SQL statement to sum the filesizes of public records matching the platform.

func SumSection

func SumSection() SQL

SumSection is an SQL statement to sum the filesizes of public records matching the section.

func Summary

func Summary() SQL

Summary is an SQL statement to count the number of files, sum the filesize, select the oldest year and the newest year.

func Writers

func Writers() SQL

Writers selects a list of distinct writers.

type TableIndexStats added in v1.16.1

type TableIndexStats struct {
	TableName   string
	TableSize   string
	IndexesSize string
	TotalSize   string
	SeqScan     int64
	IdxScan     int64
	LiveTuples  int64
	DeadTuples  int64
	Updates     int64
	HotUpdates  int64
	LastVacuum  *time.Time
	LastAnalyze *time.Time
	StatsReset  *time.Time
	Indexes     []IndexStat
}

TableIndexStats holds table-level storage/scan/HOT/vacuum summary alongside all active indexes.

func Indexes added in v1.16.1

func Indexes(ctx context.Context, db *sql.DB) (TableIndexStats, error)

Indexes queries PostgreSQL catalog and usage statistics for all active indexes on the files table.

func (TableIndexStats) DeadTuplesFormatted added in v1.16.1

func (t TableIndexStats) DeadTuplesFormatted() string

func (TableIndexStats) HotUpdatePct added in v1.16.1

func (t TableIndexStats) HotUpdatePct() string

HotUpdatePct returns the percentage of UPDATEs handled as Heap-Only Tuple (HOT) updates.

func (TableIndexStats) HotUpdatesFormatted added in v1.16.1

func (t TableIndexStats) HotUpdatesFormatted() string

func (TableIndexStats) IdxScanFormatted added in v1.16.1

func (t TableIndexStats) IdxScanFormatted() string

func (TableIndexStats) IdxScanPct added in v1.16.1

func (t TableIndexStats) IdxScanPct() string

IdxScanPct returns the percentage of table scans served by indexes.

func (TableIndexStats) LastAnalyzeString added in v1.16.1

func (t TableIndexStats) LastAnalyzeString() string

LastAnalyzeString returns the formatted timestamp of the last analyze/autoanalyze.

func (TableIndexStats) LastVacuumString added in v1.16.1

func (t TableIndexStats) LastVacuumString() string

LastVacuumString returns the formatted timestamp of the last vacuum/autovacuum.

func (TableIndexStats) LiveTuplesFormatted added in v1.16.1

func (t TableIndexStats) LiveTuplesFormatted() string

func (TableIndexStats) SeqScanFormatted added in v1.16.1

func (t TableIndexStats) SeqScanFormatted() string

func (TableIndexStats) StatsResetString added in v1.16.1

func (t TableIndexStats) StatsResetString() string

StatsResetString returns the formatted timestamp of the last database stats reset.

func (TableIndexStats) UpdatesFormatted added in v1.16.1

func (t TableIndexStats) UpdatesFormatted() string

type Version

type Version string // Version of the PostgreSQL database server in use.

func (*Version) Query

func (v *Version) Query(db *sql.DB) error

Query the database version.

func (*Version) String

func (v *Version) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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