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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:33:13+00:00 2026-06-15T03:33:13+00:00

Expanding on the usual INSERT OR UPDATE conondrum I have a function that serves

  • 0

Expanding on the usual INSERT OR UPDATE conondrum

I have a function that serves normal UPDATE OR INSERT, and it looks like this:

CREATE OR REPLACE FUNCTION updateslave ( varchar(7), smallint ) RETURNS void AS
$$
 BEGIN
   LOOP
     UPDATE consist SET
       master = $1,
       time = now() WHERE slave = $2;
     IF found THEN
       RETURN;
     END IF;
     BEGIN
       INSERT INTO consist(master, slave, time) VALUES ( $1, $2, now() );
       RETURN;
     EXCEPTION WHEN unique_violation THEN
       -- do nothing, then loop and retry
     END;
   END LOOP;
 END;
##
LANGUAGE plpgsql;

Now, the issue is that i’m trying to rewrite it for a similar operation in a different, table. However, the difference is that in this other table there is no single unique column, but the combination of two columns only exists in one row. Is it possible to declare unique_violation based on the combination of two columns instead?

For the sake of keeping examples simple, let’s assume that the table looks exactly the same as the one i use the above function for, but with master and slave being the two columns that together produce uniqueness:

 Column |            Type             |                  Modifiers                   | Storage  | Description
--------+-----------------------------+----------------------------------------------+----------+-------------
 master | character varying(7)        | not null default 'unused'::character varying | extended |
 slave  | smallint                    | not null                                     | plain    |
 time   | timestamp without time zone | default now()                                | plain    |
  • 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-15T03:33:14+00:00Added an answer on June 15, 2026 at 3:33 am

    The best approach is to define a unique constraint on the table, that way no matter how the update happens, ie if your proc is used or not, everything is OK.

    The easiest way to do that is to create a unique index over the two columns:

    create unique index any_mame on mytable(col1, col2);
    

    You can also alter the table to add a unique constraint, but there’s not much difference.

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

Sidebar

Related Questions

Expanding on this question , it looks like I did not provide enough detail.
I am expanding forms that are created with Zend_Dojo , for this i have
In my application I have a UILabel that holds an expanding number of entries
Background Expanding upon this question. I have a collection of points (in a three
I'm expanding further on this question . I currently have an asp.net hyperlink for
Considering that the family of iDevices is expanding, I guess this is a problem
I have a bunch of divs that I am expanding/retracting individually using .toggle and
Quick question: if I have an XML like this one: <?xml version=1.0 encoding=utf-8?> <cop
I am using and expanding a function that detects whether two 2d rotated rectangles
Expanding on this question , For items that trigger dialogs and menus (i.e. non

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.