fix(userbot): mark deletions per row to stay under timescale decompression limit

This commit is contained in:
hh
2026-09-06 21:30:51 +02:00
parent ab59ef4525
commit 5ec0212af5
@@ -30,6 +30,16 @@ ON CONFLICT (account_id, chat_id, message_id, date) DO UPDATE SET
edited_at = now() edited_at = now()
""" """
_FIND_DELETED = """
SELECT chat_id, message_id, date FROM messages
WHERE account_id = $1 AND message_id = ANY($2::bigint[]) AND deleted_at IS NULL
"""
_MARK_DELETED = """
UPDATE messages SET deleted_at = now()
WHERE account_id = $1 AND chat_id = $2 AND message_id = $3 AND date = $4
"""
_INSERT_VERSION = """ _INSERT_VERSION = """
INSERT INTO message_versions INSERT INTO message_versions
(account_id, chat_id, message_id, observed_at, edit_date, text, raw) (account_id, chat_id, message_id, observed_at, edit_date, text, raw)
@@ -128,30 +138,34 @@ async def max_message_id(
) )
async def _mark_deleted(
pool: asyncpg.Pool, account_id: int, rows: list[asyncpg.Record]
) -> None:
for row in rows:
await pool.execute(
_MARK_DELETED, account_id, row["chat_id"], row["message_id"], row["date"]
)
async def mark_deleted_box( async def mark_deleted_box(
pool: asyncpg.Pool, account_id: int, message_ids: list[int] pool: asyncpg.Pool, account_id: int, message_ids: list[int]
) -> None: ) -> None:
await pool.execute( rows = await pool.fetch(
"UPDATE messages SET deleted_at = now() " f"{_FIND_DELETED} AND chat_id > $3",
"WHERE account_id = $1 AND message_id = ANY($2::bigint[]) "
"AND chat_id > $3 AND deleted_at IS NULL",
account_id, account_id,
message_ids, message_ids,
CHANNEL_ID_THRESHOLD, CHANNEL_ID_THRESHOLD,
) )
await _mark_deleted(pool, account_id, rows)
async def mark_deleted_channel( async def mark_deleted_channel(
pool: asyncpg.Pool, account_id: int, chat_id: int, message_ids: list[int] pool: asyncpg.Pool, account_id: int, chat_id: int, message_ids: list[int]
) -> None: ) -> None:
await pool.execute( rows = await pool.fetch(
"UPDATE messages SET deleted_at = now() " f"{_FIND_DELETED} AND chat_id = $3", account_id, message_ids, chat_id
"WHERE account_id = $1 AND chat_id = $2 AND message_id = ANY($3::bigint[]) "
"AND deleted_at IS NULL",
account_id,
chat_id,
message_ids,
) )
await _mark_deleted(pool, account_id, rows)
async def add_version( # noqa: PLR0913 async def add_version( # noqa: PLR0913