<?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>WHILE LOOP &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/while-loop/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, 25 Jul 2017 14:12:49 +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: Don&#8217;t Create or Drop Temp Table in LOOP, otherwise will get OID does not exist</title>
		<link>https://www.dbrnd.com/2017/08/postgresql-dont-create-or-drop-temp-table-in-loop-otherwise-will-get-oid-does-not-exist/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 01 Aug 2017 16:16:33 +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[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[Temp Table]]></category>
		<category><![CDATA[WHILE LOOP]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=5729</guid>

					<description><![CDATA[Anuraag Veerapaneni &#8211; He is my work coordinator, and he got &#8220;OID does not exist&#8221; error in one of the function execution. Later we found that the problem was in the function code. The temp table was creating and dropping within the WHILE LOOP. Internally, WHILE LOOP also keeps one session, so the OBJECT ID [&#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%2F2017%2F08%2Fpostgresql-dont-create-or-drop-temp-table-in-loop-otherwise-will-get-oid-does-not-exist%2F&amp;linkname=PostgreSQL%3A%20Don%E2%80%99t%20Create%20or%20Drop%20Temp%20Table%20in%20LOOP%2C%20otherwise%20will%20get%20OID%20does%20not%20exist" 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%2F2017%2F08%2Fpostgresql-dont-create-or-drop-temp-table-in-loop-otherwise-will-get-oid-does-not-exist%2F&amp;linkname=PostgreSQL%3A%20Don%E2%80%99t%20Create%20or%20Drop%20Temp%20Table%20in%20LOOP%2C%20otherwise%20will%20get%20OID%20does%20not%20exist" 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%2F2017%2F08%2Fpostgresql-dont-create-or-drop-temp-table-in-loop-otherwise-will-get-oid-does-not-exist%2F&amp;linkname=PostgreSQL%3A%20Don%E2%80%99t%20Create%20or%20Drop%20Temp%20Table%20in%20LOOP%2C%20otherwise%20will%20get%20OID%20does%20not%20exist" 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%2F2017%2F08%2Fpostgresql-dont-create-or-drop-temp-table-in-loop-otherwise-will-get-oid-does-not-exist%2F&amp;linkname=PostgreSQL%3A%20Don%E2%80%99t%20Create%20or%20Drop%20Temp%20Table%20in%20LOOP%2C%20otherwise%20will%20get%20OID%20does%20not%20exist" 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%2F2017%2F08%2Fpostgresql-dont-create-or-drop-temp-table-in-loop-otherwise-will-get-oid-does-not-exist%2F&amp;linkname=PostgreSQL%3A%20Don%E2%80%99t%20Create%20or%20Drop%20Temp%20Table%20in%20LOOP%2C%20otherwise%20will%20get%20OID%20does%20not%20exist" 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%2F2017%2F08%2Fpostgresql-dont-create-or-drop-temp-table-in-loop-otherwise-will-get-oid-does-not-exist%2F&amp;linkname=PostgreSQL%3A%20Don%E2%80%99t%20Create%20or%20Drop%20Temp%20Table%20in%20LOOP%2C%20otherwise%20will%20get%20OID%20does%20not%20exist" 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%2F2017%2F08%2Fpostgresql-dont-create-or-drop-temp-table-in-loop-otherwise-will-get-oid-does-not-exist%2F&#038;title=PostgreSQL%3A%20Don%E2%80%99t%20Create%20or%20Drop%20Temp%20Table%20in%20LOOP%2C%20otherwise%20will%20get%20OID%20does%20not%20exist" data-a2a-url="https://www.dbrnd.com/2017/08/postgresql-dont-create-or-drop-temp-table-in-loop-otherwise-will-get-oid-does-not-exist/" data-a2a-title="PostgreSQL: Don’t Create or Drop Temp Table in LOOP, otherwise will get OID does not exist"></a></p><p><strong>Anuraag Veerapaneni</strong> &#8211; He is my work coordinator, and he got &#8220;OID does not exist&#8221; error in one of the function execution. </p>
<p>Later we found that the problem was in the function code. The temp table was creating and dropping within the WHILE LOOP. </p>
<blockquote><p>Internally, WHILE LOOP also keeps one session, so the OBJECT ID of dropped temp table still there in the session of WHILE LOOP.<br />
After dropping the temp table, it creates a new temp table in WHILE LOOP with the new object id but dropped temp table object id is still in the session so while selecting a temp table it will search for old Temp table which already dropped.</p></blockquote>
<p>Our advice: please never write code to create or drop temp tables in the WHILE LOOP. </p>
<p><strong>For better understanding, please visit below demonstration:</strong></p>
<p><strong> Create a test function:</strong></p><pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION fn_check_drop_temp_table()
RETURNS integer
AS
$dbrnd$
 
DECLARE i INT;
j OID;
z OID;
b OID;
a OID;
 
BEGIN   
    i:=0;   
    z:=Coalesce((SELECT relfilenode FROM pg_class
    WHERE relname LIKE '%tmp_drop_test%'),111111);
   
    RAISE NOTICE '%','No table found...'|| z;
   
    DROP TABLE IF EXISTS tmp_a;
    CREATE TABLE tmp_a (col1 int);
       
    WHILE i&lt;=2
    LOOP   
		j:=coalesce((SELECT relfilenode FROM pg_class
		WHERE relname LIKE '%tmp_drop_test%'),1);
		
		RAISE NOTICE '%','1= No data, Other value= OID Data --'|| j;
	   
		DROP TABLE IF EXISTS tmp_drop_test; -- Now drop temp table
		CREATE temp TABLE tmp_drop_test AS SELECT 1 AS int1; -- Create temp table again with same name
	   
		a:=coalesce((SELECT relfilenode FROM pg_class
		WHERE relname LIKE '%tmp_drop_test%'),1);
	   
		RAISE NOTICE '%','After creating temp table - OID of temp table:'|| a; 
		
		INSERT INTO tmp_a
		SELECT * FROM tmp_drop_test;
						
		i:=i+1;   
    END LOOP;   
                RETURN 1;
END;
$dbrnd$
LANGUAGE PLPGSQL;</pre><p> </p>
<p><strong>Now, execute above function:</strong></p>
<pre class="crayon-plain-tag">SELECT *FROM fn_check_drop_temp_table();</pre> </p>
<p><strong>Got error in the result:</strong></p>
<pre class="crayon-plain-tag">NOTICE:  No table found...111111
 
NOTICE:  1= No data, Other value= OID Data --1
 
NOTICE:  After creating temp table - OID of temp table:27102093
 
NOTICE:  1= No data, Other value= OID Data --27102093
 
NOTICE:  After creating temp table - OID of temp table:27102120
 
 
ERROR:  relation with OID 27102093 does not exist
CONTEXT:  SQL statement "INSERT INTO tmp_a SELECT * FROM tmp_drop_test"
PL/pgSQL function "fn_check_drop_temp_table" line 33 at SQL statement</pre> </p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>PostgreSQL: Shell script to store psql result in RECORD ARRAY using WHILE LOOP</title>
		<link>https://www.dbrnd.com/2017/02/postgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop/</link>
					<comments>https://www.dbrnd.com/2017/02/postgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 28 Feb 2017 16:16:46 +0000</pubDate>
				<category><![CDATA[Bash Shell Script]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[Bash shell script]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[linux]]></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 Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[psql]]></category>
		<category><![CDATA[RECORD ARRAY]]></category>
		<category><![CDATA[Shell script]]></category>
		<category><![CDATA[WHILE LOOP]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4664</guid>

					<description><![CDATA[Now a day, I am developing different Linux shell scripts to monitor the PostgreSQL database server. Later, I will share those shell scripts. But before that, I want to share few basic things which we can embed in any one of our Shell scripts. In this post, I am sharing a small shell script to [&#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%2F2017%2F02%2Fpostgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop%2F&amp;linkname=PostgreSQL%3A%20Shell%20script%20to%20store%20psql%20result%20in%20RECORD%20ARRAY%20using%20WHILE%20LOOP" 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%2F2017%2F02%2Fpostgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop%2F&amp;linkname=PostgreSQL%3A%20Shell%20script%20to%20store%20psql%20result%20in%20RECORD%20ARRAY%20using%20WHILE%20LOOP" 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%2F2017%2F02%2Fpostgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop%2F&amp;linkname=PostgreSQL%3A%20Shell%20script%20to%20store%20psql%20result%20in%20RECORD%20ARRAY%20using%20WHILE%20LOOP" 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%2F2017%2F02%2Fpostgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop%2F&amp;linkname=PostgreSQL%3A%20Shell%20script%20to%20store%20psql%20result%20in%20RECORD%20ARRAY%20using%20WHILE%20LOOP" 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%2F2017%2F02%2Fpostgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop%2F&amp;linkname=PostgreSQL%3A%20Shell%20script%20to%20store%20psql%20result%20in%20RECORD%20ARRAY%20using%20WHILE%20LOOP" 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%2F2017%2F02%2Fpostgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop%2F&amp;linkname=PostgreSQL%3A%20Shell%20script%20to%20store%20psql%20result%20in%20RECORD%20ARRAY%20using%20WHILE%20LOOP" 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%2F2017%2F02%2Fpostgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop%2F&#038;title=PostgreSQL%3A%20Shell%20script%20to%20store%20psql%20result%20in%20RECORD%20ARRAY%20using%20WHILE%20LOOP" data-a2a-url="https://www.dbrnd.com/2017/02/postgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop/" data-a2a-title="PostgreSQL: Shell script to store psql result in RECORD ARRAY using WHILE LOOP"></a></p><p>Now a day, I am developing different Linux shell scripts to monitor the PostgreSQL database server. Later, I will share those shell scripts.</p>
<p>But before that, I want to share few basic things which we can embed in any one of our Shell scripts.</p>
<p>In this post, I am sharing a small shell script to iterate the result of psql SELECT using RECORD ARRAY and WHILE LOOP.<br />
Using that, we can do further processing and validation on each record.</p>
<p><strong>Script to iterate the table result:</strong><br />
<pre class="crayon-plain-tag">#!/bin/bash

DB_NAME=$1
psql -d ${DB_NAME} -At -c "select rno, studname, studclass from tbl_students" \
    | while read -a Record ; do
    rno=${Record[0]}
    studname=${Record[1]}
    studclass=${Record[2]}

    echo "${rno} ${studname} ${studclass}"
done</pre>
<strong>The result:</strong><br />
<pre class="crayon-plain-tag">rno | studname
-----+----------+-----------
1 | Anvesh
2 | Neevan
3 | Toby
5 | Roy
4 | Jenny
6 | Kaviy
7 | Martin
8 | Laxmi
9 | Nion
(9 rows)</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2017/02/postgresql-shell-script-to-store-psql-result-in-record-array-using-while-loop/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
