Thursday, October 12, 2017

PostgreSQL: Display materialized view definition

select definition from pg_matviews where matviewname = 'mv_jersey_tap_data_3months_lag';

PostgreSQL: Find numeric rows w/o digit after decimal point

Source: Fetch records that are non zero after the decimal point in PostgreSQL
SELECT count(*) FROM t WHERE amount != trunc(amount)

Wednesday, October 11, 2017

Delete files older than file

Source: Delete files older than specific file
find __dir__ -type f ! -newer __file__ -delete

Wednesday, August 30, 2017

PostgreSQL: dump and restore one table

pg_dump --host db2.prod.vty.mbqt --port 5432 --username postgres --no-owner --no-password --format custom --encoding UTF8 --verbose --file /tmp/jerome/jerome_test.custom --table public.jerome_test --data-only dwhmbqt
Then
pg_restore --host db2.prod.vty.mbqt --port 5432 --username postgres --no-owner --no-password --dbname dwhmbqt --data-only --verbose /tmp/jerome/jerome_test.custom

PostgreSQL: tables without PK

Source: http://petereisentraut.blogspot.fr/2009/07/how-to-find-all-tables-without-primary.html
-- Tables of db2 without PK
SELECT table_catalog, table_schema, table_name
    FROM information_schema.tables
    WHERE (table_catalog, table_schema, table_name) NOT IN
          (SELECT table_catalog, table_schema, table_name
               FROM information_schema.table_constraints
               WHERE constraint_type = 'PRIMARY KEY')
      AND table_schema NOT IN ('information_schema', 'pg_catalog') and table_schema ~ '_db$' and table_schema not in ('prov_db', 'queue_db', 'ota_db', 'sla_db');

PostgreSQL: find non-empty tables

Source: https://stackoverflow.com/questions/24734489/show-all-not-empty-tables-in-postgres
SELECT n.nspname, c.relname                                                                                                                                                                           
FROM pg_class c
INNER JOIN pg_namespace n ON (n.oid = c.relnamespace)
WHERE c.reltuples != 0 AND c.relkind = 'r' and nspname ~ '_db$'

Thursday, June 8, 2017

Internet lost while VPN Cisco and wired to wifi

The situation is the following:
  • Wired to LAN on Windows 7 
  • VPN Cisco AnyConnect connected 
  • Internet OK

then

Change LAN connection from wired to WIFI => Internet lost unless disconnecting VPN

Solution 1: 
Reboot : )

Solution 2: 

  • Disconnect Cisco VPN 
  • Windows Device Manager | Network adapters 
  • Right click on wireless adapter
  • Select Disable

Then

  • Right click on wireless adapter
  • Select Enable
  • Reconnect Cisco VPN

Wednesday, May 24, 2017

PostgreSQL: dump and restore some schemas

Dump
pg_dump --host localhost --port 5432 --username "postgres"  --clean --no-owner --no-password  --format custom --encoding UTF8 --verbose --file "/tmp/dwhmbqt_2017-05-24.dump.custom" --schema resource_db --schema crm_db --schema iot_db --schema offer_db --schema shared_db dwhmbqt
Restore
pg_restore -U postgres -d dwhmbqt /tmp/dwhmbqt_2017-05-24.dump.custom

Wednesday, May 10, 2017

PostgreSQL: copy in and out

Source:
Import from local CSV to remote DB:
psql -Upostgres -h my_host -d my_db -c "\copy public.nwkmonitor from '/tmp/in.csv' with delimiter as ',' null as '';"
Export from remote DB to local CSV:
psql -Upostgres -h my_host -d my_db -c "\copy public.nwkmonitor2 to '/tmp/out.csv' csv delimiter as ',' header;"

Tuesday, May 9, 2017

PostgreSQL: lateral join tutorial

Source: Postgres LATERAL JOIN

or better, from Erwin Brandstetter:
Source: Split function-returned record into multiple columns

originally correlated to:
Source: error : subquery must return only one column

PostgreSQL: immutable vs stable

Reminder:

  • STABLE : If you call it with the same arguments more than once during the execution of a single query it will return the same value
  • IMMUTABLE : It will always return the same value for the same arguments, regardless of database contents or the passage of time

Saturday, May 6, 2017

PostgreSQL: move from child table to parent table

Source: Postgres table inheritance: move from parent to child and vice versa
with deleted as (delete from  returning *) insert into  select * from deleted

Wednesday, April 26, 2017

PostgreSQL: create read only user

Source: How to Create a Read-Only Database User in Postgres for Analytics and Reporting
createuser -U postgres -D -R -S aws_user
alter user aws_user with encrypted password 'MY PASSWORD';
grant connect on database dwhmbqt to aws_user;
do
$$
declare
 _query text;
 _schema text;
begin
 for _schema in select nspname from pg_namespace where nspname ~ '_db$' loop
   _query := format('grant usage on schema %I to aws_user', _schema);
   raise notice '%', _query;
    execute _query;
 end loop;
end;
$$
do
$$
declare
 _query text;
 _schema text;
begin
 for _schema in select nspname from pg_namespace where nspname ~ '_db$' loop
  _query := format('grant select on all tables in schema %I to aws_user', _schema);
  raise notice '%', _query;
   execute _query;
 end loop;
end;
$$
Then in pg_hba.conf:
host    dwhmbqt         aws_user           10.192.0.0/16   md5

PostgreSQL: out of shared memory

Sometimes, some transactions are big enough to break the limit of max_locks_per_transaction, and this will cause "out of shared memory" errors.

Identify processes with their number of locks:
select distinct pid, count(*) over (partition by pid) as count from pg_locks
Then identify the query:
select * from pg_stat_activity where pid = 119254

Monday, April 24, 2017

Prevent VPN from changing DNS on Windows 7

Source: Prevent VPN from changing DNS on Windows 7 / 8 developer preview
After Cisco AnyConnect VPN connection:
ping 8.8.8.8 OK
ping google.com KO
netsh interface ipv4 show dnsservers
netsh interface ipv4 add dnsservers name="Local Area Connection 3" address=192.168.1.1 index=1 validate=no