Skip to content

Instantly share code, notes, and snippets.

View atvanguard's full-sized avatar
🔭
everyday m hubblin'

atvanguard atvanguard

🔭
everyday m hubblin'
View GitHub Profile
api_logs=> \d logs
Table "public.logs"
Column | Type | Modifiers
-----------+-----------------------------+-----------------
id | bigint | not null
api_key | character varying | not null
statuscode | integer |
latency | integer |
method | character varying |
created_at | timestamp without time zone | default now()
SELECT count(*) FROM logs
WHERE api_key = 'SEM3XXXXXXXXXYYYYYYYY'
AND statuscode >= 500
AND created_at::date >= '2017–10–20' AND created_at::date < '2017–10–21';
-- Note that the legacy logs table already exists and logs_main is intended to be the master table
api_logs=> CREATE TABLE logs_main (LIKE logs INCLUDING DEFAULTS INCLUDING INDEXES);
api_logs=> ALTER TABLE logs INHERIT logs_main;
api_logs=> BEGIN;
-- logs_201709 contains the logs from the beginning of time till September, 2017
api_logs=> ALTER TABLE logs RENAME TO logs_201709;
EXPLAIN SELECT count(*) FROM logs
WHERE api_key = 'SEM3XXXXXXXXXYYYYYYYY'
AND statuscode >= 500
AND created_at >= '2017–10–20' AND created_at < '2017–10–21';
QUERY PLAN
--------------------------------------------------------------------
Aggregate (cost=16.36..16.37 rows=1 width=0)
-> Append (cost=0.00..16.35 rows=3 width=0)
QUERY PLAN
--------------------------------------------------------------------
Aggregate (cost=8.18..8.19 rows=1 width=0)
-> Append (cost=0.00..8.17 rows=2 width=0)
-> Seq Scan on logs (cost=0.00..0.00 rows=1 width=0)
Filter: ((statuscode >= 500) AND ("created_at" >= '2017-10-20 00:00:00'::timestamp without time zone) AND ("created_at" < '2017-10-21 00:00:00'::timestamp without time zone) AND ((api_key)::text = 'SEM3XXXXXXXXXYYYYYYYY'::text))
-> Index Scan using logs_201710_api_key_created_at_idx on logs_201710 (cost=0.15..8.17 rows=1 width=0)
Index Cond: (((api_key)::text = 'SEM3XXXXXXXXXYYYYYYYY'::text) AND ("created_at" >= '2017-10-20 00:00:00'::timestamp without time zone) AND ("created_at" < '2017-10-21 00:00:00'::timestamp without time zone))
api_logs=> CREATE OR REPLACE FUNCTION logs_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF ( NEW.created_at < '2017-10-01T00:00:00' ) THEN
INSERT INTO logs_201709 VALUES (NEW.*);
ELSIF ( NEW.created_at >= '2017-10-01T00:00:00' AND
NEW.created_at < '2017-11-01T00:00:00' ) THEN
INSERT INTO logs_201710 VALUES (NEW.*);
ELSE
RAISE EXCEPTION 'Date out of range. Fix the logs_insert_trigger() function!';
import { Client } from 'pg';
const copyTo = require('pg-copy-streams').to;
const copyFrom = require('pg-copy-streams').from;
const fromDb = new Client({
host: 'oldDb.rds.amazonaws.com',
user: 'username',
password: 'password',
database: 'api_logs'
});
api_logs=# \d+ logs
Table "public.logs"
Column | Type | Modifiers | Storage | Stats target | Description
------------+-----------------------------+---------------+----------+--------------+-------------
id | bigint | not null | plain | |
api_key | character varying | not null | extended | |
statuscode | integer | | plain | |
latency | integer | | plain | |
method | character varying | | extended | |
created_at | timestamp without time zone | default now() | plain | |

Keybase proof

I hereby claim:

  • I am atvanguard on github.
  • I am atvanguard (https://keybase.io/atvanguard) on keybase.
  • I have a public key whose fingerprint is CEDE F950 D9C8 4A61 FFCE 1568 6F43 0E10 16B8 5EA2

To claim this, I am signing this object:

contract LiquidDemocracy {
enum State {Invalid, Created, Voted, Delegated}
struct Proposal {
string description;
uint numVotes;
}
Proposal[] public proposals;
address[] validVoters;