Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:48:34+00:00 2026-06-15T15:48:34+00:00

I am trying to create a trigger function in PostgreSQL that should check records

  • 0

I am trying to create a trigger function in PostgreSQL that should check records with the same id (i.e. comparison by id with existing records) before inserting or updating the records. If the function finds records that have the same id, then that entry is set to be the time_dead. Let me explain with this example:

INSERT INTO persons (id, time_create, time_dead, name)
VALUES (1, 'now();', ' ', 'james');

I want to have a table like this:

 id  time_create  time-dead  name
 1   06:12                   henry   
 2   07:12                   muka

id 1 had a time_create 06.12 but the time_dead was NULL. This is the same as id 2 but next time I try to run the insert query with same id but different names I should get a table like this:

 id  time_create  time-dead  name
 1   06:12        14:35      henry   
 2   07:12                   muka
 1   14:35                   waks

henry and waks share the same id 1. After running an insert query henry’s time_dead is equal to waks’ time_create. If another entry was to made with id 1, lets say for james, the time entry for james will be equal to the time_dead for waks. And so on.

So far my function looks like this. But it’s not working:

CREATE FUNCTION tr_function() RETURNS trigger AS '
BEGIN
  IF tg_op = ''UPDATE'' THEN
     UPDATE persons
     SET time_dead = NEW.time_create
     Where
         id = NEW.id
         AND time_dead IS NULL
         ;

  END IF;
  RETURN new;
END
' LANGUAGE plpgsql;

CREATE TRIGGER sofgr BEFORE INSERT OR UPDATE
        ON persons FOR each ROW
        EXECUTE PROCEDURE tr_function();

When I run this its say time_dead is not supposed to be null. Is there a way I can write a trigger function that will automatically enter the time upon inserting or updating but give me results like the above tables when I run a select query?

What am I doing wrong?

My two tables:

CREATE TABLE temporary_object
(
  id integer NOT NULL,
  time_create timestamp without time zone NOT NULL,
  time_dead timestamp without time zone,
  PRIMARY KEY (id, time_create)
);

CREATE TABLE persons
(
  name text
)
INHERITS (temporary_object);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:48:35+00:00Added an answer on June 15, 2026 at 3:48 pm

    Trigger function

    CREATE FUNCTION tr_function()
      RETURNS trigger
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       UPDATE persons p
       SET    time_dead = NEW.time_create
       WHERE  p.id = NEW.id
       AND    p.name <> NEW.name
       AND    p.time_dead IS NULL;
    
       RETURN NEW;
    END
    $func$;
    

    You were missing the INSERT case in your trigger function (IF tg_op = ''UPDATE''). But there is no need for checking TG_OP to begin with, since the trigger only fires on INSERT OR UPDATE – assuming you don’t use the same function in other triggers. So I removed the cruft.

    You don’t have to escape single quotes inside a dollar-quoted string.

    Also added:

    AND    p.name <> NEW.name
    

    … to prevent INSERT‘s from terminating themselves instantly (and causing an infinite recursion). This assumes that a row can never succeed another row with the same name.

    Aside: The setup is still not bullet-proof. UPDATEs could mess with your system. I could keep updating the id or a row, thereby terminating other rows but not leaving a successor. Consider disallowing updates on id. Of course, that would make the trigger ON UPDATE pointless. I doubt you need that to begin with.

    DEFAULT now()

    If you want to use now() as default for time_create just make it so. Read the manual about setting a column DEFAULT. Then skip time_create in INSERTs and it is filled automatically.

    If you want to force it (prevent everyone from entering a different value) create a trigger ON INSERT or add the following at the top of your trigger:

    IF TG_OP = 'INSERT' THEN
        NEW.time_create := now();   -- type timestamp or timestamptz!
        RETURN NEW;
    END IF;
    

    That forces the current timestamp for new rows unconditionally.
    (Assuming your column "time_create" is actually type timestamp.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

how can i create a trigger function before adding/updating ,the function should check records
Possible Duplicate: Reset JavaScript Counter? I'm trying to create a function that will trigger
I am trying to write a trigger function in PostgreSQL 8.2 that will dynamically
I am trying to create Update trigger which should be invoked only if the
I'm trying to create a basic database trigger that conditionally deletes rows from database1.table1
I'm trying to create a simple flex4 project which involves some timers that trigger
I am trying to create a PostgreSQL trigger in a Play2.0 database evolution script.
I am trying to create a trigger that will update a GEOMETRY column based
Im trying to create a function, that will return a mysql query, which i
I am trying to create a trigger to update another table with a condition

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.