简体   繁体   English

触发PostgreSQL数据库的日期更改!

[英]Trigger for change in date for PostgreSQL database!

How shall I write a trigger which will be fired when the LocalDate of the PostgreSQL database server changes? 如何编写一个触发器,当PostgreSQL数据库服务器的LocalDate更改时将触发该触发器? I want a notification when the LocalDate (the date of the postgresql database server) is changed, which will be the most effective way to do that? 当LocalDate(PostgreSQL数据库服务器的日期)更改时,我想要一个通知,这将是最有效的方法? The application which needs the notification is written in Java. 需要通知的应用程序是用Java编写的。

Further clarification: I am building Public Transportation system, and i need to set the schedule of the day, at the start of every day. 进一步的说明:我正在构建公共交通系统,我需要在每天的开始时确定一天的时间表。 And the day is according to the time of the database server. 而日期则取决于数据库服务器的时间。 How do I achieve this with minimum overhead? 如何以最小的开销实现这一目标?

The only thing I can think of is to have a scheduled job (server-side job) insert the current date into a table once a day at, say, 12:01 am local time. 我唯一能想到的就是安排一个调度作业(服务器端作业),将每天的当前日期插入本地表中,例如每天在当地时间12:01。 A trigger could respond to that. 触发器可以对此做出响应。

There's not a way to do this only using database without periodic checking. 没有一种方法,只能使用数据库而不进行定期检查。

If you need notification in a client application then LISTEN/NOTIFY can be used. 如果您需要在客户端应用程序中进行通知,则可以使用LISTEN / NOTIFY。 You'd need to configure server scheduler (for example cron) to launch a script which connect to a server at 00:00 and issue a NOTIFY. 您需要配置服务器调度程序(例如cron)以启动脚本,该脚本在00:00连接到服务器并发出NOTIFY。 But this will need a Java client to periodically check for PQnotifies , which is not better than just checking current_date periodically. 但这需要Java客户端定期检查PQnotifies ,这比仅定期检查current_date更好。

What do you need this for? 您需要什么呢? Maybe there's a better solution to your underlining problem. 也许您的下划线问题有更好的解决方案。


Your only sensible option is to create a program on server which does your daily maintenance and schedule it using server operating system scheduler (cron on Unices, Windows Task Scheduler on Windows). 唯一明智的选择是在服务器上创建一个程序,该程序进行日常维护并使用服务器操作系统计划程序(在Unices上为cron,在Windows上为Windows Task Scheduler)来计划程序。

For extra reliability: 为了提高可靠性:

  • set your maintenance script to lock a special table maintenance ( last_maintenance date ) 设置维护脚本以锁定特殊表维护(last_maintenance日期)
  • when a client connects to a database it should check if current_date=last_maintenance and error out if it isn't; 当客户端连接到数据库时,应检查current_date = last_maintenance,如果不是,则进行错误提示;
  • if currently maintenance is running then it would just wait on lock until it's done. 如果当前正在运行维护,则它将等待锁定直到完成。

What is "LocalDate" ? 什么是“ LocalDate”? You mean the server system date? 您是指服务器系统日期? In that case it's not possible, triggers fire on record changes in a table. 在这种情况下,不可能对表中的记录更改触发触发。 Check the manual: http://www.postgresql.org/docs/current/static/plpgsql-trigger.html 检查手册: http : //www.postgresql.org/docs/current/static/plpgsql-trigger.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM