Documentation
¶
Overview ¶
Pogo is a lightweight Go library for querying a subset of PostgreSQL's internal states. It focuses on the data that are highly dynamic in nature, and provides some convenience on recursivley joining some system catalogue relations and function calls on the fly. Currently it supports PostgreSQL version 9.6 and 13 only.
In particular, it supports querying on the following relations:
pg_locks pg_stat_activity pg_stat_replication pg_stat_ssl pg_stat_gssapi pg_stat_wal_receiver pg_stat_subscription pg_stat_database pg_stat_database_conflicts pg_stat_user_tables pg_stat_user_indexes pg_statio_user_indexes pg_statio_user_sequences pg_statio_user_tables pg_stat_user_tables pg_stat_user_functions pg_stat_archiver pg_stat_bgwriter pg_stat_slru
Among these relations, the following four relations are supported as "primary" query targets (i.e. pogo provides functions that query them directly and give you the resulting rows as defined structs)
pg_locks pg_stat_activity pg_stat_replication pg_stat_user_tables
Examples:
Querying relations other than the four primary supported targets (querying pg_stat_database view joining with pg_stat_activity view and pg_locks view, and pg_stat_activity view in turn includes pg_blocking_pids(pid)):
rows, err := pogo.Query(sql.DB).For( pogo.StatDatabaseView.With( pogo.StatActivityView.With( pogo.BlockingPIDs ), pogo.LocksView, ), )
Querying pg_stat_activity for Postgres 13, joining with pg_locks:
statActivities, err := pogo.Query(sql.DB).StatActivity13( pogo.LocksView, )
The currently supported joins are as follows:
pg_stat_activity - pg_locks (on pid) - pg_locks (on backend_xid) - pg_stat_ssl (on pid) - pg_stat_gssapi (on pid) - pg_stat_wal_receiver (on pid) - pg_stat_subscription (on pid) - pg_stat_database (on pid) - pg_stat_database_conflicts (on pid) - pg_blocking_pids(pg_stat_activity.pid) pg_stat_replication - pg_locks (on pid) - pg_stat_ssl (on pid) - pg_stat_gssapi (on pid) - pg_stat_wal_receiver (on pid) pg_stat_user_tables - pg_locks (on relid) - pg_stat_user_indexes (on relid) - pg_stat_subscription (on relid) - pg_statio_user_indexes (on relid) - pg_statio_user_sequences (on relid) - pg_statio_user_tables (on relid) pg_locks - pg_stat_activity (on pid) - pg_stat_database (on database) - pg_stat_user_tables (on relation) - pg_stat_user_indexes (on relation) - pg_statio_user_tables (on relation) - pg_statio_user_indexes (on relation) - pg_statio_user_sequences (on relation) pg_stat_ssl - pg_locks (on pid) - pg_stat_activity (on pid) pg_stat_gssapi - pg_locks (on pid) - pg_stat_activity (on pid) pg_stat_wal_receiver - pg_locks (on pid) - pg_stat_activity (on pid) pg_stat_subscription - pg_locks (on pid) - pg_stat_activity (on pid) pg_stat_database - pg_database_conflicts (on datid) - pg_locks (on datid) - pg_stat_activity (on datid) pg_stat_database_conflicts pg_stat_user_indexes - pg_stat_user_tables (on relid) - pg_statio_user_tables (on relid) - pg_locks (on indexrelid) - pg_statio_user_indexes (on indexrelid) pg_statio_user_indexes pg_statio_user_sequences pg_statio_user_tables pg_stat_user_tables pg_stat_user_functions pg_stat_archiver pg_stat_bgwriter pg_stat_slru
You can join recursively with any depth you want (as long as there are no cycles), although high depth will incur performance hit. Use at your own risk.
Index ¶
- Variables
- func SetPostgresVersion(v PostgresVersion) error
- type PostgresVersion
- type QueryRunner
- func (qr QueryRunner) For(queryable query.Queryable) (*sql.Rows, error)
- func (qr QueryRunner) Locks13(where string, joins ...query.Queryable) ([]postgres13.LockJoined, error)
- func (qr QueryRunner) Locks9(where string, joins ...query.Queryable) ([]postgres9.LockJoined, error)
- func (qr QueryRunner) StatActivity13(where string, joins ...query.Queryable) ([]postgres13.StatActivityJoined, error)
- func (qr QueryRunner) StatActivity9(where string, joins ...query.Queryable) ([]postgres9.StatActivityJoined, error)
- func (qr QueryRunner) StatReplication13(where string, joins ...query.Queryable) ([]postgres13.StatReplicationJoined, error)
- func (qr QueryRunner) StatReplication9(where string, joins ...query.Queryable) ([]postgres9.StatReplicationJoined, error)
- func (qr QueryRunner) StatTable13(where string, joins ...query.Queryable) ([]postgres13.StatTableJoined, error)
- func (qr QueryRunner) StatTable9(where string, joins ...query.Queryable) ([]postgres9.StatTableJoined, error)
- func (qr QueryRunner) Tuple(ctx context.Context, args TupleArgs) (null.String, error)
- type Queryor
- type Scannable
- type TupleArgs
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( LocksView = query.Queryable{Target: query.TargetLocks} LocksOnTxIDView = query.Queryable{Target: query.TargetLocksOnTxID} StatActivityView = query.Queryable{Target: query.TargetStatActivity} StatReplicationView = query.Queryable{Target: query.TargetStatReplication} StatSSLView = query.Queryable{Target: query.TargetStatSSL} StatGSSAPIView = query.Queryable{Target: query.TargetStatGSSAPI} StatWALReceiverView = query.Queryable{Target: query.TargetStatWALReceiver} StatSubscriptionView = query.Queryable{Target: query.TargetStatSubscription} StatDatabaseView = query.Queryable{Target: query.TargetStatDatabase} StatDatabaseConflictsView = query.Queryable{Target: query.TargetStatDatabaseConflicts} StatUserTablesView = query.Queryable{Target: query.TargetStatUserTables} StatUserIndexesView = query.Queryable{Target: query.TargetStatUserIndexes} StatIOUserIndexesView = query.Queryable{Target: query.TargetStatIOUserIndexes} StatIOUserSequencesView = query.Queryable{Target: query.TargetStatIOUserSequences} StatIOUserTablesView = query.Queryable{Target: query.TargetStatIOUserTables} StatUserFunctionsView = query.Queryable{Target: query.TargetStatUserFunctions} StatArchiverView = query.Queryable{Target: query.TargetStatArchiver} StatBGWriterView = query.Queryable{Target: query.TargetStatBGWriter} StatSLRUView = query.Queryable{Target: query.TargetStatSLRU} )
Queryable views:
var (
BlockingPIDs = query.Queryable{Target: query.TargetBlockingPIDs, SelectOnly: true}
)
Non-relation queryables:
Functions ¶
func SetPostgresVersion ¶
func SetPostgresVersion(v PostgresVersion) error
SetPostgresVersion sets the Postgres version and locks it down. You can set the version only once. Trying to set it again will return a non-nil error and won't have any effects.
Types ¶
type PostgresVersion ¶
type PostgresVersion version.PostgresVersion
PostgresVersion represents the version of Postgres pogo will use. Currently supports 9.6 and 13
const ( Postgres9 PostgresVersion = PostgresVersion(version.Postgres9) Postgres13 = PostgresVersion(version.Postgres13) )
Postgres version enums
func GetPostgresVersion ¶
func GetPostgresVersion() PostgresVersion
GetPostgresVersion reports the current Postgres version pogo is using.
func (PostgresVersion) String ¶
func (v PostgresVersion) String() string
String representation of Postgres version.
type QueryRunner ¶
type QueryRunner struct {
// contains filtered or unexported fields
}
QueryRunner is an encapsulation for running a single query.
func Query ¶
func Query(queryor Queryor) QueryRunner
Query prepares a query running instance. Note that it does not run a query by itself.
Example ¶
package main import ( "github.com/sanggonlee/pogo" ) func main() { var db pogo.Queryor rows, _ := pogo.Query(db).For( pogo.StatDatabaseView.With( pogo.StatActivityView.With( pogo.BlockingPIDs, ), pogo.LocksView, ), ) defer rows.Close() for rows.Next() { // ... } }
Output:
func QueryContext ¶
func QueryContext(ctx context.Context, queryor Queryor) QueryRunner
QueryContext is like Query, except it also takes a context.
func (QueryRunner) For ¶
For is used to run a query on arbitrary relations. It returns sql.Rows, which you can scan to the structure you see fit.
func (QueryRunner) Locks13 ¶
func (qr QueryRunner) Locks13(where string, joins ...query.Queryable) ([]postgres13.LockJoined, error)
Locks13 is a convenience method for running a query on pg_locks view. It is meant to be used for Postgres v13. If you want to select rows with certain conditions, pass a non-empty where argument, which will be injected as WHERE {where} in the query.
func (QueryRunner) Locks9 ¶
func (qr QueryRunner) Locks9(where string, joins ...query.Queryable) ([]postgres9.LockJoined, error)
Locks9 is a convenience method for running a query on pg_locks view. It is meant to be used for Postgres v9.6. If you want to select rows with certain conditions, pass a non-empty where argument, which will be injected as WHERE {where} in the query.
func (QueryRunner) StatActivity13 ¶
func (qr QueryRunner) StatActivity13(where string, joins ...query.Queryable) ([]postgres13.StatActivityJoined, error)
StatActivity13 is a convenience method for running a query on pg_stat_activity view. It is meant to be used for Postgres v13. If you want to select rows with certain conditions, pass a non-empty where argument, which will be injected as WHERE {where} in the query.
func (QueryRunner) StatActivity9 ¶
func (qr QueryRunner) StatActivity9(where string, joins ...query.Queryable) ([]postgres9.StatActivityJoined, error)
StatActivity9 is a convenience method for running a query on pg_stat_activity view. It is meant to be used for Postgres v9.6. If you want to select rows with certain conditions, pass a non-empty where argument, which will be injected as WHERE {where} in the query.
func (QueryRunner) StatReplication13 ¶
func (qr QueryRunner) StatReplication13(where string, joins ...query.Queryable) ([]postgres13.StatReplicationJoined, error)
StatReplication13 is a convenience method for running a query on pg_stat_replication view. It is meant to be used for Postgres v13. If you want to select rows with certain conditions, pass a non-empty where argument, which will be injected as WHERE {where} in the query.
func (QueryRunner) StatReplication9 ¶
func (qr QueryRunner) StatReplication9(where string, joins ...query.Queryable) ([]postgres9.StatReplicationJoined, error)
StatReplication9 is a convenience method for running a query on pg_stat_replication view. It is meant to be used for Postgres v9.6. If you want to select rows with certain conditions, pass a non-empty where argument, which will be injected as WHERE {where} in the query.
func (QueryRunner) StatTable13 ¶
func (qr QueryRunner) StatTable13(where string, joins ...query.Queryable) ([]postgres13.StatTableJoined, error)
StatTable13 is a convenience method for running a query on pg_stat_user_tables view. It is meant to be used for Postgres v13. If you want to select rows with certain conditions, pass a non-empty where argument, which will be injected as WHERE {where} in the query.
func (QueryRunner) StatTable9 ¶
func (qr QueryRunner) StatTable9(where string, joins ...query.Queryable) ([]postgres9.StatTableJoined, error)
StatTable9 is a convenience method for running a query on pg_stat_user_tables view. It is meant to be used for Postgres v9.6. If you want to select rows with certain conditions, pass a non-empty where argument, which will be injected as WHERE {where} in the query.
func (QueryRunner) Tuple ¶
func (qr QueryRunner) Tuple(ctx context.Context, args TupleArgs) (null.String, error)
Tuple runs a query on a specific relation matching the ctid given by a pair (page,tuple) and returns the resulting row as a JSON string, with its properties mapping to the columns. Note that tuples retrieved this way are no longer consistent after VACUUM FULL.
type Queryor ¶
type Queryor interface { // Query is used to query the database. Query(query string, args ...interface{}) (*sql.Rows, error) // QueryContext is used to query the database with a context. QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) }
Queryor is an interface for general Postgres connection pool. sql.DB, sql.Stmt, sql.Tx, and other extensions to the Go sql package provide abstractions that implement this.
type Scannable ¶
type Scannable interface { // ScanDestinations returns all the scan destinations of the // receiver, along with the destinations of all the joined relations, // which are given as its arguments. ScanDestinations([]query.Queryable) []interface{} }
Scannable describes objects that qualify for Postgres query destinations
type TupleArgs ¶
type TupleArgs struct { // Name of the relation. Corresponds to "relname" column in pg_stat_user_tables view. RelName string // Page of the tuple to query for. Page int64 // Tuple index of the tuple to query for. Tuple int64 }
TupleArgs specifies the arguments to feed when querying for a tuple.