Skip to content

Commit 0a79673

Browse files
author
Shane Borden
committed
add new scripts
1 parent 8eca9fa commit 0a79673

File tree

4 files changed

+99
-1
lines changed

4 files changed

+99
-1
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ build-sqlscripts: clean-sqlscripts ## Build the collector SQL scripts.
6666
python3 -c "import m2r; python_text = m2r.convert(open('postgres/README.md').read()); f = open('postgres/README.txt', 'w'); f.write(python_text); f.close()"
6767
@mkdir -p $(BUILD_DIR)/postgres
6868
@cp postgres/*.sql $(BUILD_DIR)/postgres
69+
@cp postgres/.psqlrc $(BUILD_DIR)/postgres
6970
@cp postgres/README.txt $(BUILD_DIR)/postgres
71+
@cp postgres/README.md $(BUILD_DIR)/postgres
7072
@cp LICENSE $(BUILD_DIR)/postgres
7173
@echo "SQL Helper Scripts for Postgres version $(VERSION) ($(COMMIT_SHA))" > $(BUILD_DIR)/postgres/VERSION.txt
7274

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
SELECT
2+
d.name AS DatabaseName,
3+
d.is_encrypted,
4+
dek.encryption_state,
5+
CASE dek.encryption_state
6+
WHEN 0 THEN 'No Database Encryption Key Present, Not Encrypted'
7+
WHEN 1 THEN 'Unencrypted'
8+
WHEN 2 THEN 'Encryption in Progress'
9+
WHEN 3 THEN 'Encrypted'
10+
WHEN 4 THEN 'Key Change in Progress'
11+
WHEN 5 THEN 'Decryption in Progress'
12+
WHEN 6 THEN 'Protection Change in Progress'
13+
ELSE 'Not Encrypted or No Key' -- Or NULL if no entry in dek
14+
END AS EncryptionStatusDescription,
15+
dek.percent_complete,
16+
dek.key_algorithm,
17+
dek.key_length
18+
FROM
19+
sys.databases d
20+
LEFT JOIN
21+
sys.dm_database_encryption_keys dek ON d.database_id = dek.database_id
22+
WHERE d.is_encrypted <> 0;
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
-- Copyright 2024 shaneborden
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- https://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
15+
CREATE EXTENSION pg_freespacemap;
16+
17+
CREATE OR REPLACE FUNCTION show_empty_pages(p_table_name TEXT)
18+
RETURNS VOID AS $$
19+
DECLARE
20+
-- Core processing variables
21+
table_oid_regclass REGCLASS;
22+
block_size BIGINT;
23+
fsm_granularity BIGINT;
24+
max_fsm_free_space BIGINT;
25+
total_pages BIGINT;
26+
high_water_mark BIGINT := 0;
27+
28+
-- Variables for the final summary
29+
first_empty_block BIGINT;
30+
free_pages_at_end BIGINT;
31+
free_space_at_end TEXT;
32+
BEGIN
33+
-- Setup
34+
table_oid_regclass := p_table_name::regclass;
35+
block_size := current_setting('block_size')::bigint;
36+
SELECT relpages INTO total_pages FROM pg_class WHERE oid = table_oid_regclass;
37+
fsm_granularity := block_size / 256;
38+
max_fsm_free_space := floor((block_size - 24) / fsm_granularity) * fsm_granularity;
39+
40+
--------------------------------------------------------------------------------
41+
-- PASS 1: FIND THE HIGH-WATER MARK (last page with data)
42+
--------------------------------------------------------------------------------
43+
FOR i IN REVERSE (total_pages - 1)..0 LOOP
44+
IF pg_freespace(table_oid_regclass, i) < max_fsm_free_space THEN
45+
high_water_mark := i;
46+
EXIT;
47+
END IF;
48+
END LOOP;
49+
50+
--------------------------------------------------------------------------------
51+
-- FINAL STEP: CALCULATE AND RAISE THE SUMMARY NOTICE
52+
--------------------------------------------------------------------------------
53+
first_empty_block := high_water_mark + 1;
54+
free_pages_at_end := total_pages - first_empty_block;
55+
IF free_pages_at_end < 0 THEN
56+
free_pages_at_end := 0;
57+
END IF;
58+
free_space_at_end := pg_size_pretty(free_pages_at_end * block_size);
59+
60+
RAISE NOTICE '-------------------------------------------------------------';
61+
RAISE NOTICE 'Summary for table: %', p_table_name;
62+
RAISE NOTICE '-------------------------------------------------------------';
63+
RAISE NOTICE 'The High Water Mark (HWM) is at page: %', total_pages;
64+
IF total_pages <> first_empty_block THEN
65+
RAISE NOTICE 'First potentially empty page is at: %', first_empty_block;
66+
RAISE NOTICE 'Total Pages in Table: %', total_pages;
67+
RAISE NOTICE 'Number of potentially truncatable pages at the end: %', free_pages_at_end;
68+
RAISE NOTICE 'Amount of free space at the end of the table: %', free_space_at_end;
69+
ELSE
70+
RAISE NOTICE 'There are no empty pages to truncate';
71+
END IF;
72+
RAISE NOTICE '-------------------------------------------------------------';
73+
END;
74+
$$ LANGUAGE plpgsql;

postgres/postgres_table_and_index_bloat_stats.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,6 @@ ROUND((
133133
0.0
134134
ELSE
135135
sml.relpages::float / otta
136-
END)::numeric, 1) > 10
136+
END)::numeric, 1) > 0
137137
ORDER BY
138138
2,4,3 DESC;

0 commit comments

Comments
 (0)