I'm running a telegram bot using python-telegram-bot==13.13 and psycopg==3.1.4 for database connections.
Yesterday this error raised suddenly:
psycopg.transaction.OutOfOrderTransactionNesting: transaction commit at the wrong nesting level
.
The function is more than a hundred of lines so I'll put only the transaction where the error raised:
Due to the traceback, it happened on the first line here:
with conn.transaction():
cur.execute(f'update testauth_profile set balance=balance-{price} where telegram_id={update.effective_chat.id}')
cur.execute(f'update testauth_profile set balance=balance+{price-cost} where id=622')
cur.execute(f"insert into testauth_log values ('{data['mdn']}','{name}',now(),{context.user_data['uid']},'{data['id']}')")
cur.execute(f"insert into verifications values ('{data['id']}',now()+interval'15 minute',{update.effective_chat.id},0,'{data['mdn']}',{price-cost},{price},{context.user_data['lang']})")
where conn
is a psycopg connection and has autocommit=True. and cur
is a cursor from conn
.
After that, everything happend on the database in the next 11 hours were rolled back and I lost them permanently.
Any idea for what caused this error? and how can I avoid this in the future?
I searched in google but nothing about this error. I had a look at the postgres logs in /var/logs/postgres/ but there was no errors in the whole 11 hours and even before. The strange thing is that I never touched the code and it has been running for days without any problem. The function where the error was raised, was executed many times before without problems.