Re: Minor buglet in update...from (I think) - Mailing list pgsql-hackers

From Bruce Momjian
Subject Re: Minor buglet in update...from (I think)
Date
Msg-id 200111270058.fAR0w6c15346@candle.pha.pa.us
Whole thread Raw
In response to Minor buglet in update...from (I think)  (Philip Warner <pjw@rhyme.com.au>)
List pgsql-hackers
> Bruce Momjian <pgman@candle.pha.pa.us> writes:
> > I thought the aggregate would be generated on all rows in the table in
> > the pre-transaction version of the table, so in this example:
> >     regression=# update t2 set f2 = min(f1) from t1;
> > It places the minimum value of t1.f1 in all t2.f2 rows.
> 
> This actually is not the most interesting example, because the aggregate
> doesn't depend at all on t2.  Try this instead:
> 
> regression=# create table t1(f1 int);
> CREATE
> regression=# create table t2(f1 int);
> CREATE
> regression=# insert into t1 values(-1);
> INSERT 400599 1
> regression=# insert into t1 values(-2);
> INSERT 400600 1
> regression=# insert into t1 values(-3);
> INSERT 400601 1
> regression=# insert into t2 values(-1);
> INSERT 400602 1
> regression=# insert into t2 values(-2);
> INSERT 400603 1
> regression=# insert into t2 values(-3);
> INSERT 400604 1
> regression=# update t2 set f1 = count(*) from t1;
> UPDATE 1
> regression=# select * from t2;
>  f1
> ----
>  -2
>  -3
>   9
> (3 rows)
> 
> regression=#
> 
> This is certainly broken, but what's the correct behavior?

Shouldn't it be 9 because there is no join of t1 and t2?
I can also see 3 as a valid answer.

> Or how about this, which doesn't even use an aggregate:
> 
> regression=# update t2 set f1 = t1.f1 from t1;
> UPDATE 3
> regression=# select * from t2;
>  f1
> ----
>  -1
>  -1
>  -1
> (3 rows)
> 
> regression=#
> 
> That's surprising too, perhaps, but what would you have expected
> and why?

So it grabs the first match.  Seems reasonable because t1 returns more
than one row.

> 
> There's a reason why SQL99 forbids joins and aggregates in UPDATE ...
> they're not always well-defined.

Yes, I see that now.

> I had a proposal (GROUP BY ctid) in the older thread for fixing the
> aggregate misbehavior, but it doesn't solve the more general problem
> of a join that produces multiple matches for the same target row.
> Seems like that probably ought to draw an error.

Or a NOTICE stating a random row was chosen.

--  Bruce Momjian                        |  http://candle.pha.pa.us pgman@candle.pha.pa.us               |  (610)
853-3000+  If your life is a hard drive,     |  830 Blythe Avenue +  Christ can be your backup.        |  Drexel Hill,
Pennsylvania19026
 


pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: Minor buglet in update...from (I think)
Next
From: Tom Lane
Date:
Subject: Re: Minor buglet in update...from (I think)