Postgres: Fix account_write cleanup query

Avoid misbehaving in situations where there's an account write for a
slot that doesn't yet have an entry in the slots table by limiting the
slot numbers.
This commit is contained in:
Christian Kamm 2022-01-20 15:28:16 +01:00
parent 287308daf7
commit 185bf4f9cf
1 changed files with 5 additions and 3 deletions

View File

@ -178,13 +178,15 @@ fn make_cleanup_steps(tables: &Vec<String>) -> HashMap<String, String> {
.iter()
.map(|table_name| {
let sql = format!(
"DELETE FROM {table} AS data
"WITH newest_rooted AS (SELECT max(slot) AS newest_rooted_slot FROM slot WHERE status = 'Rooted')
DELETE FROM {table} AS data
USING
(SELECT max(slot) AS newest_rooted_slot FROM slot WHERE status = 'Rooted') s,
newest_rooted,
(SELECT DISTINCT ON(pubkey_id) pubkey_id, slot, write_version
FROM {table}
LEFT JOIN slot USING(slot)
WHERE status = 'Rooted' OR status is NULL
CROSS JOIN newest_rooted
WHERE slot <= newest_rooted_slot AND (status = 'Rooted' OR status is NULL)
ORDER BY pubkey_id, slot DESC, write_version DESC
) newest_rooted_write
WHERE data.pubkey_id = newest_rooted_write.pubkey_id