Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 2EF78B5DBBE for ; Sat, 11 Jun 2011 12:39:16 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 29338-06 for ; Sat, 11 Jun 2011 15:39:08 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail42.e.nsc.no (mail42.e.nsc.no [193.213.115.42]) by mail.postgresql.org (Postfix) with ESMTP id 6783CB5D80A for ; Sat, 11 Jun 2011 12:39:07 -0300 (ADT) Received: from balapapa.localnet (168.173.16.62.customer.cdi.no [62.16.173.168]) (authenticated bits=0) by mail42.nsc.no (8.14.4/8.14.4) with ESMTP id p5BFd5eM009436 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Sat, 11 Jun 2011 17:39:06 +0200 (MEST) From: Leif Biberg Kristensen To: pgsql-sql@postgresql.org Subject: Re: Subselects not allowed? Date: Sat, 11 Jun 2011 17:39:04 +0200 User-Agent: KMail/1.13.7 (Linux/2.6.38-gentoo-r6; KDE/4.6.2; x86_64; ; ) References: <201106111701.54145.leif@solumslekt.org> <20110611152340.GA9062@tux> In-Reply-To: <20110611152340.GA9062@tux> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201106111739.04845.leif@solumslekt.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201106/39 X-Sequence-Number: 35505 On Saturday 11. June 2011 17.23.40 Andreas Kretschmer wrote: > Leif Biberg Kristensen wrote: > > Can anybody tell me why this doesn't work? > > > > pgslekt=> CREATE SEQUENCE sources_source_id_seq START WITH (SELECT > > MAX(source_id) FROM sources); > > ERROR: syntax error at or near "(" > > LINE 1: CREATE SEQUENCE sources_source_id_seq START WITH (SELECT MAX... > > > > ^ > > > > pgslekt=> > > > > regards, Leif > > You can't do that, but you can do this: > > test=# select * from foo; > i > ---- > 98 > 99 > (2 rows) > > Time: 0,146 ms > test=*# \ds seq_foo; > No matching relations found. > test=*# do $$ declare m int; begin select into m max(i) from foo; execute > 'create sequence seq_foo start with ' || m; end; $$; DO > Time: 1,115 ms > test=*# \ds seq_foo; > List of relations > Schema | Name | Type | Owner > --------+---------+----------+------------ > public | seq_foo | sequence | kretschmer > (1 row) > > test=*# select * from seq_foo; > sequence_name | last_value | start_value | increment_by | max_value > | min_value | cache_value | log_cnt | is_cycled | is_called > ---------------+------------+-------------+--------------+---------------- > -----+-----------+-------------+---------+-----------+----------- seq_foo > | 99 | 99 | 1 | 9223372036854775807 | > 1 | 1 | 1 | f | f (1 row) > > > > I'm using 9.1Beta, but it works since 9.0, see: > http://www.depesz.com/index.php/2009/11/01/waiting-for-8-5-do/#more-1535 Yes it works like a charm with 9.x, but it's not backwards compatible. That looks like a problem waiting to happen. But of course I can create a one- shot function and drop it afterwards. regards, Leif