doltgresql icon indicating copy to clipboard operation
doltgresql copied to clipboard

UPDATE metadata semantics incorrect

Open zachmu opened this issue 3 months ago • 1 comments

The Postgres response for an UPDATE reports how many rows matched the WHERE clause, regardless of whether the value changed. This differs from the MySQL semantics (reports rows changed), but is equivalent to setting the CLIENT_FOUND_ROWS client compatibility flag.

This bug is critical due to its interpretation by ORMs, where a 0 result is used as proof of absence from a table.

CREATE TABLE bug3 (id integer PRIMARY KEY, v text);                                                       
INSERT INTO bug3 VALUES (1, 'same');                                                                      
                                                                                                          
UPDATE bug3 SET v = 'same'      WHERE id = 1;    -- reports 0; PostgreSQL reports 1                       
UPDATE bug3 SET v = 'different' WHERE id = 1;    -- reports 1                                             
UPDATE bug3 SET v = 'x'         WHERE id = 999;  -- reports 0

zachmu avatar Aug 17 '26 22:08 zachmu

A runnable reproduction of this bug on DoltgreSQL 1.3.1, side by side with PostgreSQL 18.6, is at https://github.com/Reliable-Collaboration/repro-doltgresql-bug-update-row-count.

On DoltgreSQL 1.3.1, the command tag of an UPDATE counts only the rows whose values changed: UPDATE t SET a = a WHERE id = 1 matches one row and answers UPDATE 0. PostgreSQL 18.6 counts every row the UPDATE matched, changed or not, and answers UPDATE 1; for an UPDATE that matches two rows and changes one, PostgreSQL answers UPDATE 2 and DoltgreSQL UPDATE 1.

git clone https://github.com/Reliable-Collaboration/repro-doltgresql-bug-update-row-count.git
cd repro-doltgresql-bug-update-row-count
./repro.sh

The test

repro.sql:

CREATE TABLE t (id int PRIMARY KEY, a int);
INSERT INTO t VALUES (1, 10), (2, 20);

-- Matches one row and leaves its value as it is.
UPDATE t SET a = a WHERE id = 1;

-- Matches both rows, but only row 2 gets a new value.
UPDATE t SET a = 10;

SELECT * FROM t ORDER BY id;

Other observations

Each variant was run on DoltgreSQL 1.3.1 and on PostgreSQL 18.6, and PostgreSQL ran every one without an error:

  • An UPDATE that matches three rows and changes none answers UPDATE 0 (PostgreSQL: UPDATE 3), and one that matches three rows and changes two answers UPDATE 2 (PostgreSQL: UPDATE 3). An UPDATE that matches no row answers UPDATE 0 on both.
  • Setting a column to the literal value it already holds, a text column to the same text, or a NULL to NULL answers UPDATE 0 (PostgreSQL: UPDATE 1).
  • So does an UPDATE that changes nothing inside an explicit transaction, on a table without a primary key, or on a table with a BEFORE UPDATE trigger that returns NEW, and so does SET id = id on the primary key.
  • An UPDATE sent through the extended query protocol with psql's \bind, setting a value the row already holds, answers UPDATE 0 (PostgreSQL: UPDATE 1).
  • With RETURNING, the count agrees with PostgreSQL: UPDATE t SET a = a WHERE id = 1 RETURNING id, a returns the row and answers UPDATE 1 on both.
  • In PL/pgSQL, FOUND follows the same count: a function that runs UPDATE t SET a = a WHERE id = 1; and then RETURN FOUND; returns f (PostgreSQL: t).
  • INSERT ... ON CONFLICT (id) DO UPDATE SET a = EXCLUDED.a on an existing row answers INSERT 0 0 when the row keeps its values and INSERT 0 2 when it changes; PostgreSQL answers INSERT 0 1 both times.
  • The UPDATE count is also described in dolthub/doltgresql#3113.

Environment

  • DoltgreSQL 1.3.1, the newest release when this was written: image dolthub/doltgresql:1.3.1, digest sha256:6c85cb1f35beabf47f094336a420255130b841b1645f36d79ef046276af36851. Its bundled psql is 17.11.
  • PostgreSQL 18.6: image postgres:18.6-bookworm, digest sha256:1c59e2c3c818eaa0f0628f695b36e7c9e362d6b219b36a54a32df645cbd7e1af. Its bundled psql is 18.6.
  • Reproduced on 2026-09-10 with Docker 29.7.2 on Linux x86_64 (Ubuntu 26.04.1 LTS under WSL 2).

mattchristenson avatar Sep 11 '26 02:09 mattchristenson