-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
client.go
738 lines (662 loc) · 20.7 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package postgresqlreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/postgresqlreceiver"
import (
"context"
"database/sql"
"errors"
"fmt"
"net"
"strconv"
"strings"
"time"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/config/configtls"
"go.opentelemetry.io/collector/featuregate"
"go.uber.org/multierr"
)
const lagMetricsInSecondsFeatureGateID = "postgresqlreceiver.preciselagmetrics"
var preciseLagMetricsFg = featuregate.GlobalRegistry().MustRegister(
lagMetricsInSecondsFeatureGateID,
featuregate.StageBeta,
featuregate.WithRegisterDescription("Metric `postgresql.wal.lag` is replaced by more precise `postgresql.wal.delay`."),
featuregate.WithRegisterFromVersion("0.89.0"),
)
// databaseName is a name that refers to a database so that it can be uniquely referred to later
// i.e. database1
type databaseName string
// tableIdentifier is an identifier that contains both the database and table separated by a "|"
// i.e. database1|table2
type tableIdentifier string
// indexIdentifier is a unique string that identifies a particular index and is separated by the "|" character
type indexIdentifer string
// errNoLastArchive is an error that occurs when there is no previous wal archive, so there is no way to compute the
// last archived point
var errNoLastArchive = errors.New("no last archive found, not able to calculate oldest WAL age")
type client interface {
Close() error
getDatabaseStats(ctx context.Context, databases []string) (map[databaseName]databaseStats, error)
getDatabaseLocks(ctx context.Context) ([]databaseLocks, error)
getBGWriterStats(ctx context.Context) (*bgStat, error)
getBackends(ctx context.Context, databases []string) (map[databaseName]int64, error)
getDatabaseSize(ctx context.Context, databases []string) (map[databaseName]int64, error)
getDatabaseTableMetrics(ctx context.Context, db string) (map[tableIdentifier]tableStats, error)
getBlocksReadByTable(ctx context.Context, db string) (map[tableIdentifier]tableIOStats, error)
getReplicationStats(ctx context.Context) ([]replicationStats, error)
getLatestWalAgeSeconds(ctx context.Context) (int64, error)
getMaxConnections(ctx context.Context) (int64, error)
getIndexStats(ctx context.Context, database string) (map[indexIdentifer]indexStat, error)
listDatabases(ctx context.Context) ([]string, error)
getVersion(ctx context.Context) (string, error)
}
type postgreSQLClient struct {
client *sql.DB
closeFn func() error
}
var _ client = (*postgreSQLClient)(nil)
type postgreSQLConfig struct {
username string
password string
database string
address confignet.AddrConfig
tls configtls.ClientConfig
}
func sslConnectionString(tls configtls.ClientConfig) string {
if tls.Insecure {
return "sslmode='disable'"
}
conn := ""
if tls.InsecureSkipVerify {
conn += "sslmode='require'"
} else {
conn += "sslmode='verify-full'"
}
if tls.CAFile != "" {
conn += fmt.Sprintf(" sslrootcert='%s'", tls.CAFile)
}
if tls.KeyFile != "" {
conn += fmt.Sprintf(" sslkey='%s'", tls.KeyFile)
}
if tls.CertFile != "" {
conn += fmt.Sprintf(" sslcert='%s'", tls.CertFile)
}
return conn
}
func (c postgreSQLConfig) ConnectionString() (string, error) {
// postgres will assume the supplied user as the database name if none is provided,
// so we must specify a database name even when we are just collecting the list of databases.
database := defaultPostgreSQLDatabase
if c.database != "" {
database = c.database
}
host, port, err := net.SplitHostPort(c.address.Endpoint)
if err != nil {
return "", err
}
if c.address.Transport == confignet.TransportTypeUnix {
// lib/pg expects a unix socket host to start with a "/" and appends the appropriate .s.PGSQL.port internally
host = "/" + host
}
return fmt.Sprintf("port=%s host=%s user=%s password=%s dbname=%s %s", port, host, c.username, c.password, database, sslConnectionString(c.tls)), nil
}
func (c *postgreSQLClient) Close() error {
if c.closeFn != nil {
return c.closeFn()
}
return nil
}
type databaseStats struct {
transactionCommitted int64
transactionRollback int64
deadlocks int64
tempFiles int64
tupUpdated int64
tupReturned int64
tupFetched int64
tupInserted int64
tupDeleted int64
blksHit int64
blksRead int64
}
func (c *postgreSQLClient) getDatabaseStats(ctx context.Context, databases []string) (map[databaseName]databaseStats, error) {
query := filterQueryByDatabases(
"SELECT datname, xact_commit, xact_rollback, deadlocks, temp_files, tup_updated, tup_returned, tup_fetched, tup_inserted, tup_deleted, blks_hit, blks_read FROM pg_stat_database",
databases,
false,
)
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
var errs error
dbStats := map[databaseName]databaseStats{}
for rows.Next() {
var datname string
var transactionCommitted, transactionRollback, deadlocks, tempFiles, tupUpdated, tupReturned, tupFetched, tupInserted, tupDeleted, blksHit, blksRead int64
err = rows.Scan(&datname, &transactionCommitted, &transactionRollback, &deadlocks, &tempFiles, &tupUpdated, &tupReturned, &tupFetched, &tupInserted, &tupDeleted, &blksHit, &blksRead)
if err != nil {
errs = multierr.Append(errs, err)
continue
}
if datname != "" {
dbStats[databaseName(datname)] = databaseStats{
transactionCommitted: transactionCommitted,
transactionRollback: transactionRollback,
deadlocks: deadlocks,
tempFiles: tempFiles,
tupUpdated: tupUpdated,
tupReturned: tupReturned,
tupFetched: tupFetched,
tupInserted: tupInserted,
tupDeleted: tupDeleted,
blksHit: blksHit,
blksRead: blksRead,
}
}
}
return dbStats, errs
}
type databaseLocks struct {
relation string
mode string
lockType string
locks int64
}
func (c *postgreSQLClient) getDatabaseLocks(ctx context.Context) ([]databaseLocks, error) {
query := `SELECT relname AS relation, mode, locktype,COUNT(pid)
AS locks FROM pg_locks
JOIN pg_class ON pg_locks.relation = pg_class.oid
GROUP BY relname, mode, locktype;`
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("unable to query pg_locks and pg_locks.relation: %w", err)
}
defer rows.Close()
var dl []databaseLocks
var errs []error
for rows.Next() {
var relation, mode, lockType string
var locks int64
err = rows.Scan(&relation, &mode, &lockType, &locks)
if err != nil {
errs = append(errs, err)
continue
}
dl = append(dl, databaseLocks{
relation: relation,
mode: mode,
lockType: lockType,
locks: locks,
})
}
return dl, multierr.Combine(errs...)
}
// getBackends returns a map of database names to the number of active connections
func (c *postgreSQLClient) getBackends(ctx context.Context, databases []string) (map[databaseName]int64, error) {
query := filterQueryByDatabases("SELECT datname, count(*) as count from pg_stat_activity", databases, true)
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
ars := map[databaseName]int64{}
var errors error
for rows.Next() {
var datname string
var count int64
err = rows.Scan(&datname, &count)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
if datname != "" {
ars[databaseName(datname)] = count
}
}
return ars, errors
}
func (c *postgreSQLClient) getDatabaseSize(ctx context.Context, databases []string) (map[databaseName]int64, error) {
query := filterQueryByDatabases("SELECT datname, pg_database_size(datname) FROM pg_catalog.pg_database WHERE datistemplate = false", databases, false)
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
sizes := map[databaseName]int64{}
var errors error
for rows.Next() {
var datname string
var size int64
err = rows.Scan(&datname, &size)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
if datname != "" {
sizes[databaseName(datname)] = size
}
}
return sizes, errors
}
// tableStats contains a result for a row of the getDatabaseTableMetrics result
type tableStats struct {
database string
schema string
table string
live int64
dead int64
inserts int64
upd int64
del int64
hotUpd int64
seqScans int64
size int64
vacuumCount int64
}
func (c *postgreSQLClient) getDatabaseTableMetrics(ctx context.Context, db string) (map[tableIdentifier]tableStats, error) {
query := `SELECT schemaname as schema, relname AS table,
n_live_tup AS live,
n_dead_tup AS dead,
n_tup_ins AS ins,
n_tup_upd AS upd,
n_tup_del AS del,
n_tup_hot_upd AS hot_upd,
seq_scan AS seq_scans,
pg_relation_size(relid) AS table_size,
vacuum_count
FROM pg_stat_user_tables;`
ts := map[tableIdentifier]tableStats{}
var errors error
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
for rows.Next() {
var schema, table string
var live, dead, ins, upd, del, hotUpd, seqScans, tableSize, vacuumCount int64
err = rows.Scan(&schema, &table, &live, &dead, &ins, &upd, &del, &hotUpd, &seqScans, &tableSize, &vacuumCount)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
ts[tableKey(db, schema, table)] = tableStats{
database: db,
schema: schema,
table: table,
live: live,
dead: dead,
inserts: ins,
upd: upd,
del: del,
hotUpd: hotUpd,
seqScans: seqScans,
size: tableSize,
vacuumCount: vacuumCount,
}
}
return ts, errors
}
type tableIOStats struct {
database string
schema string
table string
heapRead int64
heapHit int64
idxRead int64
idxHit int64
toastRead int64
toastHit int64
tidxRead int64
tidxHit int64
}
func (c *postgreSQLClient) getBlocksReadByTable(ctx context.Context, db string) (map[tableIdentifier]tableIOStats, error) {
query := `SELECT schemaname as schema, relname AS table,
coalesce(heap_blks_read, 0) AS heap_read,
coalesce(heap_blks_hit, 0) AS heap_hit,
coalesce(idx_blks_read, 0) AS idx_read,
coalesce(idx_blks_hit, 0) AS idx_hit,
coalesce(toast_blks_read, 0) AS toast_read,
coalesce(toast_blks_hit, 0) AS toast_hit,
coalesce(tidx_blks_read, 0) AS tidx_read,
coalesce(tidx_blks_hit, 0) AS tidx_hit
FROM pg_statio_user_tables;`
tios := map[tableIdentifier]tableIOStats{}
var errors error
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
for rows.Next() {
var schema, table string
var heapRead, heapHit, idxRead, idxHit, toastRead, toastHit, tidxRead, tidxHit int64
err = rows.Scan(&schema, &table, &heapRead, &heapHit, &idxRead, &idxHit, &toastRead, &toastHit, &tidxRead, &tidxHit)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
tios[tableKey(db, schema, table)] = tableIOStats{
database: db,
schema: schema,
table: table,
heapRead: heapRead,
heapHit: heapHit,
idxRead: idxRead,
idxHit: idxHit,
toastRead: toastRead,
toastHit: toastHit,
tidxRead: tidxRead,
tidxHit: tidxHit,
}
}
return tios, errors
}
type indexStat struct {
index string
table string
schema string
database string
size int64
scans int64
}
func (c *postgreSQLClient) getIndexStats(ctx context.Context, database string) (map[indexIdentifer]indexStat, error) {
query := `SELECT schemaname, relname, indexrelname,
pg_relation_size(indexrelid) AS index_size,
idx_scan
FROM pg_stat_user_indexes;`
stats := map[indexIdentifer]indexStat{}
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var errs []error
for rows.Next() {
var (
schema, table, index string
indexSize, indexScans int64
)
err := rows.Scan(&schema, &table, &index, &indexSize, &indexScans)
if err != nil {
errs = append(errs, err)
continue
}
stats[indexKey(database, schema, table, index)] = indexStat{
index: index,
table: table,
schema: schema,
database: database,
size: indexSize,
scans: indexScans,
}
}
return stats, multierr.Combine(errs...)
}
type bgStat struct {
checkpointsReq int64
checkpointsScheduled int64
checkpointWriteTime float64
checkpointSyncTime float64
bgWrites int64
bufferBackendWrites int64
bufferFsyncWrites int64
bufferCheckpoints int64
buffersAllocated int64
maxWritten int64
}
func (c *postgreSQLClient) getBGWriterStats(ctx context.Context) (*bgStat, error) {
version, err := c.getVersion(ctx)
if err != nil {
return nil, err
}
major, err := parseMajorVersion(version)
if err != nil {
return nil, err
}
var (
checkpointsReq, checkpointsScheduled int64
checkpointSyncTime, checkpointWriteTime float64
bgWrites, bufferCheckpoints, bufferAllocated int64
bufferBackendWrites, bufferFsyncWrites, maxWritten int64
)
if major < 17 {
query := `SELECT
checkpoints_req AS checkpoint_req,
checkpoints_timed AS checkpoint_scheduled,
checkpoint_write_time AS checkpoint_duration_write,
checkpoint_sync_time AS checkpoint_duration_sync,
buffers_clean AS bg_writes,
buffers_backend AS backend_writes,
buffers_backend_fsync AS buffers_written_fsync,
buffers_checkpoint AS buffers_checkpoints,
buffers_alloc AS buffers_allocated,
maxwritten_clean AS maxwritten_count
FROM pg_stat_bgwriter;`
row := c.client.QueryRowContext(ctx, query)
if err = row.Scan(
&checkpointsReq,
&checkpointsScheduled,
&checkpointWriteTime,
&checkpointSyncTime,
&bgWrites,
&bufferBackendWrites,
&bufferFsyncWrites,
&bufferCheckpoints,
&bufferAllocated,
&maxWritten,
); err != nil {
return nil, err
}
return &bgStat{
checkpointsReq: checkpointsReq,
checkpointsScheduled: checkpointsScheduled,
checkpointWriteTime: checkpointWriteTime,
checkpointSyncTime: checkpointSyncTime,
bgWrites: bgWrites,
bufferBackendWrites: bufferBackendWrites,
bufferFsyncWrites: bufferFsyncWrites,
bufferCheckpoints: bufferCheckpoints,
buffersAllocated: bufferAllocated,
maxWritten: maxWritten,
}, nil
} else {
query := `SELECT
cp.num_requested AS checkpoint_req,
cp.num_timed AS checkpoint_scheduled,
cp.write_time AS checkpoint_duration_write,
cp.sync_time AS checkpoint_duration_sync,
cp.buffers_written AS buffers_checkpoints,
bg.buffers_clean AS bg_writes,
bg.buffers_alloc AS buffers_allocated,
bg.maxwritten_clean AS maxwritten_count
FROM pg_stat_bgwriter bg, pg_stat_checkpointer cp;`
row := c.client.QueryRowContext(ctx, query)
if err = row.Scan(
&checkpointsReq,
&checkpointsScheduled,
&checkpointWriteTime,
&checkpointSyncTime,
&bufferCheckpoints,
&bgWrites,
&bufferAllocated,
&maxWritten,
); err != nil {
return nil, err
}
return &bgStat{
checkpointsReq: checkpointsReq,
checkpointsScheduled: checkpointsScheduled,
checkpointWriteTime: checkpointWriteTime,
checkpointSyncTime: checkpointSyncTime,
bgWrites: bgWrites,
bufferBackendWrites: -1, // Not found in pg17+ tables
bufferFsyncWrites: -1, // Not found in pg17+ tables
bufferCheckpoints: bufferCheckpoints,
buffersAllocated: bufferAllocated,
maxWritten: maxWritten,
}, nil
}
}
func (c *postgreSQLClient) getMaxConnections(ctx context.Context) (int64, error) {
query := `SHOW max_connections;`
row := c.client.QueryRowContext(ctx, query)
var maxConns int64
err := row.Scan(&maxConns)
return maxConns, err
}
type replicationStats struct {
clientAddr string
pendingBytes int64
flushLagInt int64 // Deprecated
replayLagInt int64 // Deprecated
writeLagInt int64 // Deprecated
flushLag float64
replayLag float64
writeLag float64
}
func (c *postgreSQLClient) getDeprecatedReplicationStats(ctx context.Context) ([]replicationStats, error) {
query := `SELECT
coalesce(cast(client_addr as varchar), 'unix') AS client_addr,
coalesce(pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn), -1) AS replication_bytes_pending,
extract('epoch' from coalesce(write_lag, '-1 seconds'))::integer,
extract('epoch' from coalesce(flush_lag, '-1 seconds'))::integer,
extract('epoch' from coalesce(replay_lag, '-1 seconds'))::integer
FROM pg_stat_replication;
`
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("unable to query pg_stat_replication: %w", err)
}
defer rows.Close()
var rs []replicationStats
var errors error
for rows.Next() {
var client string
var replicationBytes int64
var writeLagInt, flushLagInt, replayLagInt int64
err = rows.Scan(&client, &replicationBytes,
&writeLagInt, &flushLagInt, &replayLagInt)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
rs = append(rs, replicationStats{
clientAddr: client,
pendingBytes: replicationBytes,
replayLagInt: replayLagInt,
writeLagInt: writeLagInt,
flushLagInt: flushLagInt,
})
}
return rs, errors
}
func (c *postgreSQLClient) getReplicationStats(ctx context.Context) ([]replicationStats, error) {
if !preciseLagMetricsFg.IsEnabled() {
return c.getDeprecatedReplicationStats(ctx)
}
query := `SELECT
coalesce(cast(client_addr as varchar), 'unix') AS client_addr,
coalesce(pg_wal_lsn_diff(pg_current_wal_lsn(), replay_lsn), -1) AS replication_bytes_pending,
extract('epoch' from coalesce(write_lag, '-1 seconds'))::decimal AS write_lag_fractional,
extract('epoch' from coalesce(flush_lag, '-1 seconds'))::decimal AS flush_lag_fractional,
extract('epoch' from coalesce(replay_lag, '-1 seconds'))::decimal AS replay_lag_fractional
FROM pg_stat_replication;
`
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("unable to query pg_stat_replication: %w", err)
}
defer rows.Close()
var rs []replicationStats
var errors error
for rows.Next() {
var client string
var replicationBytes int64
var writeLag, flushLag, replayLag float64
err = rows.Scan(&client, &replicationBytes, &writeLag, &flushLag, &replayLag)
if err != nil {
errors = multierr.Append(errors, err)
continue
}
rs = append(rs, replicationStats{
clientAddr: client,
pendingBytes: replicationBytes,
replayLag: replayLag,
writeLag: writeLag,
flushLag: flushLag,
})
}
return rs, errors
}
func (c *postgreSQLClient) getLatestWalAgeSeconds(ctx context.Context) (int64, error) {
query := `SELECT
coalesce(last_archived_time, CURRENT_TIMESTAMP) AS last_archived_wal,
CURRENT_TIMESTAMP
FROM pg_stat_archiver;
`
row := c.client.QueryRowContext(ctx, query)
var lastArchivedWal, currentInstanceTime time.Time
err := row.Scan(&lastArchivedWal, ¤tInstanceTime)
if err != nil {
return 0, err
}
if lastArchivedWal.Equal(currentInstanceTime) {
return 0, errNoLastArchive
}
age := int64(currentInstanceTime.Sub(lastArchivedWal).Seconds())
return age, nil
}
func (c *postgreSQLClient) listDatabases(ctx context.Context) ([]string, error) {
query := `SELECT datname FROM pg_database
WHERE datistemplate = false;`
rows, err := c.client.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var databases []string
for rows.Next() {
var database string
if err := rows.Scan(&database); err != nil {
return nil, err
}
databases = append(databases, database)
}
return databases, nil
}
func (c *postgreSQLClient) getVersion(ctx context.Context) (string, error) {
query := "SHOW server_version;"
row := c.client.QueryRowContext(ctx, query)
var version string
err := row.Scan(&version)
return version, err
}
func parseMajorVersion(ver string) (int, error) {
parts := strings.Split(ver, ".")
if len(parts) < 2 {
return 0, fmt.Errorf("unexpected version string: %s", ver)
}
return strconv.Atoi(parts[0])
}
func filterQueryByDatabases(baseQuery string, databases []string, groupBy bool) string {
if len(databases) > 0 {
var queryDatabases []string
for _, db := range databases {
queryDatabases = append(queryDatabases, fmt.Sprintf("'%s'", db))
}
if strings.Contains(baseQuery, "WHERE") {
baseQuery += fmt.Sprintf(" AND datname IN (%s)", strings.Join(queryDatabases, ","))
} else {
baseQuery += fmt.Sprintf(" WHERE datname IN (%s)", strings.Join(queryDatabases, ","))
}
}
if groupBy {
baseQuery += " GROUP BY datname"
}
return baseQuery + ";"
}
func tableKey(database, schema, table string) tableIdentifier {
return tableIdentifier(fmt.Sprintf("%s|%s|%s", database, schema, table))
}
func indexKey(database, schema, table, index string) indexIdentifer {
return indexIdentifer(fmt.Sprintf("%s|%s|%s|%s", database, schema, table, index))
}