Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter Table too slow in postgres

I'm trying to add a new column

ALTER TABLE "Cidade" ADD COLUMN "BoundBox" VARCHAR(255)

to this table:

 "Cidade"
    "Id" integer not null
        constraint "Cidade_PK"
            primary key,
    "Nome" varchar(120),
    "EstadoId" integer not null
        constraint "Estado_Cidade_FK"
            references "Estado",
    "PontoCentralLatitude" numeric,
    "PontoCentralLongitude" numeric

But the query never finish, I've already waited for 5 minutes and nothing happened. The table has only 5,000 records, and I can't wait too much time since it block the access to the table. I have a test database (equal to production), and it worked very quickly. The postgres version is 9.5.6.

like image 870
Juliano Grams Avatar asked Jan 27 '18 19:01

Juliano Grams


1 Answers

If you are running PostgreSQL 9.6+ you can use pg_blocking_pids() to find PID of queries that lock your one.

select pid, pg_blocking_pids(pid) as blocked_by, query as blocked_query
from pg_stat_activity
where pg_blocking_pids(pid)::text != '{}';
like image 100
Eugenij Avatar answered Sep 23 '22 01:09

Eugenij