<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:default="http://www.w3.org/1999/xhtml">
  <title xmlns="http://www.w3.org/2005/Atom">Dobrica Pavlinušić's random unstructured stuff: PostgreSQL</title>
  <id xmlns="http://www.w3.org/2005/Atom">https://saturn.ffzg.hr/rot13/</id>
  <link xmlns="http://www.w3.org/2005/Atom" type="text/html" rel="alternate" href="https://saturn.ffzg.hr/rot13/index.cgi?postgresql"/>
  <link xmlns="http://www.w3.org/2005/Atom" type="application/atom+xml" rel="self" href="https://saturn.ffzg.hr%2C%20saturn.ffzg.hr:443/feed/workspace/rot13?page=postgresql;type=Atom"/>
  <updated xmlns="http://www.w3.org/2005/Atom">2011-11-27T21:03:07Z</updated>
  <entry xmlns="http://www.w3.org/2005/Atom" xmlns:default="http://www.w3.org/1999/xhtml">
    <title xmlns="http://www.w3.org/2005/Atom">PostgreSQL</title>
    <link xmlns="http://www.w3.org/2005/Atom" type="text/html" rel="alternate" href="https://saturn.ffzg.hr/rot13/index.cgi?postgresql"/>
    <content xmlns="http://www.w3.org/2005/Atom" xmlns:default="http://www.w3.org/1999/xhtml" type="xhtml">
      <div xmlns="http://www.w3.org/1999/xhtml"><div>Creator: Dobrica Pavlinušić</div><hr/><div>Tags: howto, PostgreSQL</div><hr/><div class="wiki">
<p>
SQL snippets for PostgreSQL</p>
<div class="nlw_phrase"><table class="wafl_container">
  <tr>
    <td>
      <div class="wafl_box">
        
          <div class="wafl_titlebox">
            <div class="wafl_title">
              
                Contents: [Dobrica Pavlinušić's random unstructured stuff]
              
            </div>
          </div>
          <div class="wafl_items">
            
              <div class="wiki">
<ul>
<li><span class="nlw_phrase"><a title="section link" href="https://saturn.ffzg.hr/rot13/index.cgi?dobrica_pavlinu%C5%A1i%C4%87_s_random_unstructured_stuff#size_of_transaction_log">Dobrica Pavlinušić's random unstructured stuff (Size of transaction log)</a><!-- wiki: {link: [Dobrica Pavlinušić's random unstructured stuff] Size of transaction log} --></span></li>
<li><span class="nlw_phrase"><a title="section link" href="https://saturn.ffzg.hr/rot13/index.cgi?dobrica_pavlinu%C5%A1i%C4%87_s_random_unstructured_stuff#size_of_tables_in_current_database">Dobrica Pavlinušić's random unstructured stuff (Size of tables in current database)</a><!-- wiki: {link: [Dobrica Pavlinušić's random unstructured stuff] Size of tables in current database} --></span></li>
<li><span class="nlw_phrase"><a title="section link" href="https://saturn.ffzg.hr/rot13/index.cgi?dobrica_pavlinu%C5%A1i%C4%87_s_random_unstructured_stuff#number_of_rows_in_all_tables">Dobrica Pavlinušić's random unstructured stuff (Number of rows in all tables)</a><!-- wiki: {link: [Dobrica Pavlinušić's random unstructured stuff] Number of rows in all tables} --></span></li>
<li><span class="nlw_phrase"><a title="section link" href="https://saturn.ffzg.hr/rot13/index.cgi?dobrica_pavlinu%C5%A1i%C4%87_s_random_unstructured_stuff#script_to_display_database_size">Dobrica Pavlinušić's random unstructured stuff (Script to display database size)</a><!-- wiki: {link: [Dobrica Pavlinušić's random unstructured stuff] Script to display database size} --></span></li>

<ul>
<li><span class="nlw_phrase"><a title="section link" href="https://saturn.ffzg.hr/rot13/index.cgi?dobrica_pavlinu%C5%A1i%C4%87_s_random_unstructured_stuff#all_databases">Dobrica Pavlinušić's random unstructured stuff (all databases)</a><!-- wiki: {link: [Dobrica Pavlinušić's random unstructured stuff] all databases} --></span></li>
<li><span class="nlw_phrase"><a title="section link" href="https://saturn.ffzg.hr/rot13/index.cgi?dobrica_pavlinu%C5%A1i%C4%87_s_random_unstructured_stuff#all_databases_grouped_by_user">Dobrica Pavlinušić's random unstructured stuff (all databases, grouped by user)</a><!-- wiki: {link: [Dobrica Pavlinušić's random unstructured stuff] all databases, grouped by user} --></span></li>
</ul>
<li><span class="nlw_phrase"><a title="section link" href="https://saturn.ffzg.hr/rot13/index.cgi?dobrica_pavlinu%C5%A1i%C4%87_s_random_unstructured_stuff#active_locks">Dobrica Pavlinušić's random unstructured stuff (Active locks)</a><!-- wiki: {link: [Dobrica Pavlinušić's random unstructured stuff] Active locks} --></span></li>
<li><span class="nlw_phrase"><a title="section link" href="https://saturn.ffzg.hr/rot13/index.cgi?dobrica_pavlinu%C5%A1i%C4%87_s_random_unstructured_stuff#graphics">Dobrica Pavlinušić's random unstructured stuff (Graphics)</a><!-- wiki: {link: [Dobrica Pavlinušić's random unstructured stuff] Graphics} --></span></li>
<li><span class="nlw_phrase"><a title="section link" href="https://saturn.ffzg.hr/rot13/index.cgi?dobrica_pavlinu%C5%A1i%C4%87_s_random_unstructured_stuff#current_query_activity">Dobrica Pavlinušić's random unstructured stuff (Current query activity)</a><!-- wiki: {link: [Dobrica Pavlinušić's random unstructured stuff] Current query activity} --></span></li>
<li><span class="nlw_phrase"><a title="section link" href="https://saturn.ffzg.hr/rot13/index.cgi?dobrica_pavlinu%C5%A1i%C4%87_s_random_unstructured_stuff#quickly_convert_database_table_to_sqlite">Dobrica Pavlinušić's random unstructured stuff (Quickly convert database table to SQLite)</a><!-- wiki: {link: [Dobrica Pavlinušić's random unstructured stuff] Quickly convert database table to SQLite} --></span></li>
</ul>
</div>

            
            
          </div>
        
      </div>
    </td>
  </tr>
</table>
<!-- wiki: {toc: }
--></div><br/><br/><h1 id="size_of_transaction_log">Size of transaction log</h1>
<pre>
select sum((pg_stat_file('pg_xlog/' || file)).size)
from pg_ls_dir('pg_xlog') as file
where file ~ '^[0-9A-F]';
</pre>
<br/><h1 id="size_of_tables_in_current_database">Size of tables in current database</h1>
<pre>
select relname,pg_relation_size(oid) as size
from pg_class
where relkind = 'r' and relname not like 'pg_%' order by size desc;
</pre>
<br/><h1 id="number_of_rows_in_all_tables">Number of rows in all tables</h1>
<p>
<a target="_blank" title="(external link)" href="http://stackoverflow.com/questions/2596670/how-do-you-find-the-row-count-for-all-your-tables-in-postgres">http://stackoverflow.com/questions/2596670/how-do-you-find-the-row-count-for-all-your-tables-in-postgres</a></p>
<pre>
SELECT 
  nspname AS schemaname,relname,reltuples::int
FROM pg_class C
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
WHERE 
  nspname NOT IN ('pg_catalog', 'information_schema') AND
  relkind='r' 
ORDER BY reltuples DESC;
</pre>
<br/><h1 id="script_to_display_database_size">Script to display database size</h1>
<p>
Script: <span class="nlw_phrase"><a href="https://saturn.ffzg.hr/rot13/index.cgi/pg_size?action=attachments_download;page_name=postgresql;id=20071005123933-0-17773">pg_size</a><!-- wiki: {file: pg_size} --></span></p>
<h2 id="all_databases">all databases</h2>
<pre>
$ pg_size
</pre>
<br/><h2 id="all_databases_grouped_by_user">all databases, grouped by user</h2>
<pre>
$ pg_size -u
</pre>
<br/><h1 id="active_locks">Active locks</h1>
<pre>
select * from pg_locks where pid not in (select procpid from pg_stat_activity);
</pre>
<br/><h1 id="graphics">Graphics</h1>
<p>
Taken from <a target="_blank" title="(external link)" href="http://lca2007.linux.org.au/att_data/Miniconfs(2f)PostgreSQL/attachments/advanced.pdf">http://lca2007.linux.org.au/att_data/Miniconfs(2f)PostgreSQL/attachments/advanced.pdf</a></p>
<pre>
# CREATE AGGREGATE array_accum (anyelement)
(
    sfunc = array_append,
    stype = anyarray,
    initcond = '{}'
);

# select d, count(*),
       array_to_string(array_accum('+'::text),'') as graph
       from hits group by 1 order by 1 asc;
     d       | count | graph
------------+-------+----------
 2007-01-13 |      8 | ++++++++
 2007-01-14 |      7 | +++++++
 2007-01-15 |      4 | ++++
(3 rows)
</pre>
<br/><h1 id="current_query_activity">Current query activity</h1>
<pre>
select datid,datname,procpid,current_query,query_start from pg_stat_activity where current_query not like '&lt;IDLE&gt;' ;
</pre>
<br/><p>
To actually <strong>see</strong> queries you also have to edit <tt>postgresql.conf</tt> to include:</p>
<pre>
stats_command_string = yes
</pre>
<br/><h1 id="quickly_convert_database_table_to_sqlite">Quickly convert database table to SQLite</h1>
<pre>
pg_dump --inserts --no-owner --no-privileges --table hr_edu_orgs a3c | grep -v ^SET | sqlite3 var/a3c.sqlite
</pre>
</div>
<hr/><div>Attachments: pg_size</div></div>
    </content>
    <id xmlns="http://www.w3.org/2005/Atom">https://saturn.ffzg.hr/rot13/index.cgi?postgresql</id>
    <author xmlns="http://www.w3.org/2005/Atom">
      <name xmlns="http://www.w3.org/2005/Atom">Dobrica Pavlinušić</name>
    </author>
    <updated xmlns="http://www.w3.org/2005/Atom">2011-11-27T21:03:07Z</updated>
    <category xmlns="http://www.w3.org/2005/Atom" term="howto" label="howto"/>
    <category xmlns="http://www.w3.org/2005/Atom" term="PostgreSQL" label="PostgreSQL"/>
  </entry>
</feed>
