<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: getting multiple values from sequences</title>
	<atom:link href="http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/</link>
	<description></description>
	<lastBuildDate>Mon, 06 Sep 2010 15:06:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Helge</title>
		<link>http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/comment-page-1/#comment-25483</link>
		<dc:creator>Helge</dc:creator>
		<pubDate>Fri, 21 Mar 2008 12:18:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/#comment-25483</guid>
		<description>There is one more (little known) alternative to the generate_series() which was discussed in the matching IRC chat ;-)
It is INSERT with the RETURNING clause (apparently available since 8.2):

  INSERT INTO master table VALUES ( ... ), ( ... ), ( ...)
    RETURNING pkey;

This returns the primary key assigned by the database for each inserted record. Which can then be used to construct subsequent INSERTs in belongs-to style tables (eg addresses of contacts).
(w/o the RETURNING feature you would need to do individual INSERTs to grab generated pkeys using CURRVAL)

Basic flow with RETURNING is:
a) build INSERT statement for master records
BEGIN;
b) INSERT master records, RETURNING ids
c) collect ids and build INSERT statements for child records [O(N)]
d) INSERT child records
COMMIT;

Instead of:
0) SELECT nextval FROM generate_series [O(N)]
a) build INSERT statement for master records with IDs
b) build INSERT statements for child records
BEGIN;
c) INSERT master records, INSERT child records
COMMIT;


Advantage over generate_series(): one less SQL statement send to server. Disadvantage: transaction (including locks on master) is kept open longer since step c) can take a while for large import sets. The generate_series() runs completely out of TX bounds.


Anyways, the multi_nextval() is awesome for large sets (O(1), out of TX). Thanks depesz!</description>
		<content:encoded><![CDATA[<p>There is one more (little known) alternative to the generate_series() which was discussed in the matching IRC chat <img src='http://www.depesz.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /><br />
It is INSERT with the RETURNING clause (apparently available since 8.2):</p>
<p>  INSERT INTO master table VALUES ( &#8230; ), ( &#8230; ), ( &#8230;)<br />
    RETURNING pkey;</p>
<p>This returns the primary key assigned by the database for each inserted record. Which can then be used to construct subsequent INSERTs in belongs-to style tables (eg addresses of contacts).<br />
(w/o the RETURNING feature you would need to do individual INSERTs to grab generated pkeys using CURRVAL)</p>
<p>Basic flow with RETURNING is:<br />
a) build INSERT statement for master records<br />
BEGIN;<br />
b) INSERT master records, RETURNING ids<br />
c) collect ids and build INSERT statements for child records [O(N)]<br />
d) INSERT child records<br />
COMMIT;</p>
<p>Instead of:<br />
0) SELECT nextval FROM generate_series [O(N)]<br />
a) build INSERT statement for master records with IDs<br />
b) build INSERT statements for child records<br />
BEGIN;<br />
c) INSERT master records, INSERT child records<br />
COMMIT;</p>
<p>Advantage over generate_series(): one less SQL statement send to server. Disadvantage: transaction (including locks on master) is kept open longer since step c) can take a while for large import sets. The generate_series() runs completely out of TX bounds.</p>
<p>Anyways, the multi_nextval() is awesome for large sets (O(1), out of TX). Thanks depesz!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: przemek tomczyk</title>
		<link>http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/comment-page-1/#comment-25481</link>
		<dc:creator>przemek tomczyk</dc:creator>
		<pubDate>Fri, 21 Mar 2008 09:39:01 +0000</pubDate>
		<guid isPermaLink="false">http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/#comment-25481</guid>
		<description>nah, same flaw as in 1st comment :-)</description>
		<content:encoded><![CDATA[<p>nah, same flaw as in 1st comment <img src='http://www.depesz.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: przemek tomczyk</title>
		<link>http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/comment-page-1/#comment-25480</link>
		<dc:creator>przemek tomczyk</dc:creator>
		<pubDate>Fri, 21 Mar 2008 09:33:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/#comment-25480</guid>
		<description>and what about &quot;basic approach&quot; modified not to send values from db to client/application:

select min(nextval(&#039;seq&#039;)) from generate_series(1,1000);
?</description>
		<content:encoded><![CDATA[<p>and what about &#8220;basic approach&#8221; modified not to send values from db to client/application:</p>
<p>select min(nextval(&#8216;seq&#8217;)) from generate_series(1,1000);<br />
?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: depesz</title>
		<link>http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/comment-page-1/#comment-25470</link>
		<dc:creator>depesz</dc:creator>
		<pubDate>Thu, 20 Mar 2008 13:47:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/#comment-25470</guid>
		<description>@maniek:
no, it doesn&#039;t. sequences are outside of transactions.</description>
		<content:encoded><![CDATA[<p>@maniek:<br />
no, it doesn&#8217;t. sequences are outside of transactions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: maniek</title>
		<link>http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/comment-page-1/#comment-25469</link>
		<dc:creator>maniek</dc:creator>
		<pubDate>Thu, 20 Mar 2008 13:24:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/#comment-25469</guid>
		<description>Actually i think (have not checked) that you don`t need the advisory locks in this situation - the alter statement does get an exclusive lock on the sequence till the end of transaction anyway</description>
		<content:encoded><![CDATA[<p>Actually i think (have not checked) that you don`t need the advisory locks in this situation &#8211; the alter statement does get an exclusive lock on the sequence till the end of transaction anyway</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: depesz</title>
		<link>http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/comment-page-1/#comment-25468</link>
		<dc:creator>depesz</dc:creator>
		<pubDate>Thu, 20 Mar 2008 12:41:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/#comment-25468</guid>
		<description>@Steve Haslam:
of course i can, but it gets complex as we insert to many tables.

anyway - yes, it&#039;s a perfectly viable solution for import question.</description>
		<content:encoded><![CDATA[<p>@Steve Haslam:<br />
of course i can, but it gets complex as we insert to many tables.</p>
<p>anyway &#8211; yes, it&#8217;s a perfectly viable solution for import question.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Haslam</title>
		<link>http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/comment-page-1/#comment-25467</link>
		<dc:creator>Steve Haslam</dc:creator>
		<pubDate>Thu, 20 Mar 2008 12:34:19 +0000</pubDate>
		<guid isPermaLink="false">http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/#comment-25467</guid>
		<description>Ah yes, I see what you mean. So maybe a &quot;safe&quot; compromise would be to copy the data to a temp table and then use insert to essentially join the data up with nextval() calls for each row... which presumably would only be faster than inserting them manually because of having less network turnarounds...</description>
		<content:encoded><![CDATA[<p>Ah yes, I see what you mean. So maybe a &#8220;safe&#8221; compromise would be to copy the data to a temp table and then use insert to essentially join the data up with nextval() calls for each row&#8230; which presumably would only be faster than inserting them manually because of having less network turnarounds&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: depesz</title>
		<link>http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/comment-page-1/#comment-25466</link>
		<dc:creator>depesz</dc:creator>
		<pubDate>Thu, 20 Mar 2008 12:18:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/#comment-25466</guid>
		<description>no, because there is no guarateee that generated numbers will be consecutive.

i.e. it might happen that you will get:
10,11,12,15,17,18,19,...</description>
		<content:encoded><![CDATA[<p>no, because there is no guarateee that generated numbers will be consecutive.</p>
<p>i.e. it might happen that you will get:<br />
10,11,12,15,17,18,19,&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve Haslam</title>
		<link>http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/comment-page-1/#comment-25465</link>
		<dc:creator>Steve Haslam</dc:creator>
		<pubDate>Thu, 20 Mar 2008 12:17:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.depesz.com/index.php/2008/03/20/getting-multiple-values-from-sequences/#comment-25465</guid>
		<description>Couldn&#039;t you do:

create temp table generated(id int);

insert into generated(id)
select nextval(&#039;seq&#039;) from generate_series(1,1000);

select min(id) as first_id, max(id) as last_id from generated;

drop table generated;

?</description>
		<content:encoded><![CDATA[<p>Couldn&#8217;t you do:</p>
<p>create temp table generated(id int);</p>
<p>insert into generated(id)<br />
select nextval(&#8216;seq&#8217;) from generate_series(1,1000);</p>
<p>select min(id) as first_id, max(id) as last_id from generated;</p>
<p>drop table generated;</p>
<p>?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
