Skip to content

Instantly share code, notes, and snippets.

View hooopo's full-sized avatar
🍏
I may be slow to respond.

Hooopo hooopo

🍏
I may be slow to respond.
View GitHub Profile
\copy (SELECT row_number() over() AS "序号", n.nspname AS "系统名", c.relname AS "英文表名", d.description AS "字段中文名", a.attname AS "字段英文名", format_type(a.atttypid, a.atttypmod) AS "数据类型", a.attnotnull as "是否为空", coalesce(i.indisprimary, false) AS "是否主键", def.adsrc AS "默认值" FROM pg_attribute a LEFT JOIN pg_class c ON a.attrelid = c.oid LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace LEFT JOIN pg_description d ON (c.oid = d.objoid AND a.attnum = d.objsubid) LEFT JOIN pg_index i ON (c.oid = i.indrelid AND i.indkey[0] = a.attnum) LEFT JOIN pg_attrdef def ON (a.attrelid = def.adrelid AND a.attnum = def.adnum) WHERE c.relkind in ('r', '') AND pg_catalog.pg_table_is_visible(c.oid) AND n.nspname != 'pg_catalog' AND n.nspname != 'information_schema' AND n.nspname != 'public' AND n.nspname !~ '^pg_toast' AND a.attname NOT IN ('cid', 'xid', 'xmin', 'xmax', 'cmin', 'cmax', 'ctid', 'tableoid') ORDER BY 2,3) to '~/columns.csv' with csv head
\copy (SELECT row_number() over() AS "序号", n.nspname AS "系统名", c.relname AS "英文表名", d.description AS "字段中文名", a.attname AS "字段英文名", format_type(a.atttypid, a.atttypmod) AS "数据类型", a.attnotnull as "是否为空", coalesce(i.indisprimary, false) AS "是否主键", def.adsrc AS "默认值" FROM pg_attribute a LEFT JOIN pg_class c ON a.attrelid = c.oid LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace LEFT JOIN pg_description d ON (c.oid = d.objoid AND a.attnum = d.objsubid) LEFT JOIN pg_index i ON (c.oid = i.indrelid AND i.indkey[0] = a.attnum) LEFT JOIN pg_attrdef def ON (a.attrelid = def.adrelid AND a.attnum = def.adnum) WHERE c.relkind in ('r', '') AND pg_catalog.pg_table_is_visible(c.oid) AND n.nspname != 'pg_catalog' AND n.nspname != 'information_schema' AND n.nspname != 'public' AND n.nspname !~ '^pg_toast' AND a.attname NOT IN ('cid', 'xid', 'xmin', 'xmax', 'cmin', 'cmax', 'ctid', 'tableoid') ORDER BY 2,3) to '~/columns.csv' with csv head
\copy (SELECT n.nspname as "系统名", c.relname as "表名",pg_catalog.pg_size_pretty(pg_catalog.pg_table_size(c.oid)) as "大小",pg_catalog.obj_description(c.oid, 'pg_class') as "中文名",c.reltuples AS "条数" FROM pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r','') AND n.nspname <> 'pg_catalog' AND n.nspname <> 'information_schema' AND n.nspname !~ '^pg_toast' AND pg_catalog.pg_table_is_visible(c.oid) ORDER BY 1 DESC,2) to '/tmp/tables.csv' WITH CSV HEADER;
@hooopo
hooopo / postgres_queries_and_commands.sql
Created January 26, 2018 05:51 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(query_start, clock_timestamp()), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(query_start, clock_timestamp()), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
[["天津"],
["北京"],
["上海"],
["重庆"],
["香港"],
["澳门"],
["河北", "保定"],
["河北", "张家口"],
["河北", "承德"],
["河北", "沧州"],
[["上海"],
["北京"],
["天津"],
["重庆"],
["台湾"],
["香港"],
["澳门"],
["河北", "张家口"],
["河北", "承德"],
["河北", "沧州"],
# speed up pluck
class ActiveRecord::Relation
class RailsDateTimeDecoder < PG::SimpleDecoder
def decode(string, tuple=nil, field=nil)
if Rails.version >= "4.2.0"
@caster ||= ActiveRecord::Type::DateTime.new
@caster.type_cast_from_database(string)
else
#!/usr/bin/env ruby
require 'open-uri'
patchset = ARGV[0]
version = ARGV[1]
raise 'Please specify a patchset' if patchset.nil?
raise 'Please specify a ruby version' if version.nil?
@hooopo
hooopo / gist:10fef815d816faeef03310724ce29cbc
Created July 14, 2016 03:44 — forked from lukas-vlcek/gist:6039115
Torturing Word Delimiter TokenFilter in ElasticSearch
#!/bin/sh
curl -X DELETE 'localhost:9200/i/'
curl -X POST 'localhost:9200/i/' -d '{
"settings" : {
"analysis" : {
"analyzer" : {
"crazy" : {
"type" : "custom",

My largest Sidekiq application had a memory leak and I was able to find and fix it in just few hours spent on analyzing Ruby's heap. In this post I'll show my profiling setup.

As you might know Ruby 2.1 introduced a few great changes to ObjectSpace, so now it's much easier to find a line of code that is allocating too many objects. Here is great post explaining how it's working.

I was too lazy to set up some seeding and run it locally, so I checked that test suite passes when profiling is enabled and pushed debugging to production. Production environment also suited me better since my jobs data can't be fully random generated.

So, in order to profile your worker, add this to your Sidekiq configuration:

if ENV["PROFILE"]