<?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>pg_stat_user_tables &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/pg_stat_user_tables/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>Fri, 22 Sep 2017 21:44:27 +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: Script to find total Live Tuples and Dead Tuples (Row) of a Table</title>
		<link>https://www.dbrnd.com/2016/10/postgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance/</link>
					<comments>https://www.dbrnd.com/2016/10/postgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Tue, 04 Oct 2016 16:16:17 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[PostgreSQL DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[dead tuples]]></category>
		<category><![CDATA[live tuples]]></category>
		<category><![CDATA[Multi version concurrency control]]></category>
		<category><![CDATA[MVCC]]></category>
		<category><![CDATA[pg_class]]></category>
		<category><![CDATA[pg_stat_get_dead_tuples()]]></category>
		<category><![CDATA[pg_stat_get_live_tuples()]]></category>
		<category><![CDATA[pg_stat_user_tables]]></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[VACUUM]]></category>
		<category><![CDATA[VACUUM FULL]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=3701</guid>

					<description><![CDATA[This is one of the very important post for all PostgreSQL Database Professionals. In this post, I am sharing a small, but very powerful script to know about the Live Tuples (Rows) and Dead Tuples (Rows) of the PostgreSQL Object like: Tables and Indexes. If you don&#8217;t know about the MVCC (Multi Version Concurrency Control), [&#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%2F2016%2F10%2Fpostgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20total%20Live%20Tuples%20and%20Dead%20Tuples%20%28Row%29%20of%20a%20Table" 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%2F2016%2F10%2Fpostgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20total%20Live%20Tuples%20and%20Dead%20Tuples%20%28Row%29%20of%20a%20Table" 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%2F2016%2F10%2Fpostgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20total%20Live%20Tuples%20and%20Dead%20Tuples%20%28Row%29%20of%20a%20Table" 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%2F2016%2F10%2Fpostgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20total%20Live%20Tuples%20and%20Dead%20Tuples%20%28Row%29%20of%20a%20Table" 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%2F2016%2F10%2Fpostgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20total%20Live%20Tuples%20and%20Dead%20Tuples%20%28Row%29%20of%20a%20Table" 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%2F2016%2F10%2Fpostgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance%2F&amp;linkname=PostgreSQL%3A%20Script%20to%20find%20total%20Live%20Tuples%20and%20Dead%20Tuples%20%28Row%29%20of%20a%20Table" 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%2F2016%2F10%2Fpostgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance%2F&#038;title=PostgreSQL%3A%20Script%20to%20find%20total%20Live%20Tuples%20and%20Dead%20Tuples%20%28Row%29%20of%20a%20Table" data-a2a-url="https://www.dbrnd.com/2016/10/postgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance/" data-a2a-title="PostgreSQL: Script to find total Live Tuples and Dead Tuples (Row) of a Table"></a></p><p>This is one of the very important post for all PostgreSQL Database Professionals. </p>
<blockquote><p>In this post, I am sharing a small, but very powerful script to know about the Live Tuples (Rows) and Dead Tuples (Rows) of the PostgreSQL Object like: Tables and Indexes. </p></blockquote>
<p>If you don&#8217;t know about the MVCC (Multi Version Concurrency Control), Please visit this article. </p>
<blockquote data-secret="xsJ3wB5C2a" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2015/05/what-is-mvcc-multi-version-concurrency-control/">What is Multi Version Concurrency Control (MVCC)</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/05/what-is-mvcc-multi-version-concurrency-control/embed/#?secret=xsJ3wB5C2a" data-secret="xsJ3wB5C2a" width="600" height="338" title="&#8220;What is Multi Version Concurrency Control (MVCC)&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<blockquote><p>PostgreSQL is based on MVCC Architecture. </p>
<p>In MVCC Architecture, When you update or delete any row, Internally It creates the new row and mark old row as unused. </p>
<p>This kind of data, we call as Dead Tuples or Dead Rows. (We can also say like, This is an internal fragmentation).
</p></blockquote>
<p>Periodically, We should find dead rows of the object and we should remove it using VACUUM techniques of PostgreSQL.<br />
By this way, we can increase the overall performance of PostgreSQL Database Server. </p>
<blockquote data-secret="LxJHZJGjlB" class="wp-embedded-content"><p><a href="https://www.dbrnd.com/2015/11/postgresql-short-note-on-vacuum-vacuum-full-and-analyze/">PostgreSQL: Short note on VACUUM, VACUUM FULL and ANALYZE</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/11/postgresql-short-note-on-vacuum-vacuum-full-and-analyze/embed/#?secret=LxJHZJGjlB" data-secret="LxJHZJGjlB" width="600" height="338" title="&#8220;PostgreSQL: Short note on VACUUM, VACUUM FULL and ANALYZE&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p><strong>Find out Live Tuples or Dead Tuples using two different scripts. </strong></p>
<p><strong>Using system function and pg_class:</strong></p>
<pre class="crayon-plain-tag">SELECT 
	relname AS ObjectName
	,pg_stat_get_live_tuples(c.oid) AS LiveTuples
	,pg_stat_get_dead_tuples(c.oid) AS DeadTuples
FROM pg_class c;</pre> </p>
<p><strong>Using pg_stat_user_tables:</strong></p>
<pre class="crayon-plain-tag">SELECT 
	relname AS TableName
	,n_live_tup AS LiveTuples
	,n_dead_tup AS DeadTuples
FROM pg_stat_user_tables;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2016/10/postgresql-script-to-find-total-live-tuples-and-dead-tuples-row-of-a-table-execute-vacuum-remove-fragmentation-improve-performance/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Fast way to find the row count of a Table</title>
		<link>https://www.dbrnd.com/2015/10/postgresql-fast-way-to-find-the-row-count-of-a-table/</link>
					<comments>https://www.dbrnd.com/2015/10/postgresql-fast-way-to-find-the-row-count-of-a-table/#comments</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Thu, 08 Oct 2015 19:16:00 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[PostgreSQL DBA Script]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[pg_class]]></category>
		<category><![CDATA[pg_stat_user_tables]]></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[row count]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=826</guid>

					<description><![CDATA[If a table has a 5000 or 500000 or 5000000000 records and the requirement is to find the total row count of the table, most of the Database Developer always executes COUNT(*) for getting the row count. I found that many of our team members also execute COUNT(*), but just imagine that table has 5000000000 [&#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%2F10%2Fpostgresql-fast-way-to-find-the-row-count-of-a-table%2F&amp;linkname=PostgreSQL%3A%20Fast%20way%20to%20find%20the%20row%20count%20of%20a%20Table" 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%2F10%2Fpostgresql-fast-way-to-find-the-row-count-of-a-table%2F&amp;linkname=PostgreSQL%3A%20Fast%20way%20to%20find%20the%20row%20count%20of%20a%20Table" 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%2F10%2Fpostgresql-fast-way-to-find-the-row-count-of-a-table%2F&amp;linkname=PostgreSQL%3A%20Fast%20way%20to%20find%20the%20row%20count%20of%20a%20Table" 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%2F10%2Fpostgresql-fast-way-to-find-the-row-count-of-a-table%2F&amp;linkname=PostgreSQL%3A%20Fast%20way%20to%20find%20the%20row%20count%20of%20a%20Table" 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%2F10%2Fpostgresql-fast-way-to-find-the-row-count-of-a-table%2F&amp;linkname=PostgreSQL%3A%20Fast%20way%20to%20find%20the%20row%20count%20of%20a%20Table" 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%2F10%2Fpostgresql-fast-way-to-find-the-row-count-of-a-table%2F&amp;linkname=PostgreSQL%3A%20Fast%20way%20to%20find%20the%20row%20count%20of%20a%20Table" 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%2F10%2Fpostgresql-fast-way-to-find-the-row-count-of-a-table%2F&#038;title=PostgreSQL%3A%20Fast%20way%20to%20find%20the%20row%20count%20of%20a%20Table" data-a2a-url="https://www.dbrnd.com/2015/10/postgresql-fast-way-to-find-the-row-count-of-a-table/" data-a2a-title="PostgreSQL: Fast way to find the row count of a Table"></a></p><p>If a table has a 5000 or 500000 or 5000000000 records and the requirement is to find the total row count of the table, most of the Database Developer always executes COUNT(*) for getting the row count.</p>
<p>I found that many of our team members also execute COUNT(*), but just imagine that table has 5000000000 rows and COUNT(*) is taking a long time for getting the number of records. </p>
<p>The counting of the rows in such a big table always creates the performance issue and its also required I/O operation.</p>
<blockquote><p><strong>If you need exact row count for a given time, COUNT(*) is mandatory.<br />
But you can speed up this dramatically if the count does not have to be exact.<br />
You can use a metadata table or statistical table to find the row count, which is quite same as real row count.</strong></p></blockquote>
<p>Because the nature of MVCC, sometimes you can find the difference between actual record count and statistical table&#8217;s record count. </p>
<blockquote class="wp-embedded-content" data-secret="XCAWLM8eZm"><p><a href="https://www.dbrnd.com/2015/05/what-is-mvcc-multi-version-concurrency-control/">What is Multi Version Concurrency Control (MVCC)</a></p></blockquote>
<p><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted"  src="https://www.dbrnd.com/2015/05/what-is-mvcc-multi-version-concurrency-control/embed/#?secret=XCAWLM8eZm" data-secret="XCAWLM8eZm" width="600" height="338" title="&#8220;What is Multi Version Concurrency Control (MVCC)&#8221; &#8212; Database Research &amp; Development" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></p>
<p>You can easily find roughly row count using statistical tables within 1 second. </p>
<blockquote><p>
I tested and compared the two results in my local machine with the row count of 5000000000.</p>
<p>My COUNT (*) returns a result after 8 to 10 minutes and also taken 10% to 25% CPU and Memory.<br />
After this, I found row count from the statistical table, and it didn&#8217;t take even one second.</p>
<p><strong>But I found the little difference between both the count. The count of a statistical table is higher than the actual count. (12585-row count is greater) because of MVCC.</strong></p>
<p><strong> You should configure auto-vacuum and analyze on the table.</strong></p>
<p><strong> I executed vacuum and analyze on the table, and now my count is same. </strong></p></blockquote>
<p>I would suggest, please use the statistical table for the row counts.<br />
I am providing two different scripts for finding the rough row count in the PostgreSQL.</p>
<pre class="crayon-plain-tag">SELECT reltuples::bigint AS EstimatedCount
FROM   pg_class
WHERE  oid = 'public.TableName'::regclass;</pre> </p>
<pre class="crayon-plain-tag">SELECT 
	schemaname
	,relname
	,n_live_tup AS EstimatedCount 
FROM pg_stat_user_tables 
ORDER BY n_live_tup DESC;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2015/10/postgresql-fast-way-to-find-the-row-count-of-a-table/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
	</channel>
</rss>
