Search This Blog

Friday, July 21, 2017

Postgres SQL: pg_dump / pg_restore

Backup a specific table:
pg_dump --table TABLENAME -U postgres dbName -f /home/postgres/TABLENAME.sql


If there is a table with the same name in another schema, use the --schema SCHEMANAME option

Postgres SQL: User management

alter user POSTGRES with password 'newPassword';

Postgres SQL: Database admin

DB size
select pg_size_pretty(pg_database_size('DB1'));

pg_size_pretty
----------------
 48 GB
Non default parameters
select name, setting, boot_val from pg_settings where setting != boot_val;
              name              |            setting             |     boot_val  
--------------------------------+--------------------------------+-------------------
 archive_command                | cp %p /backup/DB1/current/%f |
 archive_mode                   | on                             | off
 archive_timeout                | 600                            | 0
 bgwriter_lru_maxpages          | 200                            | 100
 checkpoint_segments            | 100                            | 3
 client_encoding                | UTF8                           | SQL_ASCII
 deadlock_timeout               | 10000                          | 1000
 default_text_search_config     | pg_catalog.english             | pg_catalog.simple
 effective_cache_size           | 13107200                       | 16384
 lc_collate                     | en_US.UTF-8                    | C
 lc_ctype                       | en_US.UTF-8                    | C
 lc_messages                    | en_US.UTF-8                    |
 lc_monetary                    | en_US.UTF-8                    | C
 lc_numeric                     | en_US.UTF-8                    | C
 lc_time                        | en_US.UTF-8                    | C
 listen_addresses               | *                              | localhost
 log_autovacuum_min_duration    | 0                              | -1
 log_checkpoints                | on                             | off
 log_connections                | on                             | off
 log_directory                  | /podom_02/pg_log               | pg_log
 log_duration                   | on                             | off
 log_hostname                   | on                             | off
 log_line_prefix                | %d %u %r %p %c                 |
 log_lock_waits                 | on                             | off
 log_min_duration_statement     | 1000                           | -1
 log_min_messages               | notice                         | warning
 log_rotation_size              | 0                              | 10240
 log_statement                  | ddl                            | none
 log_statement_stats            | on                             | off
 log_temp_files                 | 0                              | -1
 log_timezone                   | Europe/Paris                   | UNKNOWN
 log_truncate_on_rotation       | on                             | off
 logging_collector              | on                             | off
 maintenance_work_mem           | 1048576                        | 16384
 max_connections                | 200                            | 100
 max_stack_depth                | 2048                           | 100
 random_page_cost               | 1                              | 4
 server_encoding                | UTF8                           | SQL_ASCII
 shared_buffers                 | 1048576                        | 1024
 superuser_reserved_connections | 5                              | 3
 TimeZone                       | Europe/Paris                   | UNKNOWN
 timezone_abbreviations         | Default                        | UNKNOWN
 wal_buffers                    | 2048                           | 8
 work_mem                       | 32768                          | 1024
(44 rows)


This shows all of the server configuration changes made via updates to the postgresql.conf file, from a running server:

SELECT version();
SELECT name, current_setting(name), SOURCE
  FROM pg_settings
  WHERE SOURCE NOT IN ('default', 'override');

Postgres SQL: Functions


You can find basic information regarding a function with the command \df
zabbixdb=> \df function_name                                 List of functions
 Schema |      Name     | Result data type | Argument data types |  Type
--------+------------------------+------------------+-----------------------
 public | function_name | trigger          |                     | trigger


You can see the source code with this query:
testdb=> select prosrc from pg_proc where proname='function_or_procedure_name';<br />

Execute a VOID function:
testdb=> select functionName();

Execute a VOID function with named parameters:
testdb=> select functionName(param1:='valueText', param2:=valueInt);


If you need more information, here is the pg_proc table:
testdb=> \d+ pg_proc
                             Table "pg_catalog.pg_proc"
     Column      |     Type     | Modifiers | Storage  | Stats target | Description
-----------------+--------------+-----------+----------+--------------+-------------
 proname         | name         | not null  | plain    |              |
 pronamespace    | oid          | not null  | plain    |              |
 proowner        | oid          | not null  | plain    |              |
 prolang         | oid          | not null  | plain    |              |
 procost         | real         | not null  | plain    |              |
 prorows         | real         | not null  | plain    |              |
 provariadic     | oid          | not null  | plain    |              |
 protransform    | regproc      | not null  | plain    |              |
 proisagg        | boolean      | not null  | plain    |              |
 proiswindow     | boolean      | not null  | plain    |              |
 prosecdef       | boolean      | not null  | plain    |              |
 proleakproof    | boolean      | not null  | plain    |              |
 proisstrict     | boolean      | not null  | plain    |              |
 proretset       | boolean      | not null  | plain    |              |
 provolatile     | "char"       | not null  | plain    |              |
 pronargs        | smallint     | not null  | plain    |              |
 pronargdefaults | smallint     | not null  | plain    |              |
 prorettype      | oid          | not null  | plain    |              |
 proargtypes     | oidvector    | not null  | plain    |              |
 proallargtypes  | oid[]        |           | extended |              |
 proargmodes     | "char"[]     |           | extended |              |
 proargnames     | text[]       |           | extended |              |
 proargdefaults  | pg_node_tree |           | extended |              |
 prosrc          | text         |           | extended |              |
 probin          | text         |           | extended |              |
 proconfig       | text[]       |           | extended |              |
 proacl          | aclitem[]    |           | extended |              |
Indexes:
    "pg_proc_oid_index" UNIQUE, btree (oid)
    "pg_proc_proname_args_nsp_index" UNIQUE, btree (proname, proargtypes, pronamespace)
Has OIDs: yes

Postgres SQL: Tables

Table size

with index:
select pg_size_pretty(pg_total_relation_size('TABLENAME'));
 pg_size_pretty
----------------
 26 GB

without index:
select pg_size_pretty(pg_relation_size('TABLENAME'));
 pg_size_pretty
----------------
 11 GB

Tables number of lines:

select schemaname, relname, n_live_tup from pg_stat_all_tables where n_live_tup > 0 order by n_live_tup desc;

Postgres SQL: Indexes

invalid indexes:

select count(*) from pg_index where indisvalid = false



number of indexes per tables:

select tablename, count(*) from pg_indexes where schemaname != 'pg_catalog' group by tablename having count(*) > 4 order by 2 desc;
   tablename   | count
---------------+-------
 table1      |    18
 table2         |    18
 table3 |    15
 table4        |    13


Index usage

SELECT
    i.idx_scan,
    i.idx_tup_read,
    i.idx_tup_fetch,
    i.indexrelname AS index,
    it.spcname AS index_tablespace,
    i.relname AS table,
    tt.spcname AS table_tablespace,
    pg_size_pretty(pg_relation_size(i.indexrelname::text)) as index_size
FROM pg_stat_all_indexes i
    INNER JOIN pg_class ic ON (i.indexrelid = ic.oid)
    LEFT OUTER JOIN pg_tablespace it ON (ic.reltablespace = it.oid)
    INNER JOIN pg_class tc ON (i.relid = tc.oid)
    LEFT OUTER JOIN pg_tablespace tt ON (tc.reltablespace = tt.oid)
ORDER BY 1 desc, 2 desc, 3 desc


Index not used

select * from pg_stat_all_indexes
where schemaname <> 'pg_catalog'
and schemaname <> 'pg_toast'
and idx_scan = 0
and idx_tup_read = 0
and idx_tup_fetch = 0
and indexrelid not in (select indexrelid from pg_index where indisunique = true or indisprimary = true
);


Postgres SQL: Sessions


Global overview:

v8.3
SELECT
    sum(CASE WHEN waiting THEN 1 ELSE 0 END) AS waiting,
    sum(CASE WHEN current_query='' THEN 1 ELSE 0 END) AS idle,
    sum(CASE WHEN current_query=' in transaction' THEN 1 ELSE 0 END) AS idletransaction,
    sum(CASE WHEN current_query='' THEN 1 ELSE 0 END) as unknown,
    sum(CASE WHEN NOT waiting AND current_query NOT IN ('', ' in transaction', '') THEN 1 ELSE 0 END) AS active
FROM pg_stat_activity WHERE procpid != pg_backend_pid() and datname = current_database();



v9.3

all sessions (full)

select * from pg_stat_activity

all sessions (lite)

select datname, usename, state, waiting, query from pg_stat_activity;

Sessions state per DB

select datname, state, count(*) from pg_stat_activity group by datname, state order by 1, 2;

nb sessions waiting

select waiting, count(*) from pg_stat_activity group by waiting;

nb sessions waiting per DB

select datname, waiting, count(*) from pg_stat_activity group by datname, waiting order by 1, 2;

querries per DB

select datname, query, count(*) from pg_stat_activity group by datname, query order by 1,3 desc