pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Richard Huxton <dev@archonet.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-sql@postgresql.org
Subject: Re: Non-procedural field merging?
Date: Fri, 5 Jan 2001 18:09:33 -0000
Message-ID: <002501c07742$a9990aa0$1001a8c0@archonet.com> (raw)
References: <000f01c076fe$2e27b9a0$1001a8c0@archonet.com>
	<10278.978713629@sss.pgh.pa.us>

From: "Tom Lane" <tgl@sss.pgh.pa.us>

[snipped my Q about merging text fields from one table into another]

> You could do it with a user-defined aggregate function (initial
> value '' and transition function ||).  I am not sure that aggregates
> work in an intelligent way in UPDATE --- ie, I am not sure it would
> work to do
>
> update foo2 set d = catenate(foo.b) from foo where foo.a=foo2.c;

Actually, (to my surprise) it did work. I used:

richardh=> create aggregate catenate(sfunc1=textcat, basetype=text,
stype1=text, initcond1='');
CREATE
richardh=> select a,catenate(b) from foo group by a;
 a | catenate
---+----------
 1 | xxxyyy
(1 row)

Then tried the update - worked with no problem, noting that:

richardh=> update foo2 set d = catenate(foo.b) from foo where foo.a=foo2.c;
UPDATE 1
richardh=> select * from foo2;
 c |   d
---+--------
 1 | yyyxxx
(1 row)

The order is reversed between the select and the update! Important lesson in
the relationship between SQL and set theory noted (my college lecturers
would be proud of me ;-)

> I seem to recall some discussion concluding that that didn't have
> very well-defined semantics.

I can see how you'd have problems if you were expecting the aggregate to
return the same value on each run (a vacuum presumably could reorder the
values). In my case, this isn't important.

I must admit it didn't occur to me you could create your own aggregates
without resorting to C. Shame it's not a standard SQL feature.

Thanks Tom - don't know how you find the time to give so much help in the
lists.

- Richard Huxton




view thread (3+ messages)

Message-ID: <002501c07742$a9990aa0$1001a8c0@archonet.com>
Permalink:  ../002501c07742$a9990aa0$1001a8c0@archonet.com/
Also on:    postgresql.org/message-id/002501c07742$a9990aa0$1001a8c0@archonet.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-sql@postgresql.org
  Cc: dev@archonet.com, tgl@sss.pgh.pa.us
  Subject: Re: Non-procedural field merging?
  In-Reply-To: <002501c07742$a9990aa0$1001a8c0@archonet.com>
  References: <000f01c076fe$2e27b9a0$1001a8c0@archonet.com> <10278.978713629@sss.pgh.pa.us> <002501c07742$a9990aa0$1001a8c0@archonet.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox