pgstattuple.sgml 3.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

<sect1 id="pgstattuple">
 <title>pgstattuple</title>
 
 <indexterm zone="pgstattuple">
  <primary>pgstattuple</primary>
 </indexterm>

 <para>
  <literal>pgstattuple</literal> modules provides various functions to obtain 
  tuple statistics.
 </para>

 <sect2>
  <title>Functions</title>

  <itemizedlist>
   <listitem>
    <para>
     <literal>pgstattuple()</literal> returns the relation length, percentage 
     of the "dead" tuples of a relation and other info. This may help users to 
     determine whether vacuum is necessary or not. Here is an example session:
    </para>
    <programlisting>
test=> \x
Expanded display is on.
test=> SELECT * FROM pgstattuple('pg_catalog.pg_proc');
-[ RECORD 1 ]------+-------
table_len          | 458752
tuple_count        | 1470
tuple_len          | 438896
tuple_percent      | 95.67
dead_tuple_count   | 11
dead_tuple_len     | 3157
dead_tuple_percent | 0.69
free_space         | 8932
free_percent       | 1.95
    </programlisting>
    <para>
     Here are explanations for each column:
    </para>
    
    <table>
     <title><literal>pgstattuple()</literal> column descriptions</title>
     <tgroup cols="2">
      <thead>
       <row>
        <entry>Column</entry>
	<entry>Description</entry>
       </row>
      </thead>
      <tbody>
       <row>
        <entry>table_len</entry>
	<entry>physical relation length in bytes</entry>
       </row>
       <row>
        <entry>tuple_count</entry>
	<entry>number of live tuples</entry>
       </row>
       <row>
        <entry>tuple_len</entry>
	<entry>total tuples length in bytes</entry>
       </row>
       <row>
        <entry>tuple_percent</entry>
	<entry>live tuples in %</entry>
       </row>
       <row>
        <entry>dead_tuple_len</entry>
	<entry>total dead tuples length in bytes</entry>
       </row>
       <row>
        <entry>dead_tuple_percent</entry>
	<entry>dead tuples in %</entry>
       </row>
       <row>
        <entry>free_space</entry>
	<entry>free space in bytes</entry>
       </row>
       <row>
        <entry>free_percent</entry>
	<entry>free space in %</entry>
       </row>
      </tbody>
     </tgroup>
    </table>
    <para>
     <note>
      <para>
       <literal>pgstattuple</literal> acquires only a read lock on the relation. So 
       concurrent update may affect the result.
      </para>
     </note>
     <note>
      <para>
       <literal>pgstattuple</literal> judges a tuple is "dead" if HeapTupleSatisfiesNow()
       returns false.
      </para>
     </note>
    </para>
   </listitem>


   <listitem>
    <para>
     <literal>pg_relpages()</literal> returns the number of pages in the relation.
    </para>
   </listitem>

   <listitem>
    <para>
     <literal>pgstatindex()</literal> returns an array showing the information about an index:
    </para>
    <programlisting>
test=> \x
Expanded display is on.
test=> SELECT * FROM pgstatindex('pg_cast_oid_index');
-[ RECORD 1 ]------+------
version            | 2
tree_level         | 0
index_size         | 8192
root_block_no      | 1
internal_pages     | 0
leaf_pages         | 1
empty_pages        | 0
deleted_pages      | 0
avg_leaf_density   | 50.27
leaf_fragmentation | 0
    </programlisting>
   </listitem>
  </itemizedlist>
 </sect2>

 <sect2>
  <title>Usage</title>
  <para>
   <literal>pgstattuple</literal> may be called as a relation function and is
   defined as follows:
  </para>
  <programlisting>
    CREATE OR REPLACE FUNCTION pgstattuple(text) RETURNS pgstattuple_type
     AS 'MODULE_PATHNAME', 'pgstattuple'
     LANGUAGE C STRICT;

    CREATE OR REPLACE FUNCTION pgstattuple(oid) RETURNS pgstattuple_type
     AS 'MODULE_PATHNAME', 'pgstattuplebyid'
     LANGUAGE C STRICT;
  </programlisting>
  <para>
   The argument is the relation name (optionally it may be qualified)
   or the OID of the relation.  Note that pgstattuple only returns
   one row.
  </para>
 </sect2>

</sect1>