UPDATE with AND clarification

From: Jamie Thompson <jthompson(dot)neuk(at)gmail(dot)com>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: UPDATE with AND clarification
Date: 2024-03-22 11:36:54
Message-ID: CABoe=cS68ZKuY09AUE92QuZbZG=UQozBL0m+nWQQ-oLFfDhfFA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Hi,

Struggled to find a section about this when scanning through the docs, but
awkward keywords to scan for, so apologies for duplicating a request that
has very likely come through before.

I'm aware a correct syntax for a multi column UPDATE is:
> UPDATE tablename SET col1 = val1, col2 = val2 WHERE id = X

Earlier today, I was running some sql for someone which they had submitted
as:
> UPDATE tablename SET col1 = val1 AND col2 = val2 WHERE id = X

This returned UPDATE 1, but the row hadn't actually changed, col1 was still
the old value.

Please could you explain what is actually happening here? Is there a
section in the docs about this?
If I EXPLAIN (ANALYZE, VERBOSE) the update with the AND syntax, I can see
the top level update node has 0 rows:
> Update on public.tablename (cost=11.91..24.87 rows=0 width=0) (actual
time=1.377..1.378 rows=0 loops=1)
So why is it returning:
> UPDATE 1

Is it because it's only looking at col2 and in this scenario that value was
already correct? What's it doing with the first column (col1) in the SET?

Thanks, hoping this can be clarified so I can help others avoid the mistake
in future.

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message David G. Johnston 2024-03-22 13:28:42 Re: How to choose table in SELECT clause without schema qualifier or 'set local'
Previous Message intmail01 2024-03-22 07:11:23 How to choose table in SELECT clause without schema qualifier or 'set local'