Skip to content

Instantly share code, notes, and snippets.

View dcube9's full-sized avatar
🎯
Focusing

Paolo Botti dcube9

🎯
Focusing
View GitHub Profile
@dcube9
dcube9 / Postgresql_ContaRecord_Occupazione.sql
Created February 6, 2026 12:13
POSTGRESQL - Conta i record e l'occupazione di tutte le tabelle di un database
-- 1) Aggiorna le statistiche di tutte le tabelle del database corrente
ANALYZE;
-- 2) Leggi le stime aggiornate per tutte le tabelle utente,
-- includendo la dimensione totale (tabella + indici + TOAST)
SELECT
s.schemaname AS schema_name,
s.relname AS table_name,
s.n_live_tup AS row_count_estimated,
pg_total_relation_size(c.oid) AS table_size_bytes,
@dcube9
dcube9 / Postgresql_TrasformaInIndiceUnicoAutomatico.sql
Created February 4, 2026 16:46
POSTGRESQL - Trasforma una colonna numerica in un indice unico auto incrementato
-- opzionale: assicurati che id sia NOT NULL
ALTER TABLE public.mia_tabella
ALTER COLUMN id SET NOT NULL;
-- aggiungi l'identity (auto-increment)
ALTER TABLE public.mia_tabella
ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY;
SELECT setval(
pg_get_serial_sequence('mia_tabella', 'id'),
@dcube9
dcube9 / Postgresql_ContaRecord.sql
Created January 16, 2026 09:53
POSTGRESQL - Conta i record di tutte le tabelle di un database
-- 1) Aggiorna le statistiche di tutte le tabelle del database corrente
ANALYZE;
-- 2) Leggi le stime aggiornate per tutte le tabelle utente
SELECT
schemaname AS schema_name,
relname AS table_name,
n_live_tup AS row_count_estimated
FROM pg_stat_user_tables
ORDER BY schemaname, relname;