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

  • Home
  • SEARCH
  • 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 6179633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:42:11+00:00 2026-05-24T00:42:11+00:00

What is the most efficient way to create this trigger in Postgres. I’ll present

  • 0

What is the most efficient way to create this trigger in Postgres.

I’ll present below a very simplified example to what I need for my purposes, but it is based on the same concept.

Consider, we have got the schema defined below:

CREATE TABLE items (
  item_id int4,
  part_no int4,
  description text);

CREATE TABLE blacklist (
  part_no int4,
  reason text);

CREATE TABLE matches (
  item_id int4,
  part_no int4,
  reason text);

Then, every time a new item is added, we check if it is on the blacklist (comparing the part_no), and if it is, we create a new entry on the matches table.

  • 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-05-24T00:42:12+00:00Added an answer on May 24, 2026 at 12:42 am

    You’d want a before-insert or after-insert trigger on items:

    create trigger blacklist_matches after insert on items
    for each row execute procedure check_blacklist();
    

    Then the check_blacklist function would look something like this:

    create function check_blacklist()
        returns trigger as $$
    begin
        insert into matches (item_id, part_no, reason)
        select NEW.item_id, NEW.part_no, blacklist.reason
        from blacklist
        where blacklist.part_no = NEW.part_no;
        return null;
    end;
    $$ language plpgsql;
    

    The funny looking $$ is, more or less, the SQL version of a heredoc.

    That wraps the “is it on the blacklist” check and the matches insertion in one simple bit of SQL. The NEW variable in a trigger is a reference to the new row that you’re working with. If NEW.part_no doesn’t match anything in blacklist, then the SELECT won’t produce anything and the INSERT will not be executed.

    Presumably you’d have an index on blacklist.part_no (which looks like a PK anyway) so the above should be quick enough.

    The PostgreSQL documentation is pretty good and has a whole section on stored procedures and triggers if you need a reference.

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

Sidebar

Related Questions

What is the most efficient way to create a constant (never changes at runtime)
What is the most efficient way to create empty ListBuffer ? val l1 =
What's the most efficient way to convert the output of this function from a
What is the most efficient way to create a cancel event in a C#
What's the most efficient way to concatenate strings?
What's the most efficient way to resize large images in PHP? I'm currently using
What is the most efficient way to determine how many comments a particular blog
Whats the most efficient way of selecting total number of records from a large
What would be the most efficient way of recording to a log (.txt) from
What is the most efficient way to get the default constructor (i.e. instance constructor

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.