postgres

go
package consumers

import (
  "context"

  "github.com/jackc/pgx/v5/pgxpool"

Idempotent event consumer with processed-events table

go postgres messaging
by Leah Thompson 1 tab
ruby
module CollectionCacheKey
  extend ActiveSupport::Concern

  def cache_key_for(scope)
    relation = scope.respond_to?(:all) ? scope.all : scope
    model = relation.klass

Deterministic Cache Keys for Collections

rails caching activerecord
by codesnips 3 tabs
ruby
class AddUniqueIndexToTags < ActiveRecord::Migration[7.1]
  disable_ddl_transaction!

  def up
    execute <<~SQL
      UPDATE tags SET name = LOWER(TRIM(name)) WHERE name IS NOT NULL;

Safer “find or create” with Unique Constraint + Retry

rails activerecord concurrency
by codesnips 3 tabs
ruby
class ExplainAnalyzer
  def initialize(relation, watched_tables:)
    @relation = relation
    @watched_tables = Array(watched_tables).map(&:to_s)
  end

Fast Fail for Missing Indexes (EXPLAIN sanity check)

rails postgres performance
by codesnips 3 tabs
sql
CREATE TABLE posts (
    id           bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    author_id    bigint NOT NULL REFERENCES authors (id),
    title        text   NOT NULL,
    body         text   NOT NULL,
    published_at timestamptz NOT NULL DEFAULT now()

Cursor-based pagination with stable ordering

postgres performance pagination
by codesnips 4 tabs
ruby
module RetryableTransaction
  module_function

  RETRIABLE = [ActiveRecord::Deadlocked, ActiveRecord::LockWaitTimeout].freeze

  def run(max_retries: 3, base_delay: 0.05, &block)

Deadlock-Aware Retry Wrapper

rails postgres reliability
by codesnips 3 tabs
go
package importers

import (
  "context"
  "encoding/csv"
  "io"

Streaming CSV import with batched inserts

go csv postgres
by Leah Thompson 1 tab
ruby
class CreateMaintenanceTasks < ActiveRecord::Migration[7.0]
  def change
    create_table :maintenance_tasks do |t|
      t.string :name, null: false
      t.string :state, null: false, default: "pending"
      t.datetime :started_at

Database-Backed “Run Once” Migrations for Maintenance Tasks

rails migrations background-jobs
by codesnips 3 tabs
ruby
class CreateEmailDeliveries < ActiveRecord::Migration[7.1]
  def change
    create_table :email_deliveries do |t|
      t.string :dedupe_key, null: false
      t.string :mailer, null: false
      t.string :action, null: false

Transactional Email “Send Once” with Delivered Marker

rails reliability activerecord
by codesnips 4 tabs
typescript
export interface PageInfo {
  hasNextPage: boolean;
  hasPreviousPage: boolean;
  startCursor: string | null;
  endCursor: string | null;
}

API pagination response contract (page info)

typescript pagination cursor
by codesnips 4 tabs
ruby
module ExistenceChecks
  extend ActiveSupport::Concern

  class_methods do
    def has_any?(conditions = {})
      relation = conditions.present? ? where(conditions) : all

Fast “Exists” Checks with select(1) and LIMIT

rails activerecord performance
by codesnips 4 tabs
typescript
import DataLoader from "dataloader";
import { Pool } from "pg";

export interface User { id: number; name: string; }
export interface Post { id: number; user_id: number; title: string; }

N+1 avoidance with DataLoader (GraphQL)

graphql performance dataloader
by codesnips 3 tabs