Maria Garcia

39 code snips · on codesnips 5 months

Database Specialist with 12+ years optimizing data systems. Expert in PostgreSQL, MySQL, query optimization, and database architecture. Deep knowledge of indexing strategies,...

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
-- Logical backup with pg_dump
-- Single database
-- pg_dump -h localhost -U postgres -d mydb -F c -f mydb_backup.dump

-- All databases
-- pg_dumpall -h localhost -U postgres -f all_databases.sql

Database backup and recovery strategies

database backup recovery
by Maria Garcia 2 tabs
sql
-- Create roles
CREATE ROLE readonly;
CREATE ROLE readwrite;
CREATE ROLE admin WITH LOGIN PASSWORD 'secure_password';

-- Grant permissions to roles

Database security and access control

database security permissions
by Maria Garcia 2 tabs
sql
-- Create table with tsvector column
CREATE TABLE articles (
  id SERIAL PRIMARY KEY,
  title VARCHAR(200),
  content TEXT,
  author VARCHAR(100),

Full-text search with PostgreSQL and tsvector

postgresql full-text-search tsvector
by Maria Garcia 2 tabs
sql
-- PostgreSQL Declarative Partitioning (10+)

-- Create partitioned table by date range
CREATE TABLE measurements (
  id BIGSERIAL,
  sensor_id INT NOT NULL,

Table partitioning for large datasets

database partitioning postgresql
by Maria Garcia 2 tabs
sql
-- Primary server configuration (postgresql.conf)
-- wal_level = replica
-- max_wal_senders = 10
-- wal_keep_size = 64MB
-- hot_standby = on

Database replication and high availability strategies

database replication high-availability
by Maria Garcia 2 tabs
sql
-- Simple function
CREATE OR REPLACE FUNCTION get_full_name(
  first_name VARCHAR,
  last_name VARCHAR
)
RETURNS VARCHAR AS $$

Stored procedures and functions in PostgreSQL

postgresql stored-procedures functions
by Maria Garcia 2 tabs
sql
-- Unnormalized (0NF): Repeating groups
CREATE TABLE orders_bad (
  order_id INT,
  customer_name VARCHAR(100),
  customer_email VARCHAR(100),
  product1 VARCHAR(100),

Database normalization and schema design patterns

database normalization schema-design
by Maria Garcia 2 tabs
sql
-- Basic transaction
BEGIN;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

Database transactions and ACID properties

database transactions acid
by Maria Garcia 2 tabs
sql
-- Basic EXPLAIN
EXPLAIN
SELECT * FROM users WHERE email = 'alice@example.com';

-- EXPLAIN with cost and row estimates
-- Output shows: Seq Scan on users (cost=0.00..15.50 rows=1 width=100)

EXPLAIN and query plan optimization

sql explain query-optimization
by Maria Garcia 2 tabs
sql
-- Create table with JSONB column
CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  email VARCHAR(255) NOT NULL,
  name VARCHAR(255),
  metadata JSONB DEFAULT '{}'::jsonb

PostgreSQL JSONB for flexible schema design

postgresql jsonb json
by Maria Garcia 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