<?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>paging &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/paging/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>Mon, 20 Nov 2017 05:55:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL Paging Query using LIMIT and OFFSET</title>
		<link>https://www.dbrnd.com/2015/05/postgresql-paging-query-limit-offset/</link>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Wed, 27 May 2015 16:59:50 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[limit]]></category>
		<category><![CDATA[offset]]></category>
		<category><![CDATA[paging]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=194</guid>

					<description><![CDATA[In this post, I am going to demonstrate the PostgreSQL Pagination or Custom paging query using OFFSET and LIMIT. Whenever thousands or millions of rows are returning from the database at that time paging is required. Using custom paging we can return only limited data for a particular page. For example, database stored procedure returned [&#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%2F05%2Fpostgresql-paging-query-limit-offset%2F&amp;linkname=PostgreSQL%20Paging%20Query%20using%20LIMIT%20and%20OFFSET" 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%2F05%2Fpostgresql-paging-query-limit-offset%2F&amp;linkname=PostgreSQL%20Paging%20Query%20using%20LIMIT%20and%20OFFSET" 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%2F05%2Fpostgresql-paging-query-limit-offset%2F&amp;linkname=PostgreSQL%20Paging%20Query%20using%20LIMIT%20and%20OFFSET" 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%2F05%2Fpostgresql-paging-query-limit-offset%2F&amp;linkname=PostgreSQL%20Paging%20Query%20using%20LIMIT%20and%20OFFSET" 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%2F05%2Fpostgresql-paging-query-limit-offset%2F&amp;linkname=PostgreSQL%20Paging%20Query%20using%20LIMIT%20and%20OFFSET" 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%2F05%2Fpostgresql-paging-query-limit-offset%2F&amp;linkname=PostgreSQL%20Paging%20Query%20using%20LIMIT%20and%20OFFSET" 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%2F05%2Fpostgresql-paging-query-limit-offset%2F&#038;title=PostgreSQL%20Paging%20Query%20using%20LIMIT%20and%20OFFSET" data-a2a-url="https://www.dbrnd.com/2015/05/postgresql-paging-query-limit-offset/" data-a2a-title="PostgreSQL Paging Query using LIMIT and OFFSET"></a></p><p>In this post, I am going to demonstrate the PostgreSQL Pagination or Custom paging query using OFFSET and LIMIT.</p>
<blockquote><p>Whenever thousands or millions of rows are returning from the database at that time paging is required.</p>
<p>Using custom paging we can return only limited data for a particular page.</p>
<p>For example, database stored procedure returned 10,00,00 records at one call.<br />
How can you arrange this many data into the application grid or any report?</p>
<p>In this kind of requirement, database paging is playing an important role.</p></blockquote>
<p>The Application has to call a stored procedure using two primary parameters.	</p>
<blockquote><p>Page number<br />
	Page size</p></blockquote>
<p>PostgreSQL provides powerful <b>LIMIT </b>and <b>OFFSET </b>features.</p>
<p><b>LIMIT and OFFSET fetches only small portion of rows from a primary the result set. </b></p>
<p>Using LIMIT you can set a total number of rows which are limited.</p>
<p>Using OFFSET you can skip a number of rows which number is defined by OFFSET.</p>
<p><a href="http://www.postgresql.org/docs/7.3/static/queries-limit.html" target="_blank">For more details please visit this official document.</a></p>
<p>Below is a stored function for Custom Paging. </p>
<p><b>Below, is a created one Employee table with 10 rows.</b></p>
<pre class="crayon-plain-tag">CREATE TABLE Employee
(
 EmpID SERIAL
 ,EmpName CHARACTER VARYING(50)
 ,Gender CHAR(1)
 ,AGE SMALLINT
);
INSERT INTO Employee 
(
 EmpName 
 ,Gender 
 ,AGE 
)
VALUES 
 ('Anvesh','M',27)
 ,('Mohan','M',30)
 ,('Roy','M',31)
 ,('Meera','F',27)
 ,('Richa','F',26)
 ,('Martin','M',35)
 ,('Mahesh','M',38)
 ,('Paresh','M',22)
 ,('Alina','F',21)
 ,('Alex','M',24);</pre>
<p><b>Below is custom paging stored procedure:</b></p>
<pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION fn_GetEmployeeData
(
 Paging_PageSize INTEGER = NULL
 ,Paging_PageNumber INTEGER = NULL   
)
RETURNS TABLE 
( 
 outEmpID INTEGER
 ,outEmpName CHARACTER VARYING
 ,outGender CHAR(1)
 ,outAge SMALLINT
) AS
$BODY$
DECLARE PageNumber BIGINT;
 
BEGIN
 /* ***************************************************************
 Construct Custom paging parameter...
 **************************************************************** */
 IF (paging_pagesize IS NOT NULL AND paging_pagenumber IS NOT NULL) THEN
  PageNumber := (Paging_PageSize * (Paging_PageNumber-1)); 
 END IF; 
  
 /* ************************************************
 Custome paging SQL Query construction.......
 ************************************************ */
 RETURN QUERY
 SELECT
  EmpID
  ,EmpName
  ,Gender
  ,Age
 FROM public.Employee
 ORDER BY EmpID
 LIMIT Paging_PageSize
 OFFSET PageNumber; 

 EXCEPTION WHEN OTHERS THEN 
 RAISE;
END;
$BODY$
LANGUAGE 'plpgsql';</pre>
<p><b>Now execute some sample:</b></p>
<pre class="crayon-plain-tag">SELECT *FROM public.fn_GetEmployeeData(2,2)</pre>
Above sample<b> </b>only return two row at second page, mean row number 3 and 4.</p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/05/PagingResult1.png"><img decoding="async" class="aligncenter wp-image-199 size-full" title="PostgreSQL LIMIT OFFSET Paging" src="https://www.dbrnd.com/wp-content/uploads/2015/05/PagingResult1.png" alt="PagingResult1" width="358" height="132" srcset="https://www.dbrnd.com/wp-content/uploads/2015/05/PagingResult1.png 358w, https://www.dbrnd.com/wp-content/uploads/2015/05/PagingResult1-300x111.png 300w, https://www.dbrnd.com/wp-content/uploads/2015/05/PagingResult1-150x55.png 150w" sizes="(max-width: 358px) 100vw, 358px" /></a></p><pre class="crayon-plain-tag">SELECT *FROM public.fn_GetEmployeeData(4,2)</pre><p>
Above sample only return four row at second page, mean row number 5 to 8</p>
<p><a href="https://www.dbrnd.com/wp-content/uploads/2015/05/PagingResult2.png"><img decoding="async" class="aligncenter wp-image-200 size-full" title="PostgreSQL LIMIT OFFSET Paging " src="https://www.dbrnd.com/wp-content/uploads/2015/05/PagingResult2.png" alt="PagingResult2" width="346" height="143" srcset="https://www.dbrnd.com/wp-content/uploads/2015/05/PagingResult2.png 346w, https://www.dbrnd.com/wp-content/uploads/2015/05/PagingResult2-300x124.png 300w" sizes="(max-width: 346px) 100vw, 346px" /></a></p>
<blockquote><p><span style="color: #ff0000;"><b>Please Note:<br />
Whenever you are using LIMIT at that time do not forget to use default ORDER BY which helps LIMIT to restricting data into a particular order. </b></span></p></blockquote>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
