Skip to content

Instantly share code, notes, and snippets.

@jberkus
jberkus / xlog_numeric_location.sql
Last active February 20, 2016 20:30
Determine Most Caught-up Standby
-- For documentation on these functions, please see blog post at:
-- http://www.databasesoup.com/2012/10/determining-furthest-ahead-replica.html
-- determines current xlog location as a monotonically increasing numeric.
CREATE OR REPLACE FUNCTION xlog_location_numeric(vcloc text default NULL)
RETURNS numeric
LANGUAGE plpgsql
as $f$
DECLARE cloc text;
floc text[];
@revmischa
revmischa / pglisten.c
Last active February 23, 2023 19:52
Example postgresql asynchronous connection with LISTEN handler
#include <stdio.h>
#include <stdlib.h>
#include <libpq-fe.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
// channel to LISTEN on
const char *listenChannel = "foo";
@revmischa
revmischa / ResultSet.pm
Last active December 19, 2015 17:38
DBIx::Class generic resultset method to take the current query and estimate a count of the rows returned. Use `->estimate_count` in place of `->count` to get a fast, approximate count of complex queries.
Add the following to your base ResultSet class:
# fast row estimation function for postgres
# similar to ->count, but may be much faster for Pg < 9.2
# uses query planner row count estimation, accuracy depends on when
# ANALYZE/autovacuum was last performed on the table
# more info: http://wiki.postgresql.org/wiki/Count_estimate
sub estimate_count {
my ($self) = @_;
@jfrost
jfrost / gist:6584871
Created September 16, 2013 18:53
PostgreSQL query: completely unused indexes
-- Completely unused indexes:
SELECT relid::regclass as table, indexrelid::regclass as index
, pg_size_pretty(pg_relation_size(indexrelid))
FROM pg_stat_user_indexes
JOIN pg_index
USING (indexrelid)
WHERE idx_scan = 0
AND indisunique IS FALSE order by pg_relation_size(indexrelid);
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'
),
@jfrost
jfrost / gist:e36a4fdd2812b64c3ac9
Created November 5, 2014 21:12
pgbouncer simple benchmark results
So, I did a quick test with pgbouncer running on the local host and talking to a remote postgresql backend. I had the script open a connection, run a quick almost instantaneous query (SELECT now() ), collect the results and close the connection. It looped over this 50,000 times.
Connecting to the bouncer over local unix socket, it took 31s to perform all the queries.
Connecting to the bouncer over localhost, it took 45s to perform all the queries.
Connecting to the bouncer running on the remote server, it took 1m6s
Without using pgbouncer, it took 3m34s
@jberkus
jberkus / gist:43a74b63921aa58f90c8
Last active September 4, 2015 05:10
Simple stupid function to do python-style dict string replacement.
-- function for doing dictionary-style replacement of varaibles
-- in a SQL string. variables are marked in the text with ${var}
-- and replaced using a json dictionary
create or replace function replace_vars ( somestring text,
vars JSON )
returns text
language plpgsql
immutable
as
# only supply or default logfile path when none is given explicitly in
# postgresql.conf
my @options = ($pg_ctl, 'start', '-D', $info{'pgdata'});
my $logsize = 0;
if ($info{'logfile'}) {
push @options, ('-l', $info{'logfile'});
# remember current size of the log
$logsize = (stat $info{'logfile'})[7] || 0; # ignore stat errors
}
anonymous
anonymous / GAME_MASTER_v0_1.protobuf
Created July 16, 2016 16:31
Pokemon Go decoded GAME_MASTER protobuf file v0.1
Result: 1
Items {
TemplateId: "BADGE_BATTLE_ATTACK_WON"
Badge {
BadgeType: BADGE_BATTLE_ATTACK_WON
BadgeRanks: 4
Targets: "\nd\350\007"
}
}
Items {
(function() {
const xhrProto = XMLHttpRequest.prototype,
origOpen = xhrProto.open,
origSend = xhrProto.send;
const typingUrlRegex = /https:\/\/discord.com\/api\/v6\/channels\/[0-9]+\/typing/;
xhrProto.open = function (method, url) {
this._url = url;
return origOpen.apply(this, arguments);