Skip to content

Instantly share code, notes, and snippets.

View thiagoa's full-sized avatar

Thiago Araújo Silva thiagoa

  • thoughtbot
  • Natal / RN - Brazil
View GitHub Profile
@thiagoa
thiagoa / distinct-on-join-vs-left-join.sql
Last active August 1, 2026 00:12
DISTINCT ON: JOIN vs LEFT JOIN plan differences
-- DISTINCT ON: INNER JOIN vs LEFT JOIN
--
-- With LEFT JOIN, Postgres switches to a Hash Right Join that loses
-- the index's sort order. It hash-joins all 5 million rows, sorts
-- them on disk, and then deduplicates. LIMIT can't stop early
-- because the Unique node sits on top of the sort.
--
-- With INNER JOIN, Postgres can use a Merge Join that walks the covering
-- index in order. The output is already sorted, so LIMIT stops
-- after the first 15 unique users — no disk sort at all.
@thiagoa
thiagoa / max-id-variation.sql
Created July 31, 2026 22:51
MAX(id) variation for finding the latest row and why it can break
-- A common variation uses MAX(id) to find the latest status:
SELECT u.id, s.status
FROM users u
JOIN user_statuses s ON s.user_id = u.id
WHERE s.id = (
SELECT MAX(id) FROM user_statuses s2 WHERE s2.user_id = u.id
);
-- This assumes the highest id is always the latest status.
-- That holds under normal operation, but breaks during backfills
@thiagoa
thiagoa / distinct-on-workaround.sql
Last active July 31, 2026 21:35
DISTINCT ON + LIMIT workaround for paginated queries
-- DISTINCT ON with LIMIT is slow because Postgres can't push the LIMIT
-- through the Unique node. It hash-joins all status rows, sorts them on
-- disk, and only then returns 15.
--
-- Workaround: pre-filter users with a subquery so Postgres only sorts the
-- status rows belonging to those users.
-- Slow (~1,900 ms with 5M status rows):
SELECT DISTINCT ON (user_statuses.user_id)
users.id, users.name, user_statuses.status
@thiagoa
thiagoa / has_one_lateral_join_hack.rb
Created July 24, 2026 14:42
Making has_one work efficiently with a lateral join (hacky but functional)
# Making has_one work efficiently with a lateral join
#
# The naive has_one with an ordering scope loads all matching rows
# and discards all but the most recent per user in Ruby. This hack
# makes the filtering happen at the SQL level.
#
# The trick: alias the users table as user_statuses so that Rails'
# preloader filters the users table instead of the user_statuses
# table before the lateral join runs. Without the aliasing trick,
# Rails would filter on the join table's user_id, so it would need
RSpec.shared_examples_for "an interface" do |objects, class_methods: false|
let(:class_methods) { class_methods }
it "classes have compatible interfaces" do
objects.each_cons(2) do |left, right|
left_params = params_for(left, method(:normalize_params))
right_params = params_for(right, method(:normalize_params))
diff = (left_params - right_params) + (right_params - left_params)
### Using TestOptions
#
# TestOptions is a Minitest extension that enables setup functionality
# at the test declaration level. This allows you to declaratively
# control test behavior through options passed at the class or
# individual test level.
#
# Use TestOptions for contextual/generic behavior that is not
# essential for understanding the test's purpose, such as VCR,
# freezing time, environment variables, etc.
require "json"
require "faraday"
# Configure before using or execute this code as-is
# and get a 401
module Configuration
class << self
attr_accessor :organization_id, :api_key
end
end
@thiagoa
thiagoa / linux-usb-file-copy-fix.md
Created November 3, 2021 01:14 — forked from 2E0PGS/linux-usb-file-copy-fix.md
Fix Ubuntu and other Linux slow/hanging file copying via USB.

If your running a x64 bit Ubuntu or other Linux and find USB transfers hang at the end apply this fix:

echo $((16*1024*1024)) > /proc/sys/vm/dirty_background_bytes
echo $((48*1024*1024)) > /proc/sys/vm/dirty_bytes

I suggest you edit your /etc/rc.local file to make this change persistant across reboots.

sudo nano /etc/rc.local

RSpec.shared_examples_for "check interface against canonical" do |canonical_interface, classes_to_check|
classes_to_check.each do |class_to_check|
example "#{class_to_check} conforms to the required interface", :aggregate_failures do
interface_methods = canonical_interface.public_instance_methods(false)
methods_to_check = class_to_check.public_instance_methods(false)
diff = interface_methods - methods_to_check
expect(diff).to be_empty, (<<~MESSAGE).split("\n").join(" ")
Expected #{class_to_check} to be polymorphic with
RSpec.shared_examples_for "interface checker" do
def diff_for(array_1, array_2)
(array_1 - array_2) + (array_2 - array_1)
end
def arities_for(object)
object.public_methods.map do |m|
arity = object.method(m).arity
[m, arity == -1 ? anything : arity]
end