Skip site navigation (1)
Skip section navigation (2)
## Re: Minor buglet in update...from (I think)

### In response to

### pgsql-hackers by date

> Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)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(at)candle(dot)pha(dot)pa(dot)us | (610) 853-3000 + If your life is a hard drive, | 830 Blythe Avenue + Christ can be your backup. | Drexel Hill, Pennsylvania 19026

- Re: Minor buglet in update...from (I think) at 2001-11-27 00:51:13 from Tom Lane

Next: From:Tom LaneDate:2001-11-27 01:05:06Subject: Re: insert/update/delete statements returning a query responsePrevious: From: Tom LaneDate: 2001-11-27 00:51:13Subject: Re: Minor buglet in update...from (I think)