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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T21:42:50+00:00 2026-06-01T21:42:50+00:00

Yesterday I discovered something strange about SQL, or at least PostreSQL . Take a

  • 0

Yesterday I discovered something strange about SQL, or at least PostreSQL. Take a look below and explain why first query does nothing and the second one works just right.

-- this silently does nothing
update bodycontent 
    set body = replace(body, '~' || u.oldusername, '~' || u.newusername)
    from usermigration u;

-- this works as expected
update bodycontent 
    set body = replace(body, '~' || oldusername, '~' || newusername)
    from usermigration u;

Update: I think that everyone is missing the point on this question, the cartesian product was an original intent: there are going to be N x M updates and this is by design.

I need to replace all the pairs of usernames existing in the migration table in each row from bodycontent.

And, I repeat, the second version works as expected, but the first one does no update. All I wanted to know was why.

| usermigration table    |
--------------------------
oldusername | newusersname
--------------------------
johndoe     | johnd
john.smith  | johnsmith

Is this a bug in PostgreSQL?

  • 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-01T21:42:52+00:00Added an answer on June 1, 2026 at 9:42 pm

    You are missing a WHERE clause (that cannot be written as direct JOIN condition). The additional table usermigration has to be bound to the table bodycontent to be updated in some way, or every row of bodycontent has as many update candidates as there are rows in usermigration – the cartesian product between the two tables.

    There is no way to tell which one will be applied and persist. Both statements are wrong in this regard. For instance, if there are 1000 rows in usermigration and 1000 rows in bodycontent this will results in 1 000 000 updates candidates before 1000 can be picked.

    If you join in one or more tables in an UPDATE statement it hardly ever makes any sense without a WHERE clause connecting the result of the FROM clause to the updated table.

    Consider these notes in the manual about the UPDATE statement:

    When a FROM clause is present, what essentially happens is that the
    target table is joined to the tables mentioned in the from_list, and
    each output row of the join represents an update operation for the
    target table. When using FROM you should ensure that the join produces
    at most one output row for each row to be modified. In other words, a
    target row shouldn’t join to more than one row from the other
    table(s). If it does, then only one of the join rows will be used to
    update the target row, but which one will be used is not readily
    predictable.

    Note that the FROM clause in the UPDATE statement is a PostgreSQL extension to the SQL standard. Other DBMS use different syntax, for instance explicit JOINs to the table to be updated (in tSQL) that do not work for PostgreSQL.


    Answer to additional question in comment

    This query should work, mostly1

    UPDATE bodycontent b
    SET    body = replace(b.body, u.oldusername, u.newusername)
    FROM   usermigration u
    WHERE  b.body LIKE ('%' || u.oldusername || '%');
    

    1 The outcome is still ambiguous. Multiple matches can be found. It is uncertain, which one will be applied. The problem is that your requirements are inherently ambiguous. There can be multiple (overlapping) usernames that match and the order in which updates are applied is relevant (but not defined). The UPDATE statement perfectly reflects your flawed requirements.

    And what’s up with the '~' || part?

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

Sidebar

Related Questions

Yesterday, I discovered the Subversion (SVN) blame feature, and I was wondering, is this
Yesterday, I have a discussion with my colleagues about HTTP. It is asked why
yesterday you had taught me to query record history within a time range here
Yesterday, after about two years we re-deployed our application. Code hasn't changed, but the
Yesterday I discovered an odd bug in rather simple code that basically gets text
Yesterday and for about months I was able to push/commit. But since few hours
Yesterday I have been testing one single query for over hours and hours, but
Yesterday Oracle decided to take down java.sun.com for a while. This screwed things up
I am new to LINQ and discovered yesterday that you can have multiple where
yesterday I watched Google IO Talk about NFC and today I'm trying to do

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.