Skip to content

Commit

Permalink
add ListTableSizes and GetSize db method helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kataras committed Oct 13, 2023
1 parent 6f9c0d5 commit 84634dd
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
24 changes: 24 additions & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,27 @@ func QuerySingle[T any](ctx context.Context, db *DB, query string, args ...any)
err = db.QueryRow(ctx, query, args...).Scan(&entry)
return
}

func scanQuery[T any](ctx context.Context, db *DB, scanner func(rows Rows) (T, error), query string, args ...any) ([]T, error) {
rows, err := db.Query(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var list []T

for rows.Next() {
entry, err := scanner(rows)
if err != nil {
return nil, err
}

list = append(list, entry)
}

if err = rows.Err(); err != nil && err != ErrNoRows {
return nil, err
}

return list, nil
}
58 changes: 58 additions & 0 deletions db_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,64 @@ func (db *DB) GetVersion(ctx context.Context) (string, error) {
return versionNumber, nil // return the version number and nil as no error occurred
}

// SizeInfo is a struct which contains the size information (for individual table or the whole database).
type SizeInfo struct {
SizePretty string `json:"size_pretty"`
// The on-disk size in bytes of one fork of that relation.
// A fork is a variant of the main data file that stores additional information,
// such as the free space map, the visibility map, or the initialization fork.
// By default, this is the size of the main data fork only.
Size float64 `json:"size"`

SizeTotalPretty string `json:"size_total_pretty"`
// The total on-disk space used for that table, including all associated indexes. This is equivalent to pg_table_size + pg_indexes_size.
SizeTotal float64 `json:"size_total"`
}

// TableSizeInfo is a struct which contains the table size information used as an output parameter of the `db.ListTableSizes` method.
type TableSizeInfo struct {
TableName string `json:"table_name"`
SizeInfo
}

// ListTableSizes lists the disk size of tables (not only the registered ones) in the database.
func (db *DB) ListTableSizes(ctx context.Context) ([]TableSizeInfo, error) {
query := `SELECT
table_name,
pg_size_pretty(pg_relation_size(quote_ident(table_name))) AS size_pretty,
pg_relation_size(quote_ident(table_name)) AS size,
pg_size_pretty(pg_total_relation_size(quote_ident(table_name))) AS size_total_pretty,
pg_total_relation_size(quote_ident(table_name)) AS size_total
FROM information_schema.tables
WHERE table_schema = $1
ORDER BY 3 DESC;`

return scanQuery[TableSizeInfo](ctx, db, func(rows Rows) (t TableSizeInfo, err error) {
err = rows.Scan(&t.TableName, &t.SizePretty, &t.Size, &t.SizeTotalPretty, &t.SizeTotal)
return
}, query, db.searchPath)
}

// GetSize returns the sum of size of all the database tables.
func (db *DB) GetSize(ctx context.Context) (t SizeInfo, err error) {
query := `SELECT
pg_size_pretty(SUM(size)) AS size_pretty,
SUM(size) AS size, pg_size_pretty(SUM(size_total)) AS size_total_pretty,
SUM(size_total) AS size_total
FROM (
SELECT
table_name,
pg_relation_size(quote_ident(table_name)) AS size,
pg_total_relation_size(quote_ident(table_name)) AS size_total
FROM information_schema.tables
WHERE table_schema = $1
ORDER BY 1 DESC
) f;`

err = db.QueryRow(ctx, query, db.searchPath).Scan(&t.SizePretty, &t.Size, &t.SizeTotalPretty, &t.SizeTotal)
return
}

// MapTypeFilter is a map of expressions inputs text to field type.
// It's a TableFilter.
//
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ go 1.21
require (
github.com/gertd/go-pluralize v0.2.1
github.com/jackc/pgx/v5 v5.4.3
golang.org/x/mod v0.12.0
golang.org/x/mod v0.13.0
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
golang.org/x/crypto v0.13.0 // indirect
golang.org/x/sync v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/sync v0.4.0 // indirect
golang.org/x/text v0.13.0 // indirect
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/crypto v0.13.0 h1:mvySKfSWJ+UKUii46M40LOvyWfN0s2U+46/jDd0e6Ck=
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ=
golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k=
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit 84634dd

Please sign in to comment.