Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hidden Features of PostgreSQL [closed]

I'm surprised this hasn't been posted yet. Any interesting tricks that you know about in Postgres? Obscure config options and scaling/perf tricks are particularly welcome.

I'm sure we can beat the 9 comments on the corresponding MySQL thread :)

like image 211
ramanujan Avatar asked Apr 17 '09 17:04

ramanujan


People also ask

What is excluded in PSQL?

PostgreSQL excludes statements in PostgreSQL is used to compare any two rows from the specified column or expression by using the operator specified in PostgreSQL. At the time of excluding the column, the comparison operator will return the null or false value as output.

What is dirty pages in PostgreSQL?

The shared buffers are accessed by all the background server and user processes connecting to the database. The data that is written or modified in this location is called "dirty data" and the unit of operation being database blocks (or pages), the modified blocks are also called "dirty blocks" or "dirty pages".


1 Answers

Since postgres is a lot more sane than MySQL, there are not that many "tricks" to report on ;-)

The manual has some nice performance tips.

A few other performance related things to keep in mind:

  • Make sure autovacuum is turned on
  • Make sure you've gone through your postgres.conf (effective cache size, shared buffers, work mem ... lots of options there to tune).
  • Use pgpool or pgbouncer to keep your "real" database connections to a minimum
  • Learn how EXPLAIN and EXPLAIN ANALYZE works. Learn to read the output.
  • CLUSTER sorts data on disk according to an index. Can dramatically improve performance of large (mostly) read-only tables. Clustering is a one-time operation: when the table is subsequently updated, the changes are not clustered.

Here's a few things I've found useful that aren't config or performance related per se.

To see what's currently happening:

select * from pg_stat_activity; 

Search misc functions:

select * from pg_proc WHERE proname ~* '^pg_.*' 

Find size of database:

select pg_database_size('postgres'); select pg_size_pretty(pg_database_size('postgres')); 

Find size of all databases:

select datname, pg_size_pretty(pg_database_size(datname)) as size   from pg_database; 

Find size of tables and indexes:

select pg_size_pretty(pg_relation_size('public.customer')); 

Or, to list all tables and indexes (probably easier to make a view of this):

select schemaname, relname,     pg_size_pretty(pg_relation_size(schemaname || '.' || relname)) as size   from (select schemaname, relname, 'table' as type           from pg_stat_user_tables         union all         select schemaname, relname, 'index' as type           from pg_stat_user_indexes) x; 

Oh, and you can nest transactions, rollback partial transactions++

test=# begin; BEGIN test=# select count(*) from customer where name='test';  count  -------      0 (1 row) test=# insert into customer (name) values ('test'); INSERT 0 1 test=# savepoint foo; SAVEPOINT test=# update customer set name='john'; UPDATE 3 test=# rollback to savepoint foo; ROLLBACK test=# commit; COMMIT test=# select count(*) from customer where name='test';  count  -------      1 (1 row) 
like image 165
tommym Avatar answered Sep 19 '22 08:09

tommym