1

I need to write a trigger that will check a table column to see if data is there or not. The trigger needs to run all the time and log msg every hour.

Basically it will run a select statement if result found then sleep for an hour else log and sleep for an hour

3
  • are you talking a scheduled 'job' that runs and then re-runs after 'sleeping' (schedule a job that runs every hour?)...or are you talking a trigger that would fire every time a record is inserted/updated/deleted? I think you are thinking more the scheduled job if it's to run hourly. Commented May 22, 2012 at 18:44
  • What exactly do you mean with "if data is there or not"? Commented May 22, 2012 at 18:44
  • In one table there is a row with timestamp, I want to check if a row was inserted in it in the last hour, if so than wait else log a message Commented May 22, 2012 at 18:49

2 Answers 2

2

What you want is a scheduled job. pgAgent : http://www.pgadmin.org/docs/1.4/pgagent.html create an hourly job that checks for that line and then logs as required.

Edit to add: Curious if you've considered writing a SQL script that generates the log on the fly by reading the table instead of a job. If you have a timestamp field, it is quite possible to have a script that returns all hourly periods that don't have a corresponding entry within that time frame (assuming the time stamp isn't updated). Why store a second log when you can generate it directly against the data?

Sign up to request clarification or add additional context in comments.

Comments

1

Triggers (in pg and in every dbms i know) can execute before or after events such insert, update or delete. What you probably want here is a script launched via something like cron (if you are using a unix system) every hour, redirecting your output to the log file. I used something like this many times and it sounded like this (written in python):

#!/usr/bin/python


import psycopg2
try:
    conn = psycopg2.connect("dbname='"+dbmane+"' user='"+user+"' host='"+hostname+"' password='"+passwd+"'")
except:
    # Get the most recent exception
    exceptionType, exceptionValue, exceptionTraceback = sys.exc_info()
    # Exit the script and print an error telling what happened.
    logging.debug("I am unable to connect to the database!\n ->%s" % (exceptionValue))
    exit(2)

cur = conn.cursor()

query = "SELECT whatever FROM wherever WHERE yourconditions"
try:
    cur.execute(query)
except ProgrammingError:
    print "Programming error, no result produced"
result = cur.fetchone()

if(result == None):
    #do whatever you need, if result == None the data is not in your table column

I used to launch my script via cron every 10 minutes, you can easily configure it to launch the script every hour redirecting its output to the log file of your choice.

If your working in a windows environment, than you'll be looking for something like cron. I don't think that a trigger can help you with this, they fire only after some events (you can use a trigger to check after every insert if the inserted data is the one you want to check every hour, but it's not the same, doing it via script is the best solution in my experience)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.