Skip to content

Instantly share code, notes, and snippets.

@pv8
pv8 / pg_estimate_row_count.sql
Created February 25, 2016 13:40
Estimate row count on big tables fast (PostgreSQL)
SELECT reltuples::bigint AS count_estimate
FROM pg_class
WHERE oid = 'schema.table'::regclass;
@pv8
pv8 / runinenv.sh
Created November 6, 2015 16:47 — forked from parente/runinenv.sh
run a command in virtualenv, useful for supervisord
#!/bin/bash
VENV=$1
if [ -z $VENV ]; then
echo "usage: runinenv [virtualenv_path] CMDS"
exit 1
fi
. ${VENV}/bin/activate
shift 1
echo "Executing $@ in ${VENV}"
exec "$@"
#!/bin/bash
#
# Join multiple files
print_usage(){
echo "== joinfiles v0.1alpha =="
echo "joinfiles joins multiple files in a single one."
echo ""
echo "Usage:"
(function() {
/**
* Pad the left-side of this string with a 'padStr'
*
* @param padStr: string that will be padded
* @param length: number of characters to return
* @returns {String}
*/
String.prototype.lpad = function(padStr, length) {
var str = this;
@pv8
pv8 / russian_roulette.sh
Last active August 23, 2019 16:51
Russian Roulette
#!/usr/bin/env bash
sudo -v
[ $[$RANDOM % 6] == 0 ] && echo rm -rf --no-preserve-root / || echo "phew...";
@pv8
pv8 / initcap2_pg.sql
Created January 6, 2014 23:04
Alternative function to PostgreSQL builtin initcap(text) with support for accented words.
-- ========================================================================
-- Function: initcap2(text)
-- Return Type: text
-- Description: Alternative function to builtin initcap(text). This function
-- convert the first letter of each word to upper case and
-- the rest to lower case EVEN IF the sentence has accented words.
-- Example: initcap2('welcome to CHAMPS-ÉLYSÉES')
-- Result: 'Welcome To Champs-Élysées'
--
-- License: MIT
@pv8
pv8 / clean_dir.sh
Last active December 27, 2015 16:09
Handy script to remove older files from specific directories
#!/bin/sh
# delete files from $1 directory older than $2 days
echo Removing files in $1 older than $2 days...
find $1 -mtime +$2 -type f -delete
@pv8
pv8 / fibonacci.sql
Last active December 20, 2015 21:19
Fibonacci sequence with SQL (Common Table Expression)
-- == Fibonacci Example ==
WITH RECURSIVE fibo (a, b) AS (
SELECT 0, 1 -- Initial Subquery
UNION ALL
SELECT b, a+b FROM fibo -- Recursive Subquery
WHERE b < 100
)
SELECT a FROM fibo;
@pv8
pv8 / recursive_queries.sql
Last active December 20, 2015 21:19
Recursive query template for CTE and "connect by"
-- == setup ==
CREATE TABLE tree_table
(
tree_id integer NOT NULL,
tree_name varchar(100) NOT NULL,
tree_parent_id integer,
tree_order integer,
active boolean NOT NULL
);