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 552821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:32:26+00:00 2026-05-13T11:32:26+00:00

I’ve recently started using PostgreSQL and I’m trying to do things the right way

  • 0

I’ve recently started using PostgreSQL and I’m trying to do things “the right way” as I’ve understood it. Meaning putting as much work in the database server instead of on the client as possible.

So I’ve created a function with PL/pgSQL that will add data to a table. Since I’ve a primary key constraint set on that table, and the referenced key may not exist at the time I’m trying to add the new data I’ve added an exception catching that will create the key and then try to insert the new row again.

This is working satisfactory for me, but I’m curious whether I’m handling this “the correct way”. I’ve been trying to find some kind of guide of designing these user defined functions but havn’t really found anything I found helpful.

CREATE OR REPLACE FUNCTION add_product_price_promo_xml(v_product_code varchar, v_description varchar, v_product_group varchar,
                                                       v_mixmatch_id integer, v_price_at date, v_cost_price numeric, v_sales_price numeric,
                                                       v_tax_rate integer) RETURNS void AS $$
BEGIN
   INSERT INTO product_prices (product_code  , mixmatch_id  , price_at  , cost_price  , sales_price  , tax_rate) VALUES
                              (v_product_code, v_mixmatch_id, v_price_at, v_cost_price, v_sales_price, v_tax_rate);
EXCEPTION WHEN foreign_key_violation THEN
   INSERT INTO products (code, description, product_group) VALUES (v_product_code, v_description, v_product_group);
   PERFORM add_product_price_promo_xml($1, $2, $3, $4, $5, $6, $7, $8);
END;
$$ LANGUAGE plpgsql;

The database in question is going to be used to make reports and will be importing the complete item register every day with price updates and new items, but I won’t know which items are new and which ones are old.

  • 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-13T11:32:26+00:00Added an answer on May 13, 2026 at 11:32 am

    No!!! WRONG WAY Sorry, I’ve been using postgresql for years and this is a bad idea. The right way is (1) to create a temp table, and (2) update where there is a violation (3) insert where there isn’t a violation. I’ll show you a snippet using pg 8.4:

    CREATE TEMP TABLE temp_table (
      LIKE table INCLUDING INDEXES INCLUDING CONSTRAINTS
    );
    

    Then you want to, insert all of your stuff into temp_table and run these two commands.

    UPDATE table
    SET a = t.a
    FROM temp_table AS t
    WHERE join-constraints;
    
    INSERT INTO table
    SELECT * FROM temp_table AS t
    WHERE NOT EXISTS (
        SELECT * FROM table AS v
        WHERE ( join-constraints )
    );
    

    You can do it in a transaction if you care, and you have enough mem. This method scales because internally it doesn’t create a massive amount of checkpoints. It is also massively faster. Your current way was published as a pseudo-merge routine on Varlena in 2006, it bit me, and it has bitten lots of people. I don’t see a need for it, so I suggest you avoid it.

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

Sidebar

Ask A Question

Stats

  • Questions 390k
  • Answers 390k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer whoops! Turns out, I had got the package installed initially… May 15, 2026 at 1:07 am
  • Editorial Team
    Editorial Team added an answer Thanks to CodeFluff for the answer. I've adapted it to… May 15, 2026 at 1:07 am
  • Editorial Team
    Editorial Team added an answer On Windows you can try to open file in exclusive… May 15, 2026 at 1:07 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.