Skip to content

Instantly share code, notes, and snippets.

View sahglie's full-sized avatar

Steven Hansen sahglie

View GitHub Profile
Setting Up A Localhost Self-signed Certificate For Local Https Development
==========================================================================
- GENERATE THE CERTIFICATE
# 1. generate key
- create an 'ssl' folder somewhere
- cd to it
_.extend Backbone.Validation.callbacks,
valid: (view, attr, selector) ->
control = view.$('[' + selector + '=' + attr + ']')
group = control.parents(".control-group")
group.removeClass("error")
if control.data("error-style") == "tooltip"
# CAUTION: calling tooltip("hide") on an uninitialized tooltip
# causes bootstraps tooltips to crash somehow...
control.tooltip "hide" if control.data("tooltip")
SELECT schemaname, relname,
seq_scan as table_scans,
idx_scan as index_scans,
pg_size_pretty(pg_relation_size(relid)) as table_size,
n_tup_ins + n_tup_del + n_tup_upd + n_tup_hot_upd as write_activty
FROM pg_stat_user_tables
WHERE seq_scan > 1000
AND seq_scan > ( idx_scan / 10 )
AND pg_relation_size(relid) > ( 16000000 )
ORDER BY pg_relation_size(relid) desc;
WITH table_scans as (
SELECT relid,
tables.idx_scan + tables.seq_scan as all_scans,
( tables.n_tup_ins + tables.n_tup_upd + tables.n_tup_del ) as writes,
pg_relation_size(relid) as table_size
FROM pg_stat_user_tables as tables
),
all_writes as (
SELECT sum(writes) as total_writes
FROM table_scans
WITH btree_index_atts AS (
SELECT nspname, relname, reltuples, relpages, indrelid, relam,
regexp_split_to_table(indkey::text, ' ')::smallint AS attnum,
indexrelid as index_oid
FROM pg_index
JOIN pg_class ON pg_class.oid=pg_index.indexrelid
JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
JOIN pg_am ON pg_class.relam = pg_am.oid
WHERE pg_am.amname = 'btree'
),
SELECT * FROM (
SELECT
schemaname, tablename,
ROUND(CASE WHEN otta=0 THEN 0.0 ELSE ((sml.relpages/otta::numeric) * 100 - 100) END) AS tbloat,
CASE WHEN relpages < otta THEN 0 ELSE round(bs*(sml.relpages-otta)::numeric / (1024^2)::numeric , 2 ) END AS wastedmb,
ROUND(AVG(CASE WHEN iotta >= ipages THEN 0
WHEN iotta = 0 THEN 0
ELSE (ipages/iotta::numeric) * 100 - 100 END), -1) AS idxbloat,
SUM(CASE WHEN ipages < iotta THEN 0 ELSE round( bs*(ipages-iotta)::numeric / (1024^2)::numeric, 2 ) END) AS wastedidxmb
FROM (
import File.expand_path(File.dirname(__FILE__) + '/schema_format.rb')
namespace :db do
# Define PG environment utility methods
task :pg_env => :environment do
def pg_get_config
ActiveRecord::Base.configurations.fetch(Rails.env).tap do |config|
ENV['PGHOST'] = config['host'].to_s if config.key?('host')
ENV['PGPORT'] = config['port'].to_s if config.key?('port')
ENV['PGPASSWORD'] = config['password'].to_s if config.key?('password')