Skip to content

Instantly share code, notes, and snippets.

@lfittl
lfittl / output.txt
Last active September 20, 2016 23:24
Using pg_query fingerprinting to prevent SQL injections
#my_orm_method("1")
1
#my_orm_method("'abc'")
abc
#my_orm_method("1 UNION SELECT * FROM passwords")
test.rb:8:in `exec_with_fingerprint': Invalid fingerprint, rejecting (RuntimeError)
from test.rb:16:in `my_orm_method'
from test.rb:23:in `<main>'
@lfittl
lfittl / analyze_all.sh
Last active September 17, 2016 05:00
Running ANALYZE (or other table-specific commands) across distributed tables in a Citus cluster
# Analyzes all distributed tables
export PGHOST=...
export PGUSER=citus
export PGPASSWORD=...
#export PGSSLMODE=require
export PGDATABASE=citus
export PARALLEL_FACTOR=4
psql -tA -F" " -c "SELECT logicalrelid::regclass||'_'||s.shardid, \
# Place this in app/models/concerns/fix_composite_key_insert.rb
# Fix for https://github.com/composite-primary-keys/composite_primary_keys/issues/365
#
# Act as if the record has a single primary key column in some cases, in
# particular when we get called from _create_record with the RETURNING result:
# https://github.com/rails/rails/blob/master/activerecord/lib/active_record/persistence.rb#L560
module FixCompositeKeyInsert
extend ActiveSupport::Concern
@lfittl
lfittl / instructions.md
Last active August 29, 2016 01:00
pganalyze-collector installation instructions for other distributions

First, we'll need to place a few files in the right location:

curl -o /usr/bin/pganalyze-collector https://github.com/pganalyze/collector/releases/download/v0.9.4/pganalyze-collector-linux-amd64
curl -o /etc/systemd/system/pganalyze-collector.service https://github.com/pganalyze/collector/blob/master/contrib/systemd/pganalyze-collector.service
curl -o /etc/pganalyze-collector.conf https://github.com/pganalyze/collector/blob/master/contrib/pganalyze-collector.conf

In addition, run the following commands:

@lfittl
lfittl / example_queries.sql
Last active June 21, 2016 21:31
Example structure and queries from pganalyze timeseries data for tables/indices
SELECT
size_bytes AS table_size,
(SELECT SUM(size_bytes) FROM schema_index_stats sis JOIN schema_indices si ON (si.id = sis.schema_index_id) WHERE si.table_id = st.schema_table_id AND sis.snapshot_id = st.snapshot_id) AS index_size,
COALESCE((SELECT SUM(size_bytes) FROM schema_indices si JOIN schema_index_stats sis ON (si.id = sis.schema_index_id) WHERE si.table_id = st.schema_table_id AND sis.snapshot_id = st.snapshot_id), ?) + size_bytes AS total_size,
date_part(?, s.collected_at) AS collected_at
FROM
snapshots s JOIN schema_table_stats st
ON
(snapshot_id = s.id)
WHERE

Keybase proof

I hereby claim:

  • I am lfittl on github.
  • I am lukasfittl (https://keybase.io/lukasfittl) on keybase.
  • I have a public key whose fingerprint is CBB0 0AD9 8036 F614 093F 6EF0 F4BB 706E 4598 C323

To claim this, I am signing this object:

@lfittl
lfittl / gist:d54dc3e9754b975c936e
Last active May 10, 2023 12:13
Discussion in #citus on freenode about Citus' consistency guarantees (and the documentation about it)
[...]
<kellabyte> what happens if the master goes down?
<posi> So if citus master goes down, how does one recover?
<posi> kellabyte: https://www.citusdata.com/citusdb-architecture-whitepaper
<kellabyte> yeah, since all reads/writes must go through the master, what happens?
<kellabyte> ah thank you
<posi> "The master node keeps only metadata tables which are typically small (a few MBs in size). The metadata table can be replicated and quickly restored if the master node ever experiences a failure."
<ozgun> @posi Methods that use "logical replication" from PG to Citus will work. @andres noted that if your source table has "partitions" and the schema needs to change, you'll need to pay some attention
<lfittl_> @kellabyte https://www.citusdata.com/docs/citus/5.0/admin_guide/cluster_management.html#master-node-failures probably answers the question best
<lfittl_> (Use Postgres streaming replication is afaik the best approach here, but @ozgun knows best)
# 1. Migration:
add_column :users, :special_things, :jsonb, null: false, default: '{}'
# 2. Model
store_accessor :special_things, :my_setting
# 3. Form
@lfittl
lfittl / docker-postgres-slave.markdown
Last active February 4, 2024 07:53
Setting up a docker-ized Postgres Slave

(Note: This assumes you've already configured your master server for streaming replication)

export DATADIR=/data/postgres-9.3
export MASTER=192.168.0.1
export REPL_USER=replication
export REPL_PASSWORD=mypassword

Create a base backup:

@lfittl
lfittl / bad.rb
Last active August 29, 2015 14:04
Rails default_scope = evil
class Notification
belongs_to :user
default_scope { order('id desc') }
end
class User
has_many :notifications
end