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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T11:34:01+00:00 2026-06-07T11:34:01+00:00

Straight from the manual, here’s the canonical example of merge_db in PostgreSQL : CREATE

  • 0

Straight from the manual, here’s the canonical example of merge_db in PostgreSQL:

CREATE TABLE db (a INT PRIMARY KEY, b TEXT);

CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
$$
BEGIN
    LOOP
        -- first try to update the key
        UPDATE db SET b = data WHERE a = key;
        IF found THEN
            RETURN;
        END IF;
        -- not there, so try to insert the key
        -- if someone else inserts the same key concurrently,
        -- we could get a unique-key failure
        BEGIN
            INSERT INTO db(a,b) VALUES (key, data);
            RETURN;
        EXCEPTION WHEN unique_violation THEN
            -- Do nothing, and loop to try the UPDATE again.
        END;
    END LOOP;
END;
$$
LANGUAGE plpgsql;

SELECT merge_db(1, 'david');
SELECT merge_db(1, 'dennis');

Can this be expressed as a user-defined function in MySQL, and if so, how? Would there be any advantage over MySQL’s standard INSERT...ON DUPLICATE KEY UPDATE?

Note: I’m specifically looking for a user-defined function, not INSERT...ON DUPLICATE KEY UPDATE.

  • 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-07T11:34:04+00:00Added an answer on June 7, 2026 at 11:34 am

    Tested on MySQL 5.5.14.

    CREATE TABLE db (a INT PRIMARY KEY, b TEXT);
    
    DELIMITER //
    CREATE PROCEDURE merge_db(k INT, data TEXT) 
    BEGIN
        DECLARE done BOOLEAN;
        REPEAT
            BEGIN
                -- If there is a unique key constraint error then 
                -- someone made a concurrent insert. Reset the sentinel
                -- and try again.
                DECLARE ER_DUP_UNIQUE CONDITION FOR 23000;
                DECLARE CONTINUE HANDLER FOR ER_DUP_UNIQUE BEGIN
                    SET done = FALSE;
                END;
    
                SET done = TRUE;
                SELECT COUNT(*) INTO @count FROM db WHERE a = k;
                -- Race condition here. If a concurrent INSERT is made after
                -- the SELECT but before the INSERT below we'll get a duplicate
                -- key error. But the handler above will take care of that.
                IF @count > 0 THEN 
                    UPDATE db SET b = data WHERE a = k;
                ELSE 
                    INSERT INTO db (a, b) VALUES (k, data);
                END IF;
            END;
        UNTIL done END REPEAT;
    END//
    
    DELIMITER ;
    
    CALL merge_db(1, 'david');
    CALL merge_db(1, 'dennis');
    

    Some thoughts:

    • You can’t do an update first and then check @ROW_COUNT() because it returns the number of rows actually changed. This could be 0 if the row already has the value you are trying to update.
    • Also, @ROW_COUNT() is not replication safe.
    • You could use REPLACE...INTO.
    • If using InnoDB or a table with transaction support you might be able to use SELECT...FOR UPDATE (untested).

    I see no advantage to this solution over just using INSERT...ON DUPLICATE KEY UPDATE.

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

Sidebar

Related Questions

Here's an easy one straight from the text book I can't seem to find.
I copied the example straight from their page: http://jsfiddle.net/D2RLR/631/ ... and I get the
How in the world do you create a custom front? Almost straight from the
I have created a certificate basically straight from the keytool example page: keytool -genkey
I made an example , I copied it straight from the jQuery website yet,
Here is a program I'm trying to run straight from section 1.9 of The
Most of this is straight from the hint example. What I'd like to do
I have made a resource reader with some example code from here . Here
I took this code straight from the w3 schools example, however, outside of their
I have code straight from Google's dev guide: Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse(market://details?id=com.example.android));

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.