You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: internal/controller/postgrescluster/metrics_setup.sql
+59-50Lines changed: 59 additions & 50 deletions
Original file line number
Diff line number
Diff line change
@@ -221,33 +221,49 @@ END;
221
221
$$ LANGUAGE plpgsql;
222
222
223
223
/*
224
-
-- TODO: WHAT IS REALLY HAPPENING HERE?
225
-
* Tables and functions for monitoring changes to pg_hba_file_rules system catalogs.
226
-
* Tables allow recording of existing settings so they can be referred back to to see what changed
227
-
* If checksum function returns 0, then NO settings have changed
228
-
* If checksum function returns 1, then something has changed since last known valid state
229
-
* For replicas, logging past settings is not possible to compare what may have changed
230
-
* For replicas, by default, it is expected that its settings will match the primary
231
-
* For replicas, if the pg_hba.conf are necessarily different from the primary, a known good hash of that replica's
232
-
settings can be sent as an argument to the relevant checksum function. Views are provided to easily obtain the hash values used by this monitoring tool.
233
-
* If any known hash parameters are passed to the checksum function, note that it will override any past hash values stored in the log table when doing comparisons and completely re-evaluate the entire state. This is true even if done on a primary where the current state will then also be logged for comparison if it differs from the given hash.
234
-
Taken from https://github.com/CrunchyData/pgmonitor/blob/development/postgres_exporter/common
235
-
*/
224
+
* The `pg_hba_checksum` table, functions, and view are taken from
-- Function used to compare old pg_hba hash and current hash
253
+
/*
254
+
* `monitor.pg_hba_checksum(text)` is used to compare the previous pg_hba hash
255
+
* with a hash made of the current pg_hba hash, derived from the `monitor.pg_hba_hash` view below.
256
+
*
257
+
* This function returns
258
+
* - 0, indicating NO settings have changed
259
+
* - 1, indicating something has changed since last known valid state
260
+
*
261
+
* `monitor.pg_hba_checksum` can take a hash to be used as an override.
262
+
* This may be useful when you have a standby with different pg_hba rules;
263
+
* since it will have different rules (and therefore a different hash), you
264
+
* could alter the metric function to pass the actual hash, which would be
265
+
* used in lieu of this table's value (derived from the primary cluster's rules).
266
+
*/
251
267
DROPFUNCTION IF EXISTS monitor.pg_hba_checksum(text);
252
268
CREATEFUNCTIONmonitor.pg_hba_checksum(p_known_hba_hash text DEFAULT NULL)
253
269
RETURNS smallint
@@ -264,73 +280,67 @@ v_valid smallint;
264
280
265
281
BEGIN
266
282
267
-
SELECT pg_is_in_recovery() INTO v_is_in_recovery;
268
-
283
+
-- Retrieve the current settings from the `monitor.pg_hba_hash` view below
269
284
IF current_setting('server_version_num')::int>=100000 THEN
270
-
271
285
SELECT sha256_hash, hba_string
272
286
INTO v_hba_hash, v_hba_string
273
287
FROMmonitor.pg_hba_hash;
274
-
275
288
ELSE
276
289
RAISE EXCEPTION 'pg_hba change monitoring unsupported in versions older than PostgreSQL 10';
277
290
END IF;
278
291
292
+
-- Retrieve the last previous hash from the table
279
293
SELECT hba_hash_generated, valid
280
294
INTO v_hba_hash_old, v_valid
281
295
FROMmonitor.pg_hba_checksum
282
296
ORDER BY created_at DESCLIMIT1;
283
297
298
+
-- If an manual/override hash has been given, we will use that:
299
+
-- Do not base validity on the stored value if manual hash is given.
284
300
IF p_known_hba_hash IS NOT NULL THEN
285
301
v_hba_hash_old := p_known_hba_hash;
286
-
-- Do not base validity on the stored value if manual hash is given.
287
302
v_valid :=0;
288
303
END IF;
289
304
290
-
IF (v_hba_hash_old IS NOT NULL) THEN
291
-
292
-
IF (v_hba_hash != v_hba_hash_old) THEN
293
-
294
-
v_valid :=1;
295
-
296
-
IF v_is_in_recovery = false THEN
297
-
INSERT INTOmonitor.pg_hba_checksum (
298
-
hba_hash_generated
299
-
, hba_hash_known_provided
300
-
, hba_string
301
-
, valid)
302
-
VALUES (
303
-
v_hba_hash
304
-
, p_known_hba_hash
305
-
, v_hba_string
306
-
, v_valid);
307
-
END IF;
308
-
END IF;
309
-
305
+
IF (v_hba_hash_old IS NOT NULL) AND (v_hba_hash != v_hba_hash_old) THEN
306
+
v_valid :=1;
310
307
ELSE
311
-
312
308
v_valid :=0;
309
+
END IF;
310
+
311
+
/*
312
+
* We only want to insert into the table if we're on a primary and
313
+
* - the table/manually entered hash is empty, e.g., we've just started the cluster; or
314
+
* - the hashes don't match
315
+
*
316
+
* There's no value added by inserting into the table when no change was detected.
317
+
*/
318
+
IF (v_hba_hash_old IS NULL) OR (v_hba_hash != v_hba_hash_old) THEN
319
+
SELECT pg_is_in_recovery() INTO v_is_in_recovery;
313
320
IF v_is_in_recovery = false THEN
314
321
INSERT INTOmonitor.pg_hba_checksum (
315
322
hba_hash_generated
316
323
, hba_hash_known_provided
317
324
, hba_string
318
325
, valid)
319
-
VALUES (v_hba_hash
326
+
VALUES (
327
+
v_hba_hash
320
328
, p_known_hba_hash
321
329
, v_hba_string
322
330
, v_valid);
323
331
END IF;
324
-
325
332
END IF;
326
333
327
334
RETURN v_valid;
328
335
329
336
END
330
337
$function$;
331
-
-- End function used to compare hashes
332
338
333
-
-- View used to create hash of pg_hba
339
+
/*
340
+
* The `monitor.pg_hba_hash` view return both a hash and a string aggregate of the
341
+
* pg_catalog.pg_hba_file_rules.
342
+
* Note: We use `sha256` to hash to allow this to run on FIPS environments.
343
+
*/
334
344
DROPVIEW IF EXISTS monitor.pg_hba_hash;
335
345
CREATEVIEWmonitor.pg_hba_hash AS
336
346
-- Order by line number so it's caught if no content is changed but the order of entries is changed
@@ -347,11 +357,11 @@ CREATE VIEW monitor.pg_hba_hash AS
347
357
SELECT sha256((string_agg(type||database||user_name||address||netmask||auth_method||options, ','))::bytea) AS sha256_hash
348
358
, string_agg(type||database||user_name||address||netmask||auth_method||options, ',') AS hba_string
349
359
FROM hba_ordered_list;
350
-
-- End view used to create hash of pg_hba
351
360
352
-
-- Function used to set pg_hba as valid
353
361
/*
354
-
* This function provides quick, clear interface for resetting the checksum monitor to treat the currently detected configuration as valid after alerting on a change. Note that configuration history will be cleared.
362
+
* The `monitor.pg_hba_checksum_set_valid` function provides an interface for resetting the
363
+
* checksum monitor.
364
+
* Note: configuration history will be cleared.
355
365
*/
356
366
DROPFUNCTION IF EXISTS monitor.pg_hba_checksum_set_valid();
0 commit comments