Skip to content

Commit a37da3a

Browse files
committed
Add a cached page threshold
1 parent 4c902ff commit a37da3a

3 files changed

Lines changed: 31 additions & 28 deletions

File tree

app/cli_flag.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,22 @@ var (
1414
)
1515

1616
type CliArgs struct {
17-
PgData string
18-
Database string
19-
ConnectString string
20-
Relations []string
21-
PageThreshold int
22-
Limit int
23-
24-
OutputOptions OutputOptions
17+
PgData string
18+
Database string
19+
ConnectString string
20+
Relations []string
21+
PageThreshold int
22+
CachedPageThreshold int
23+
Limit int
24+
25+
OutputOptions
2526
}
2627

2728
func init() {
2829
flag.StringVar(&cliArgs.PgData, "pgData", "", "Location of pgdata, uses PGDATA env var if not defined")
2930
flag.StringVar(&cliArgs.ConnectString, "connect_str", "", "Connection string to PostgreSQL")
30-
flag.IntVar(&cliArgs.PageThreshold, "page_threshold", 10, "Exclude relations with less pages than the threshold")
31+
flag.IntVar(&cliArgs.PageThreshold, "page_threshold", 0, "Exclude relations pages under the threshold. -1 to display everything")
32+
flag.IntVar(&cliArgs.CachedPageThreshold, "cached_page_threshold", 0, "Exclude relations with cached pages under the threshold. -1 to display everything")
3133
flag.StringVar(&relationsFlag, "relations", "", "Filter on a specific relations (separated with commas)")
3234
}
3335

app/pg_pagecache.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ import (
1616
)
1717

1818
type PgPagecache struct {
19-
conn *pgx.Conn
20-
pgData string
21-
OutputOptions OutputOptions
19+
CliArgs
20+
conn *pgx.Conn
2221

2322
dbid uint32
2423
database string
@@ -43,7 +42,7 @@ func extractRelfilenode(filename string) (relfilenode uint32, err error) {
4342

4443
// fillPcStats iterate over fileToRelinfo and fetch page cache stats
4544
func (p *PgPagecache) fillPcStats() error {
46-
baseDir := fmt.Sprintf("%s/base/%d", p.pgData, p.dbid)
45+
baseDir := fmt.Sprintf("%s/base/%d", p.PgData, p.dbid)
4746
entries, err := os.ReadDir(baseDir)
4847
if err != nil {
4948
return fmt.Errorf("Error listing file: %v", err)
@@ -95,31 +94,29 @@ func (p *PgPagecache) fillPcStats() error {
9594
}
9695

9796
// NewPgPagecache fetches the active database id and name and creates the PgPagecache instance
98-
func NewPgPagecache(ctx context.Context, conn *pgx.Conn, cliArgs CliArgs) (pgPagecache PgPagecache, err error) {
97+
func NewPgPagecache(conn *pgx.Conn, cliArgs CliArgs) (pgPagecache PgPagecache, err error) {
9998
pgPagecache.conn = conn
100-
pgPagecache.pgData = cliArgs.PgData
101-
pgPagecache.OutputOptions = cliArgs.OutputOptions
99+
pgPagecache.CliArgs = cliArgs
100+
return
101+
}
102102

103+
func (p *PgPagecache) Run(ctx context.Context) (err error) {
103104
// Fetch dbid and database
104-
err = conn.QueryRow(ctx, "select oid, datname from pg_database where datname=current_database()").Scan(&pgPagecache.dbid, &pgPagecache.database)
105+
err = p.conn.QueryRow(ctx, "select oid, datname from pg_database where datname=current_database()").Scan(&p.dbid, &p.database)
105106
if err != nil {
106107
err = fmt.Errorf("Error getting current database: %v\n", err)
107108
return
108109
}
109-
slog.Debug("Fetched database details", "database", pgPagecache.database, "dbid", pgPagecache.dbid)
110+
slog.Info("Fetched database details", "database", p.database, "dbid", p.dbid)
110111

111112
// Fill the file -> relinfo map
112-
pgPagecache.fileToRelinfo, err = relation.GetFileToRelinfo(ctx, conn, cliArgs.Relations, cliArgs.PageThreshold)
113+
p.fileToRelinfo, err = relation.GetFileToRelinfo(ctx, p.conn, p.Relations, p.PageThreshold)
113114
if err != nil {
114115
err = fmt.Errorf("Error getting file to relation mapping: %v\n", err)
115116
return
116117
}
117-
slog.Debug("Fetched fileToRelinfo", "length", len(pgPagecache.fileToRelinfo))
118-
119-
return
120-
}
118+
slog.Info("Fetched fileToRelinfo", "length", len(p.fileToRelinfo))
121119

122-
func (p *PgPagecache) Run() (err error) {
123120
// Go through all known file and fill their PcStats
124121
err = p.fillPcStats()
125122
if err != nil {
@@ -128,8 +125,12 @@ func (p *PgPagecache) Run() (err error) {
128125

129126
// Build the relname -> relinfo map
130127
p.relToRelinfo = make(relation.RelToRelinfo, 0)
131-
for _, v := range p.fileToRelinfo {
132-
p.relToRelinfo[v.Relname] = v
128+
for k, v := range p.fileToRelinfo {
129+
if v.PcStats.PageCached <= p.CachedPageThreshold {
130+
delete(p.fileToRelinfo, k)
131+
} else {
132+
p.relToRelinfo[v.Relname] = v
133+
}
133134
}
134135

135136
p.OutputResults()

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ func main() {
3131
defer conn.Close(ctx)
3232

3333
// Build PgPagecache struct
34-
pgPagecache, err := app.NewPgPagecache(ctx, conn, cliArgs)
34+
pgPagecache, err := app.NewPgPagecache(conn, cliArgs)
3535
if err != nil {
3636
slog.Any("error", err)
3737
os.Exit(1)
3838
}
3939

4040
// Run it
41-
err = pgPagecache.Run()
41+
err = pgPagecache.Run(ctx)
4242
if err != nil {
4343
slog.Any("error", err)
4444
os.Exit(1)

0 commit comments

Comments
 (0)