adsb-1090/sql/01_audit_partitions.sql

64 lines
2.2 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- =============================================================================
-- 01_audit_partitions.sql — inventaire RÉEL des partitions de adsb.positions
--
-- Lecture seule. Ne modifie rien.
--
-- reltuples = -1 signifie « jamais analysée » en PostgreSQL 16, PAS « vide ».
-- Une somme à -16 (16 enfants × -1) est donc un compte INCONNU. Ce script
-- compte les lignes pour de vrai avant toute décision de suppression.
--
-- Usage :
-- docker exec -i wordpress-postgres psql -U pgz_admin -d wpz_postgres \
-- -v ON_ERROR_STOP=1 < 01_audit_partitions.sql
-- =============================================================================
\timing on
-- Vue d'ensemble
SELECT c.relkind,
count(*) AS nb,
CASE c.relkind WHEN 'r' THEN 'RANGE simple (cible)'
WHEN 'p' THEN 'encore sous-partitionnée HASH' END AS etat
FROM pg_inherits i JOIN pg_class c ON c.oid = i.inhrelid
WHERE i.inhparent = 'adsb.positions'::regclass
GROUP BY 1, 3
ORDER BY 1;
-- Comptage réel de chaque partition de type 'p'.
-- Sur ~127 partitions dont la plupart sont vides, l'opération est rapide :
-- un count(*) sur une partition vide ne lit aucune page.
CREATE TEMP TABLE audit_parts AS
SELECT c.relname::text AS partition,
pg_get_expr(c.relpartbound, c.oid) AS bornes,
0::bigint AS lignes
FROM pg_inherits i JOIN pg_class c ON c.oid = i.inhrelid
WHERE i.inhparent = 'adsb.positions'::regclass
AND c.relkind = 'p';
DO $$
DECLARE r record; n bigint;
BEGIN
FOR r IN SELECT partition FROM audit_parts LOOP
EXECUTE format('SELECT count(*) FROM adsb.%I', r.partition) INTO n;
UPDATE audit_parts SET lignes = n WHERE partition = r.partition;
END LOOP;
END $$;
-- Synthèse
SELECT count(*) FILTER (WHERE lignes = 0) AS partitions_vides,
count(*) FILTER (WHERE lignes > 0) AS partitions_avec_donnees,
sum(lignes) AS lignes_totales_a_migrer
FROM audit_parts;
-- Détail des partitions contenant des données (à migrer avec copie)
SELECT partition, bornes, lignes
FROM audit_parts
WHERE lignes > 0
ORDER BY bornes;
-- Bornes extrêmes des partitions vides (à recréer sans copie)
SELECT min(bornes) AS premiere_vide, max(bornes) AS derniere_vide
FROM audit_parts WHERE lignes = 0;
\timing off