通过PostgreSQL触发器记录当前Python解释器的Stacktrace [英] Log Stacktrace of current Python Interpreter via PostgreSQL trigger

查看:89
本文介绍了通过PostgreSQL触发器记录当前Python解释器的Stacktrace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找一个不时在我们的生产服务器上发生的错误,但无法通过其他方式重现:数据库中的某些值会以我不希望的方式更改。



我可以编写一个PostgreSQL触发器,如果​​该错误发生,它将触发,并从所述触发器引发异常。我会看到执行不需要的SQL语句的Python回溯。



但是在这种情况下,我不想停止处理请求。



有没有一种方法可以从PostgreSQL触发器中记录Python / Django追溯?



我知道这不是很简单,因为数据库代码在不同的Linux进程下以不同的用户ID运行。



我正在使用Python,Django,PostgreSQL,Linux。



我想这并不容易,因为数据库触发器在与python解释器不同的上下文中运行。



请询问是否需要更多信息。 / p>

更新



一种解决方案可能是覆盖 psycopg2的connection.notices

解决方案

 是否可以从PostgreSQL触发器中记录Python / Django追溯? 






不,没有




  • (SQL)查询在 DBMS服务器上执行,触发器内的代码也是如此

  • Python代码在 client 上执行,这是一个不同的进程,可能由不同的用户执行,甚至可能在不同的计算机上执行。



服务器(检测条件)和客户端(需要执行stackdump)之间的唯一连接是已连接的套接字。您可以尝试通过某些状态码来扩展服务器的回复(如果有的话),客户端将使用该状态码来堆叠自己。仅当触发器是当前事务的一部分而不是某些无关过程的触发器时,这才起作用。



另一种方法是:大量日志记录。使DBMS将个提交的SQL写入其日志文件。这可能会导致大量日志条目,您必须检查这些日志条目。


I am trying to find a bug which happens from time to time on our production server, but could not be reproduced otherwise: some value in the DB gets changed in a way which I don't want it to.

I could write a PostgreSQL trigger which fires if this bug happens, and raise an exception from said trigger. I would see the Python traceback which executes the unwanted SQL statement.

But in this case I don't want to stop the processing of the request.

Is there a way to log the Python/Django traceback from within a PostgreSQL trigger?

I know that this is not trival since the DB code runs under a different linux process with a different user id.

I am using Python, Django, PostgreSQL, Linux.

I guess this is not easy since the DB trigger runs in a different context than the python interpreter.

Please ask if you need further information.

Update

One solution might be to overwrite connection.notices of psycopg2.

解决方案

Is there a way to log the Python/Django traceback from within a PostgreSQL trigger?


No, there is not

  • The (SQL) query is executed on the DBMS-server, and so is the code inside the trigger
  • The Python code is executed on the client which is a different process, possibly executed by a different user, and maybe even on a different machine.

The only connection between the server (which detects the condition) and the client (which needs to perform the stackdump) is the connected socket. You could try to extend the server's reply (if there is one) by some status code, which is used by the client to stackddump itself. This will only work if the trigger is part of the current transaction, not of some unrelated process.

The other way is: massive logging. Make the DBMS write every submitted SQL to its logfile. This can cause huge amounts of log entries, which you have to inspect.

这篇关于通过PostgreSQL触发器记录当前Python解释器的Stacktrace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆