PostgreSQL 中 SQL Servfer 的 CONTEXT_INFO 的等价物是什么?

发布于 2024-07-16 14:53:14 字数 433 浏览 6 评论 0原文

关于我的其他问题“审核日志删除的最佳方法是什么? ”。 PostgreSQL 中相当于 CONTEXT_INFO 的是什么?

我想使用触发器记录删除操作,但由于我没有使用数据库用户作为应用的逻辑用户,因此我无法将触发器代码中的 CURRENT_USER 记录为删除记录的用户。 但是对于 INSERTUPDATE ,可以记录触发器中的记录更改,因为您只需在记录中添加用户字段,例如 inserted_bylast_updated_by,并使用这些字段记录到审核表。

In relation to my other question "What’s the best way to audit log DELETEs?". What's the PostgreSQL equivalent of CONTEXT_INFO?

I want to log deletes using a trigger, but since I'm not using the database user as my app's logical user, I cannot log the CURRENT_USER from the trigger code as the user who deleted the record. But for INSERT and UPDATE it is possible to log the record changes from trigger since you can just add a user field in the record, say inserted_by and last_updated_by, and use these fields to log to audit table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

英雄似剑 2024-07-23 14:53:14

接受的答案已过时。

在 postgresql 的最新版本中(我认为是从 9.3 或 9.4 开始),可以设置/检索仅在当前会话[或事务]中有效的配置变量。 文档参考

设置会话的示例变量:

hal=# select set_config('myvar.foo', 'bar', false);
 set_config 
------------
 bar
(1 row)

hal=# select current_setting('myvar.foo');
 current_setting 
-----------------
 bar
(1 row)

可以像使用其他设置返回函数一样使用函数current_setting

例子:

create table customers (id int);
insert into customers values (1), (2), (3), (4);

select c.*, s.*
from customers c
left join current_setting('myvar.foo') s
       on c.id = length(s)

The accepted answer is outdated.

In recent versions of postgresql (since 9.3 or 9.4 I think), one can set / retrieve config variables are only alive for the current session [or transaction]. Documentation Reference

example of setting a session variable:

hal=# select set_config('myvar.foo', 'bar', false);
 set_config 
------------
 bar
(1 row)

hal=# select current_setting('myvar.foo');
 current_setting 
-----------------
 bar
(1 row)

It is possible to use the function current_setting as one would use other set returning functions.

example:

create table customers (id int);
insert into customers values (1), (2), (3), (4);

select c.*, s.*
from customers c
left join current_setting('myvar.foo') s
       on c.id = length(s)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文