<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>returning &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/returning/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Tue, 19 Sep 2017 08:14:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.6</generator>
	<item>
		<title>PostgreSQL: SELECT Last Inserted ID or Sequence Value</title>
		<link>https://www.dbrnd.com/2015/11/postgresql-select-last-inserted-id-or-sequence-value/</link>
					<comments>https://www.dbrnd.com/2015/11/postgresql-select-last-inserted-id-or-sequence-value/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Sun, 08 Nov 2015 16:08:54 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[returning]]></category>
		<category><![CDATA[sequence]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=917</guid>

					<description><![CDATA[I found that PostgreSQL professionals are wondering about last inserted id or sequence value of PostgreSQL Tables. You can find an N number of articles and discussion on this point. Let me clear describe this topic in my words, because this is important for PostgreSQL Database Developers. The PostgreSQL provideds Sequence object and you can [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F11%2Fpostgresql-select-last-inserted-id-or-sequence-value%2F&amp;linkname=PostgreSQL%3A%20SELECT%20Last%20Inserted%20ID%20or%20Sequence%20Value" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F11%2Fpostgresql-select-last-inserted-id-or-sequence-value%2F&amp;linkname=PostgreSQL%3A%20SELECT%20Last%20Inserted%20ID%20or%20Sequence%20Value" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F11%2Fpostgresql-select-last-inserted-id-or-sequence-value%2F&amp;linkname=PostgreSQL%3A%20SELECT%20Last%20Inserted%20ID%20or%20Sequence%20Value" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F11%2Fpostgresql-select-last-inserted-id-or-sequence-value%2F&amp;linkname=PostgreSQL%3A%20SELECT%20Last%20Inserted%20ID%20or%20Sequence%20Value" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F11%2Fpostgresql-select-last-inserted-id-or-sequence-value%2F&amp;linkname=PostgreSQL%3A%20SELECT%20Last%20Inserted%20ID%20or%20Sequence%20Value" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F11%2Fpostgresql-select-last-inserted-id-or-sequence-value%2F&amp;linkname=PostgreSQL%3A%20SELECT%20Last%20Inserted%20ID%20or%20Sequence%20Value" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2015%2F11%2Fpostgresql-select-last-inserted-id-or-sequence-value%2F&#038;title=PostgreSQL%3A%20SELECT%20Last%20Inserted%20ID%20or%20Sequence%20Value" data-a2a-url="https://www.dbrnd.com/2015/11/postgresql-select-last-inserted-id-or-sequence-value/" data-a2a-title="PostgreSQL: SELECT Last Inserted ID or Sequence Value"></a></p><p>I found that PostgreSQL professionals are wondering about last inserted id or sequence value of PostgreSQL Tables. </p>
<p>You can find an N number of articles and discussion on this point. </p>
<p>Let me clear describe this topic in my words, because this is important for PostgreSQL Database Developers.</p>
<blockquote><p>The PostgreSQL provideds Sequence object and you can create a Sequence by assigning SERIAL or BIGSERIAL Datatype to the columns. </p></blockquote>
<p>You can use CURRVAL and LASTVAL to get the last inserted or generated ID by the Sequence object. </p>
<p>These both are concurrently safe, and behaviour of Postgres Sequence is designed such a way that different sessions will not interfere with each other. </p>
<blockquote><p>But sometimes, I found the wrong ID by CURRVAL or LASTVAL because there is some additional trigger rule written like when you insert one row in X table, some other row also require to insert in Y table using an INSERT Trigger.
</p></blockquote>
<p>When some extra insertion or load of insertion is going on by the different sources, LASTVAL and CURRVAL return the wrong ID. This is the very rare situation, but still, the risk is there. </p>
<p><strong>The best solution is &#8211;  PostgreSQL INSERT RETURNING:</strong><br />
Using INSERT RETURNING, you can select last inserted id for that only INSERT at the time of insertion. </p>
<p><strong>Below is a small demo:</strong></p>
<p><strong>Create a sample table:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_TestReturnID 
(
	Rno SERIAL 
	,Name CHARACTER VARYING
	,Address CHARACTER VARYING
	,CONSTRAINT pk_tbl_TestReturnID_Rno PRIMARY KEY(Rno)
);</pre> </p>
<p><strong>Insertion with RETURNING a last inserted value:</strong></p>
<pre class="crayon-plain-tag">INSERT INTO tbl_TestReturnID (Name,Address) 
VALUES ('Anvesh','Hyderabad') RETURNING Rno;</pre> </p>
<p>After executing above INSERT statement, you will get the last inserted ID.<br />
My Suggestion is to use RETURNING instead of CURRVAL and LASTVAL. </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/11/postgresql-select-last-inserted-id-or-sequence-value/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
