-
Archives
- May 2026
- April 2026
- November 2025
- September 2023
- 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
MongoDB Notes
If you’re storing binary files inside MongoDB, the convention is called GridFS. It splits each logical file into two collections: a metadata document and a sequence of binary chunks. This post is a cheat sheet for inspecting and tweaking those … Continue reading
Posted in Database
Leave a comment
Show postgres lock
You’re staring at a query that won’t finish, or a deploy that hangs on a migration, or a UI request that just sits there. Postgres is almost certainly waiting on a lock that another transaction holds. The classic query for … Continue reading
Posted in Database, PostgreSQL
Leave a comment
Check all table size in Postgres
You want a quick “which tables are eating my disk?” overview — usually before a vacuum, an archive purge, or a capacity-planning conversation. Here’s a one-shot query that joins information_schema.tables against pg_class to show every public table with its estimated … 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
Assorted postgres queries
Get table sizes: SELECT relname AS objectname, relkind AS objecttype, reltuples AS “#entries”, pg_size_pretty(relpages::bigint*8*1024) AS size FROM pg_class WHERE relpages >= 8 ORDER BY relpages DESC;
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