Skip to content

Instantly share code, notes, and snippets.

View bcambel's full-sized avatar
🌴
On vacation

Bahadir Cambel bcambel

🌴
On vacation
View GitHub Profile

Business Models

Advertising

Models Examples
Display ads Yahoo!
Search ads Google

Hey! I saw this has been indexed by the search engines. It is a first draft of a post I ended up publishing on my blog at: Scaling PostgreSQL With Pgpool and PgBouncer

Thanks for stopping by!

PostgreSQL and Pgpool Architecture

Replace a live table with DROP TABLE/ALTER TABLE. Suppose you are re-populating a read-only table that is live, so you don't want to drop the indexes or otherwise have no availability. What I do is create a new table based on the first, populate it (with COPY and no indexes), then drop the main table and alter the new table to make it named the first. Minimal downtime, e.g.:

CREATE TABLE table2 AS SELECT * FROM table LIMIT 1;
DELETE FROM table2
COPY table2 FROM '/tmp/path/to/file.txt';
CREATE INDEX blah2;
VACUUM ANALYZE VERBOSE table2;
ALTER TABLE table RENAME TO table3;
ALTER INDEX blah RENAME TO blah3;
@bcambel
bcambel / stop_index.sql
Created July 31, 2014 13:27
Stop index operations in Postgres
indisready = false
update pg_index set indisvalid = false where indexrelid = 'test_pkey'::regclass

Clojure Destructuring Tutorial and Cheat Sheet

(Related blog post)

Simply put, destructuring in Clojure is a way extract values from a datastructure and bind them to symbols, without having to explicitly traverse the datstructure. It allows for elegant and concise Clojure code.

Vectors

(defn extract-from-urls
"Takes a directory of tabbed files where URLs are the second field
(after UUID), fetches xhtml either from
dcache or the local cache (depending on doc/*use-local-cache*),
runs all extractors on the xhtml,
and writes JSON strings with extractor name/values and URL to json-
dir.
URLs with parse errors are written to parse-error-dir.
URLs not in dcache are written to cache-miss-dir.
Other errors are written to trap-dir.
@bcambel
bcambel / fizzbuzz.py
Last active August 29, 2015 14:08
FizzBuzz
def can(n,m): return n%m==0
for i in range(1,100):
if can(i,3) and can(i,5):
print "FizzBuzz",i
elif can(i,3):
print "Fizz",i
elif can(i,5):
print "Buzz",i
@bcambel
bcambel / heapsort.py
Created November 6, 2014 09:23
Heap sort implementation
def swap(i, j):
sqc[i], sqc[j] = sqc[j], sqc[i]
def heapify(end,i):
l=2 * i + 1
r=2 * (i + 1)
max=i
if l < end and sqc[i] < sqc[l]:
max = l
if r < end and sqc[max] < sqc[r]:
@bcambel
bcambel / selectionsort.py
Created November 6, 2014 11:10
Selection Sort
for i in range(len(source)):
mini = min(source[i:])
print "ITER", mini, source
min_index = source[i:].index(mini)
source[i + min_index] = source[i]
source[i] = mini
@bcambel
bcambel / quicksort.py
Created November 6, 2014 11:24
Quick sort
def qsort(arr):
print "ALL", arr
if len(arr) <= 1:
return arr
else:
left = qsort([x for x in arr[1:] if x<arr[0]])
middle = [arr[0]]
right = qsort([x for x in arr[1:] if x>=arr[0]])
print "LMR", left, middle, right
return left + middle + right