41 lines
3.3 KiB
SQL
41 lines
3.3 KiB
SQL
-- Сверка двух баз после восстановления: одинаковые числа строк и контрольные суммы. Запускается на источнике и на копии,
|
|
-- результаты сравниваются построчно (diff). Хеши считаются от полного содержимого строк в детерминированном порядке.
|
|
\pset format unaligned
|
|
\pset tuples_only on
|
|
\pset fieldsep '|'
|
|
|
|
SELECT 'users', count(*), md5(string_agg(concat_ws('|', id, username, display_name, password_hash, created_at), ',' ORDER BY id)) FROM users
|
|
UNION ALL
|
|
SELECT 'chats', count(*), md5(string_agg(concat_ws('|', id, title, created_at, history_version, last_message_at), ',' ORDER BY id)) FROM chats
|
|
UNION ALL
|
|
SELECT 'chat_members', count(*), md5(string_agg(chat_id::text || user_id::text, ',' ORDER BY chat_id, user_id)) FROM chat_members
|
|
UNION ALL
|
|
SELECT 'messages (вся таблица)', count(*), md5(string_agg(concat_ws('|', id, chat_id, sender_id, text, created_at), ',' ORDER BY id)) FROM messages
|
|
UNION ALL
|
|
SELECT 'sessions', count(*), md5(string_agg(concat_ws('|', id, encode(token_hash, 'hex'), user_id, created_at, expires_at, revoked_at), ',' ORDER BY id)) FROM sessions
|
|
UNION ALL
|
|
SELECT 'ef_migrations', count(*), md5(string_agg(migration_id || product_version, ',' ORDER BY migration_id)) FROM "__EFMigrationsHistory"
|
|
UNION ALL
|
|
-- Распределение сообщений по чатам (неравномерность сохранилась).
|
|
SELECT 'сообщений по чатам', count(*), md5(string_agg(chat_id::text || ':' || c, ',' ORDER BY chat_id))
|
|
FROM (SELECT chat_id, count(*) c FROM messages GROUP BY chat_id) t
|
|
UNION ALL
|
|
-- Выбранные сообщения: первые 100 и последние 100 самого большого чата.
|
|
SELECT 'большой чат: первые 100', count(*), md5(string_agg(id::text || text || created_at::text, ',' ORDER BY created_at, id))
|
|
FROM (SELECT * FROM messages WHERE chat_id = (SELECT chat_id FROM messages GROUP BY chat_id ORDER BY count(*) DESC, chat_id LIMIT 1)
|
|
ORDER BY created_at, id LIMIT 100) t
|
|
UNION ALL
|
|
SELECT 'большой чат: последние 100', count(*), md5(string_agg(id::text || text || created_at::text, ',' ORDER BY created_at, id))
|
|
FROM (SELECT * FROM messages WHERE chat_id = (SELECT chat_id FROM messages GROUP BY chat_id ORDER BY count(*) DESC, chat_id LIMIT 1)
|
|
ORDER BY created_at DESC, id DESC LIMIT 100) t
|
|
UNION ALL
|
|
-- Детерминированная выборка ~1 из 115 сообщений по всей таблице (около тысячи сообщений из разных чатов).
|
|
SELECT 'выборка ~1/115', count(*), md5(string_agg(id::text || chat_id::text || text || created_at::text, ',' ORDER BY id))
|
|
FROM messages WHERE ('x' || substr(md5(id::text), 1, 8))::bit(32)::int % 115 = 0
|
|
UNION ALL
|
|
-- Схема: индексы и ограничения совпадают с исходными.
|
|
SELECT 'индексы', count(*), md5(string_agg(indexdef, ',' ORDER BY indexname)) FROM pg_indexes WHERE schemaname = 'public'
|
|
UNION ALL
|
|
SELECT 'ограничения', count(*), md5(string_agg(conname || pg_get_constraintdef(oid), ',' ORDER BY conname))
|
|
FROM pg_constraint WHERE connamespace = 'public'::regnamespace;
|