Search This Blog

Friday, July 21, 2017

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


Thursday, June 4, 2015

RMAN: Restore missing archivelogs


SQL> select sequence#, applied, to_char(first_time,’dd-mm-yyyy hh24:mi:ss’) first_time
from v$archived_log
order by sequence#;


SQL> select process,sequence#,status from v$managed_standby;
PROCESS STATUS SEQUENCE# FIRST_TIME
------------ ---------- -------------------
ARCH CONNECTED 0
ARCH CONNECTED 0
MRP0 WAIT_FOR_GAP 7279
RFS WRITING 7299


WAIT_FOR_GAP means thare are missing archivelogs. You need to recover them.


We first identify whose are missing:

[oracle@ora-test ~]$ sqlplus / as sysdba
SQL> select * from v$archive_gap;


   THREAD# LOW_SEQUENCE# HIGH_SEQUENCE#

---------- ------------- --------------

         1          7279           7286




We then use RMAN to recover them:

[oracle@ora-stb ~]$ rman target / catalog rman/password@rman_repo
RMAN> restore archivelog sequence between 7279 and 7286;



You can now check
[oracle@ora-stb ~]$ ls –l /archivelog_folder/arch_72[7-8]*

Tuesday, May 26, 2015

Postgres SQL: Day to day tips

Dates

transform an int as readable timestamp:

select to_timestamp(FIELD_TO_CONVERT) from table;

Thursday, April 9, 2015

Postgres SQL: Install and first launch

Create and setup the postgres user:
[root@localhost ~]# useradd postgres -m /home/postgres

If you don't have the home dir yet, you can add it at a later stage. You just need to stop all processes used by it first.
[root@localhost ~]# mkdir -p /home/postgres
[root@localhost ~]# chown postgres:postgres /home/postgres
[root@localhost ~]# usermod -d /home/postgres postgres

Then, connect as postgres and start the server:
-bash-4.3$ initdb -D /var/lib/pgsql/data/
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /var/lib/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
creating configuration files ... ok
creating template1 database in /var/lib/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    postgres -D /var/lib/pgsql/data/
or
    pg_ctl -D /var/lib/pgsql/data/ -l logfile start

Start the server:
-bash-4.3$ pg_ctl -D /var/lib/pgsql/data/ -l logfile start

You can now create a database and connect to it:
-bash-4.3$ createdb test1
-bash-4.3$ psql test1
psql (9.3.6)
Type "help" for help.

test1=#


To stop the server:
$ pg_ctl stop