zubkov-andrei / pg_profile

Postgres historic workload reports
Other
212 stars 31 forks source link

Добавить функцию для удаления данных о таблице #74

Open triwada opened 10 months ago

triwada commented 10 months ago

В ходе анализа возникшей проблемы при вызове take_sample() из-за таблицы repack, которой уже не существовало, пришло понимание, что нужен простой способ удаление данных от таблице из истории сэмплов profile, т.к. сейчас это превращается в достаточно долгий квест по удалению всех зависимых ссылок из связанных таблиц. Хотелось бы иметь возможность вызова одной функции, которая почистит все записи от ненужной таблице их profile


"duplicate key value violates unique constraint ""uk_toast_table""                    +
 SQL statement ""INSERT INTO tables_list AS itl ( 
...
 PL/pgSQL function sample_dbobj_delta(jsonb,integer,integer,integer,boolean) line 261 at SQL statement+
 PL/pgSQL function take_sample(integer,boolean) line 1032 at assignment                               +
 PL/pgSQL function take_sample_subset(integer,integer) line 27 at assignment                          +
 SQL function ""take_sample"" statement 1                                                             +
 Key (server_id, datid, reltoastrelid)=(4, 55555, 128684901) already exists.",00:00:18.86)

postgres=# select * from profile.tables_list where (server_id, datid, reltoastrelid)=(4, 55555, 128684901);
 server_id | datid |   relid   | relkind | reltoastrelid | schemaname |   relname   | last_sample_id
-----------+-------+-----------+---------+---------------+------------+-------------+----------------
         4 | 55555 | 128684898 | r       |     128684901 | repack     | table_25114 |           2840
(1 строка)

Чтобы удалить данные об этой таблице, пришлось проделать столько шагов

kkt=# select oid,relkind,relname from pg_class where relnamespace::regnamespace::text = 'repack';
   oid    | relkind |   relname
----------+---------+--------------
 55781316 | v       | primary_keys
 55781321 | v       | tables
(2 строки)

Т.е. таблицы repack.table_25114 уже нет и поэтому можно удалить данные о ней из сэмплов profile

postgres=# delete from profile.tables_list where (server_id, datid, reltoastrelid)=(4, 55555, 128684901);
ERROR:  update or delete on table "tables_list" violates foreign key constraint "fk_st_tables_tables" on table "sample_stat_tables"
ПОДРОБНОСТИ:  Key (server_id, datid, relid)=(4, 55555, 128684898) is still referenced from table "sample_stat_tables".

удаляем из таблицы profile.sample_stat_tables данные, связанные с более несуществующей таблицой repack.table_25114

postgres=# delete from profile.sample_stat_tables where (server_id, datid, relid)=(4, 55555, 128684898);
DELETE 2

postgres=# delete from profile.tables_list where (server_id, datid, reltoastrelid)=(4, 55555, 128684901);
ERROR:  update or delete on table "tables_list" violates foreign key constraint "fk_indexes_tables" on table "indexes_list"
ПОДРОБНОСТИ:  Key (server_id, datid, relid)=(4, 55555, 128684898) is still referenced from table "indexes_list".

postgres=# select * from profile.indexes_list where (server_id, datid, relid)=(4, 55555, 128684898);
 server_id | datid | indexrelid |   relid   | schemaname | indexrelname  | last_sample_id
-----------+-------+------------+-----------+------------+---------------+----------------
         4 | 55555 |  128693602 | 128684898 | repack     | index_894519  |           2840
         4 | 55555 |  128693600 | 128684898 | repack     | index_2173005 |           2840
         4 | 55555 |  128693634 | 128684898 | repack     | index_25124   |           2840
         4 | 55555 |  128693624 | 128684898 | repack     | index_25127   |           2840
         4 | 55555 |  128693654 | 128684898 | repack     | index_25120   |           2840
         4 | 55555 |  128693618 | 128684898 | repack     | index_25131   |           2840
         4 | 55555 |  128693613 | 128684898 | repack     | index_871279  |           2840
         4 | 55555 |  128693274 | 128684898 | repack     | index_9478750 |           2840
         4 | 55555 |  128693621 | 128684898 | repack     | index_25128   |           2840
         4 | 55555 |  128693627 | 128684898 | repack     | index_25126   |           2840
         4 | 55555 |  128693594 | 128684898 | repack     | index_2599144 |           2840
         4 | 55555 |  128693172 | 128684898 | repack     | index_9480130 |           2839
(12 строк)

postgres=# delete from profile.indexes_list where (server_id, datid, relid)=(4, 55555, 128684898);
ERROR:  update or delete on table "indexes_list" violates foreign key constraint "fk_stat_indexes_indexes" on table "sample_stat_indexes"
ПОДРОБНОСТИ:  Key (server_id, datid, indexrelid)=(4, 55555, 128693602) is still referenced from table "sample_stat_indexes".

postgres=# delete from profile.sample_stat_indexes where server_id=4 and datid=55555 and indexrelid in (select indexrelid from profile.indexes_list where server_id=4 and datid=55555 and relid=128684898);
DELETE 12
postgres=# delete from profile.indexes_list where server_id=4 and datid=55555 and relid=128684898;
DELETE 12

postgres=# delete from profile.tables_list where (server_id, datid, reltoastrelid)=(4, 55555, 128684901);
DELETE 1
triwada commented 10 months ago

также замечено, что проблема "duplicate key value violates unique constraint ""uk_toast_table""" возникает с таблицами toast после выполнения repack на таблице

zubkov-andrei commented 10 months ago

Добрый день! Вы смотрите немного не в ту сторону. Я не сторонник удалений из репозитория, кроме тех, что выполняются по истечении срока хранения снимков. Если после некоторых действий с базой даннхы возникают проблемы с выполеннием снимка, то это баг, и надо чинить именно его. По идее никакие действия в базе не должны приводить к невозможности выполнения снимка. Совместимость pg_profile и repack я не проверял, но этой проблемой, очевидно, надо заняться.