|
| 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; |
0 commit comments