summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/postgres/do_query_misc.go
blob: a2299c8b40ae3bc9e8579e50d9970e431faecee3 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package postgres

import (
	"database/sql"
	"strconv"

	"github.com/jackc/pgx/v4/stdlib"
)

func (p *Postgres) doQueryServerVersion() (int, error) {
	q := queryServerVersion()

	var s string
	if err := p.doQueryRow(q, &s); err != nil {
		return 0, err
	}

	return strconv.Atoi(s)
}

func (p *Postgres) doQueryIsSuperUser() (bool, error) {
	q := queryIsSuperUser()

	var v bool
	if err := p.doQueryRow(q, &v); err != nil {
		return false, err
	}

	return v, nil
}

func (p *Postgres) doQueryPGIsInRecovery() (bool, error) {
	q := queryPGIsInRecovery()

	var v bool
	if err := p.doQueryRow(q, &v); err != nil {
		return false, err
	}

	return v, nil
}

func (p *Postgres) doQuerySettingsMaxConnections() (int64, error) {
	q := querySettingsMaxConnections()

	var s string
	if err := p.doQueryRow(q, &s); err != nil {
		return 0, err
	}

	return strconv.ParseInt(s, 10, 64)
}

func (p *Postgres) doQuerySettingsMaxLocksHeld() (int64, error) {
	q := querySettingsMaxLocksHeld()

	var s string
	if err := p.doQueryRow(q, &s); err != nil {
		return 0, err
	}

	return strconv.ParseInt(s, 10, 64)
}

const connErrMax = 3

func (p *Postgres) doQueryQueryableDatabases() error {
	q := queryQueryableDatabaseList()

	var dbs []string
	err := p.doQuery(q, func(_, value string, _ bool) {
		if p.dbSr != nil && p.dbSr.MatchString(value) {
			dbs = append(dbs, value)
		}
	})
	if err != nil {
		return err
	}

	seen := make(map[string]bool, len(dbs))

	for _, dbname := range dbs {
		seen[dbname] = true

		conn, ok := p.dbConns[dbname]
		if !ok {
			conn = &dbConn{}
			p.dbConns[dbname] = conn
		}

		if conn.db != nil || conn.connErrors >= connErrMax {
			continue
		}

		db, connStr, err := p.openSecondaryConnection(dbname)
		if err != nil {
			p.Warning(err)
			conn.connErrors++
			continue
		}

		tables, err := p.doDBQueryUserTablesCount(db)
		if err != nil {
			p.Warning(err)
			conn.connErrors++
			_ = db.Close()
			stdlib.UnregisterConnConfig(connStr)
			continue
		}

		indexes, err := p.doDBQueryUserIndexesCount(db)
		if err != nil {
			p.Warning(err)
			conn.connErrors++
			_ = db.Close()
			stdlib.UnregisterConnConfig(connStr)
			continue
		}

		if (p.MaxDBTables != 0 && tables > p.MaxDBTables) || (p.MaxDBIndexes != 0 && indexes > p.MaxDBIndexes) {
			p.Warningf("database '%s' has too many user tables(%d/%d)/indexes(%d/%d), skipping it",
				dbname, tables, p.MaxDBTables, indexes, p.MaxDBIndexes)
			conn.connErrors = connErrMax
			_ = db.Close()
			stdlib.UnregisterConnConfig(connStr)
			continue
		}

		conn.db, conn.connStr = db, connStr
	}

	for dbname, conn := range p.dbConns {
		if seen[dbname] {
			continue
		}
		delete(p.dbConns, dbname)
		if conn.connStr != "" {
			stdlib.UnregisterConnConfig(conn.connStr)
		}
		if conn.db != nil {
			_ = conn.db.Close()
		}
	}

	return nil
}

func (p *Postgres) doDBQueryUserTablesCount(db *sql.DB) (int64, error) {
	q := queryUserTablesCount()

	var v string
	if err := p.doDBQueryRow(db, q, &v); err != nil {
		return 0, err
	}

	return parseInt(v), nil
}

func (p *Postgres) doDBQueryUserIndexesCount(db *sql.DB) (int64, error) {
	q := queryUserIndexesCount()

	var v string
	if err := p.doDBQueryRow(db, q, &v); err != nil {
		return 0, err
	}

	return parseInt(v), nil
}