Delivery 1.0 Help

SQL Queries to the rescue

  • Szukanie PIDów blokujących poszczególne PIDy

  • Ubicie zapytań, które blokują zapytanie do “przepchnięcia” jak jakieś ALTERY itp

  • Indexy, które nie są poprawne - tzw Invalidy

  • Długo wykonujące się zapytania

  • Lista tabel bez klucza głównego

Szukanie PIDów blokujących poszczególne PIDy

select pid, usename, query_start, wait_event_type, wait_event, usename, query, state, pg_blocking_pids(pid) from pg_stat_activity --where query ilike '%index%' --np. żeby złapać co blokuje budowanie indexu

Ubicie zapytań, które blokują zapytanie do “przepchnięcia” jak jakieś ALTERY itp

with pids_to_kill as (select unnest(pg_blocking_pids(pid)) as one_pid from pg_stat_activity where query ilike '%alter%' -- któremy query chcemy "pomóc" and query not ilike '%select%') -- wykluczenie tego tutaj, żeby się nie załapało select pg_cancel_backend(one_pid) from pids_to_kill --delitaknie z buta -- select pg_terminate_backend(one_pid) from pids_to_kill --brutalnie z buta

Indexy, które nie są poprawne - tzw Invalidy

SELECT * FROM pg_class, pg_index WHERE pg_index.indisvalid = false AND pg_index.indexrelid = pg_class.oid;

Długo wykonujące się zapytania

select * from pg_stat_activity where state != 'idle' and query_start < now() - interval '1 minute'

Lista tabel bez klucza głównego

select tbl.table_schema, tbl.table_name from information_schema.tables tbl where table_type = 'BASE TABLE' and table_schema not in ('pg_catalog', 'information_schema') and not exists (select 1 from information_schema.key_column_usage kcu where kcu.table_name = tbl.table_name and kcu.table_schema = tbl.table_schema)
Last modified: 30 May 2024