-
Archives
- March 2019
- March 2018
- June 2017
- May 2017
- November 2016
- September 2016
- July 2016
- June 2016
- May 2016
- April 2016
- March 2016
- February 2016
- January 2016
- December 2015
- November 2015
- October 2015
- September 2015
- August 2015
- July 2015
- June 2015
- April 2015
- December 2014
- October 2014
- September 2014
- May 2014
- April 2014
- March 2014
- January 2014
- November 2013
- October 2013
- October 2012
- September 2012
- August 2012
- July 2012
- June 2012
- March 2012
- February 2012
- January 2012
- December 2011
- November 2011
- October 2011
- September 2011
- August 2011
-
Meta
Category Archives: Database
Show postgres lock
https://wiki.postgresql.org/wiki/Lock_Monitoring SELECT blocked_locks.pid AS blocked_pid, blocked_activity.usename AS blocked_user, blocking_locks.pid AS blocking_pid, blocking_activity.usename AS blocking_user, blocked_activity.query AS blocked_statement, blocking_activity.query AS current_statement_in_blocking_process FROM pg_catalog.pg_locks blocked_locks JOIN pg_catalog.pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid JOIN pg_catalog.pg_locks blocking_locks ON blocking_locks.locktype = blocked_locks.locktype AND blocking_locks.DATABASE IS … Continue reading
Posted in Database, PostgreSQL
Leave a comment
Check all table size in Postgres
The “tables ” is in information_schema The “pg_class” is in System Catalogs 1234567SELECT ist.table_name, reltuples AS "entries", pg_size_pretty(relpages::BIGINT*8*1024) AS SIZE FROM information_schema.tables ist INNER JOIN pg_class c ON (ist.table_name = c.relname) WHERE ist.table_catalog=’ip2_sync_master’ AND ist.table_schema=’public’ ORDER BY relpages DESC, ist.table_name … Continue reading
Posted in Database, PostgreSQL
Leave a comment
Synchronize a postgres table through bash and csv
Please note that the csv export process does not escape commas. 1234567891011121314151617181920212223242526272829303132333435#!/bin/bash DIR=’/root/sql_dump’ mkdir -p "$DIR" cd "$DIR" SIZE=100000 #START=611244350 START=0 END=$((START + SIZE)) STOP=189097000 TABLNAME="schema.tablename" while [[ $START -lt $STOP ]] && [[ $END -le $STOP ]]; do … Continue reading
Posted in Database, PostgreSQL
Leave a comment
Export and Import Postgres query to CSV
# =========================================================== # Export to CSV # =========================================================== # export PGPASSWORD=”YOURPASSWORD” # psql -U YOURUSERNAME -h YOURHOSTNAME -d YOURDBNAME-t -A -F”,” -c “select * from YOURTABLENAME limit 2″ > output.csv # =========================================================== # Import # =========================================================== # export PGPASSWORD=”YOURPASSWORD” # … Continue reading
Posted in Database, PostgreSQL
Leave a comment
Postgres SSD optimization
If you’re running your database on an SSD instead of a spinning disk, you might want to optimize postgres table space cost: 12345678910– Change the tablespace cost ALTER TABLESPACE pg_default SET ( seq_page_cost = 20, random_page_cost = 1 ); — … Continue reading
Posted in Database, PostgreSQL
Leave a comment
Delete Postgres Cache
1234#!/bin/bash sync echo 1 > /proc/sys/vm/drop_caches service postgresql-9.3 restart
Posted in Database, PostgreSQL
Leave a comment
Check which database object depends on (has reference to) your table
12345678910111213141516SELECT R.TABLE_NAME FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE u INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS FK ON U.CONSTRAINT_CATALOG = FK.UNIQUE_CONSTRAINT_CATALOG AND U.CONSTRAINT_SCHEMA = FK.UNIQUE_CONSTRAINT_SCHEMA AND U.CONSTRAINT_NAME = FK.UNIQUE_CONSTRAINT_NAME INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE R ON R.CONSTRAINT_CATALOG = FK.CONSTRAINT_CATALOG AND … Continue reading
Posted in Database, PostgreSQL
Leave a comment
Postgres function to devide-and-conquer (iterate) your big query
Create a postgres function to devide-and-conquer (iterate) your big query 1234567891011121314151617181920212223242526272829303132333435363738394041TRUNCATE TABLE YOUR_NEW_BIG_TABLE; DROP FUNCTION IF EXISTS pg_iterator(); CREATE OR REPLACE FUNCTION pg_iterator() RETURNS void AS $BODY$ DECLARE vOffsetRecord INT; vTotal INT; vLimit … Continue reading
Posted in Database, PostgreSQL
Leave a comment
Add user in mysql
123CREATE USER ‘YOUR_USER_NAME’@’CONNECTING_FROM_WHERE’ IDENTIFIED BY ‘THIS_USER_PASSWORD’; GRANT ALL PRIVILEGES ON *.* TO ‘YOUR_USER_NAME’@’CONNECTING_FROM_WHERE’ WITH GRANT OPTION; FLUSH PRIVILEGES;
Posted in MySQL
Leave a comment
Calling a mysql query from linux or dos command line
There are times when you want to call a mysql command from the bash or dos script then call this script in a scheduler (cron job). The example below will show you how to do so. 1mysql -uYOURUSERNAME -pYOURPASSWORD -DYOURDATABASE … Continue reading
Posted in Database, Linux, MySQL
Leave a comment