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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:03:21+00:00 2026-06-09T21:03:21+00:00

I have the responsibility of switching our code from sqlite to postgres. One of

  • 0

I have the responsibility of switching our code from sqlite to postgres. One of the queries I am having trouble with is copied below.

INSERT INTO group_phones(group_id, phone_name)
SELECT g.id, p.name 
FROM phones AS p, groups as g
WHERE g.id IN ($add_groups) AND p.name IN ($phones);

The problem arises when there is a duplicate record. In this table the combination of both values must be unique. I have used a few plpgsql functions in other places to do update-or-insert operations, but in this case I can do several inserts at once. I am not sure how to write a stored routine for this. Thanks for all the help from all the sql gurus out there!

  • 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-09T21:03:23+00:00Added an answer on June 9, 2026 at 9:03 pm

    There are 3 challenges.

    1. Your query has no JOIN condition between the tables phones and groups, making this effectively a limited CROSS JOIN – which you most probably do not intend. I.e. every phone that qualifies is combined with every group that qualifies. If you have 100 phones and 100 groups that’s already 10,000 combinations.

    2. Insert distinct combinations of (group_id, phone_name)

    3. Avoid inserting rows that are already there in table group_phones .

    All things considered it could look like this:

    INSERT INTO group_phones(group_id, phone_name)
    SELECT i.id, i.name
    FROM  (
        SELECT DISTINCT g.id, p.name -- get distinct combinations
        FROM   phones p
        JOIN   groups g ON ??how are p & g connected??
        WHERE  g.id IN ($add_groups)
        AND    p.name IN ($phones)
        ) i
    LEFT   JOIN group_phones gp ON (gp.group_id, gp.phone_name) = (i.id, i.name)
    WHERE  gp.group_id IS NULL  -- avoid duping existing rows
    

    Concurrency

    This form minimizes the chance of a race condition with concurrent write operations. If your table has heavy concurrent write load, you may want to lock the table exclusively or use serializable transaction isolation, This safeguard against the extremely unlikely case that a row is altered by a concurrent transaction in the tiny time slot between the constraint verification (row isn’t there) and the write operation in the query.

    BEGIN ISOLATION LEVEL SERIALIZABLE;
    INSERT ...
    COMMIT;
    

    Be prepared to repeat the transaction if it rolls back with a serialization error.
    For more on that topic good starting points could be this blog post by @depesz or this related question on SO.

    Normally, though, you needn’t even bother with any of this.

    Performance

    LEFT JOIN tbl ON right_col = left_col WHERE right_col IS NULL
    

    is generally the fastest method with distinct columns in the right table. If you have dupes in the column (especially if there are many),

    WHERE NOT EXISTS (SELECT 1 FROM tbl WHERE right_col = left_col)
    

    May be faster because it can stop to scan as soon as the first row is found.

    You can also use IN, like @dezso demonstrates, but it is usually slower in PostgreSQL.

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

Sidebar

Related Questions

do routed events from wpf have something in common with the chain of responsibility
From Wikipedia : Single responsibility principle states that every class should have a single
The UI thread in Winforms have the responsibility of running the message pump, by
For my understanding purpose i have implemented Chain-Of-Responsibility pattern. //Abstract Base Type public abstract
Have deployed numerous report parts which reference the same view however one of them
I've more than one activity (inside the same Application) that needs to have access
I have an application that implements something like a Chain of Responsibility in Python.
I have a method that accepts three NSMutableArrays, one as Input and two as
Situation I have a few single-responsibility Servlets that take a request, do their job,
I'm in a project that takes the Single Responsibility Principle pretty seriously. We have

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.