sql

sql
-- Enable pg_stat_statements extension
CREATE EXTENSION IF NOT EXISTS pg_stat_statements;

-- postgresql.conf:
-- shared_preload_libraries = 'pg_stat_statements'
-- pg_stat_statements.track = all

Query performance monitoring and profiling

database monitoring performance
by Maria Garcia 2 tabs
sql
-- Product.active.touch_all(:cache_synced_at) for catalog 42
UPDATE "products"
SET "updated_at" = '2024-05-01 12:00:00.123456',
    "cache_synced_at" = '2024-05-01 12:00:00.123456'
WHERE "products"."catalog_id" = 42
  AND "products"."status" = 'active';

Use `touch_all` for Efficient “Bump Updated At”

rails activerecord performance
by codesnips 4 tabs
sql
ALTER TABLE documents ADD COLUMN version BIGINT NOT NULL DEFAULT 0;

Optimistic locking with a version column

go postgres sql
by Leah Thompson 2 tabs
sql
-- Basic CTE
WITH high_value_customers AS (
  SELECT
    user_id,
    SUM(total) as lifetime_value
  FROM orders

Common Table Expressions (CTEs) for readable queries

sql cte common-table-expressions
by Maria Garcia 2 tabs
sql
-- Basic query debugging with EXPLAIN
EXPLAIN SELECT * FROM users WHERE email = 'test@example.com';

-- Output shows query plan:
-- Seq Scan on users (cost=0.00..25.00 rows=1 width=100)
--   Filter: (email = 'test@example.com'::text)

Query debugging and troubleshooting techniques

postgresql debugging troubleshooting
by Maria Garcia 2 tabs
sql
CREATE TABLE daily_events (
    id          BIGSERIAL PRIMARY KEY,
    category    TEXT        NOT NULL,
    item_id     BIGINT      NOT NULL,
    score       NUMERIC(10,2) NOT NULL DEFAULT 0,
    occurred_at TIMESTAMPTZ NOT NULL DEFAULT now()

Database-Driven “Daily Top” with window functions

postgres sql analytics
by codesnips 3 tabs
sql
-- Import CSV with COPY (fastest method)
COPY users (username, email, age, created_at)
FROM '/path/to/users.csv'
WITH (
  FORMAT csv,
  HEADER true,

Efficient data import and export strategies

database import export
by Maria Garcia 2 tabs
sql
INSERT INTO snip_counters (snip_id, views)
VALUES ($1, 1)
ON CONFLICT (snip_id)
DO UPDATE SET views = snip_counters.views + 1;

SQL upsert for counters (ON CONFLICT DO UPDATE)

postgres sql concurrency
by Mateo Rodriguez 1 tab
sql
-- Install postgres_fdw extension
CREATE EXTENSION IF NOT EXISTS postgres_fdw;

-- Create foreign server
CREATE SERVER remote_db
  FOREIGN DATA WRAPPER postgres_fdw

Foreign Data Wrappers for external data access

postgresql fdw foreign-data-wrapper
by Maria Garcia 2 tabs
sql
-- INNER JOIN: Only matching rows
SELECT
  users.name,
  orders.order_number,
  orders.total
FROM users

Advanced SQL joins and query optimization

sql joins query-optimization
by Maria Garcia 2 tabs
sql
CREATE TABLE IF NOT EXISTS idempotency_keys (
  id BIGSERIAL PRIMARY KEY,
  key TEXT NOT NULL UNIQUE,
  request_hash TEXT NOT NULL,
  response_json JSONB NOT NULL,
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()

Idempotency keys for “create” endpoints

api reliability postgres
by Mateo Rodriguez 2 tabs
sql
-- Basic VACUUM (reclaims dead tuple space)
VACUUM users;

-- VACUUM all tables in database
VACUUM;

Database maintenance with VACUUM and ANALYZE

postgresql vacuum analyze
by Maria Garcia 2 tabs