@sylix/coworker 2.0.11 → 2.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/slash/config.d.ts.map +1 -1
- package/dist/commands/slash/config.js +22 -4
- package/dist/commands/slash/config.js.map +1 -1
- package/dist/core/CoWorkerAgent.d.ts.map +1 -1
- package/dist/core/CoWorkerAgent.js +6 -3
- package/dist/core/CoWorkerAgent.js.map +1 -1
- package/dist/skills/defaults/accessibility/screen-reader-testing.md +545 -0
- package/dist/skills/defaults/accessibility/wcag-audit-patterns.md +555 -0
- package/dist/skills/defaults/ai-ml/rag.md +276 -0
- package/dist/skills/defaults/backend-development/api-design-principles.md +528 -0
- package/dist/skills/defaults/backend-development/api-design.md +285 -0
- package/dist/skills/defaults/backend-development/architecture-patterns.md +494 -0
- package/dist/skills/defaults/backend-development/async-python.md +237 -0
- package/dist/skills/defaults/backend-development/auth-implementation-patterns.md +638 -0
- package/dist/skills/defaults/backend-development/bazel-build-optimization.md +387 -0
- package/dist/skills/defaults/backend-development/billing-automation/SKILL.md +566 -0
- package/dist/skills/defaults/backend-development/code-review-excellence.md +538 -0
- package/dist/skills/defaults/backend-development/cqrs-implementation.md +554 -0
- package/dist/skills/defaults/backend-development/database-design.md +305 -0
- package/dist/skills/defaults/backend-development/debugging-strategies.md +536 -0
- package/dist/skills/defaults/backend-development/e2e-testing-patterns.md +544 -0
- package/dist/skills/defaults/backend-development/error-handling-patterns.md +641 -0
- package/dist/skills/defaults/backend-development/fastapi-templates.md +559 -0
- package/dist/skills/defaults/backend-development/fastapi.md +309 -0
- package/dist/skills/defaults/backend-development/git-advanced-workflows.md +405 -0
- package/dist/skills/defaults/backend-development/microservices-patterns.md +595 -0
- package/dist/skills/defaults/backend-development/microservices.md +284 -0
- package/dist/skills/defaults/backend-development/monorepo-management.md +623 -0
- package/dist/skills/defaults/backend-development/nodejs-backend-patterns.md +1048 -0
- package/dist/skills/defaults/backend-development/nx-workspace-patterns.md +457 -0
- package/dist/skills/defaults/backend-development/paypal-integration/SKILL.md +478 -0
- package/dist/skills/defaults/backend-development/pci-compliance/SKILL.md +480 -0
- package/dist/skills/defaults/backend-development/python-anti-patterns.md +349 -0
- package/dist/skills/defaults/backend-development/python-background-jobs.md +364 -0
- package/dist/skills/defaults/backend-development/python-code-style.md +360 -0
- package/dist/skills/defaults/backend-development/python-configuration.md +368 -0
- package/dist/skills/defaults/backend-development/python-design-patterns.md +296 -0
- package/dist/skills/defaults/backend-development/python-error-handling.md +323 -0
- package/dist/skills/defaults/backend-development/python-packaging.md +887 -0
- package/dist/skills/defaults/backend-development/python-performance-optimization.md +874 -0
- package/dist/skills/defaults/backend-development/python-project-structure.md +252 -0
- package/dist/skills/defaults/backend-development/python-resilience.md +376 -0
- package/dist/skills/defaults/backend-development/python-resource-management.md +421 -0
- package/dist/skills/defaults/backend-development/python-type-safety.md +428 -0
- package/dist/skills/defaults/backend-development/sql-optimization-patterns.md +509 -0
- package/dist/skills/defaults/backend-development/stripe-integration/SKILL.md +522 -0
- package/dist/skills/defaults/backend-development/turborepo-caching.md +376 -0
- package/dist/skills/defaults/blockchain/defi-protocol-templates.md +430 -0
- package/dist/skills/defaults/blockchain/nft-standards.md +364 -0
- package/dist/skills/defaults/blockchain/solidity-security.md +514 -0
- package/dist/skills/defaults/blockchain/web3-testing.md +360 -0
- package/dist/skills/defaults/business/competitive-landscape/SKILL.md +527 -0
- package/dist/skills/defaults/business/market-sizing-analysis/SKILL.md +451 -0
- package/dist/skills/defaults/business/startup-financial-modeling/SKILL.md +494 -0
- package/dist/skills/defaults/business/startup-metrics-framework/SKILL.md +564 -0
- package/dist/skills/defaults/business/team-composition-analysis.md +437 -0
- package/dist/skills/defaults/compliance/employment-contract-templates/SKILL.md +527 -0
- package/dist/skills/defaults/compliance/gdpr-data-handling/SKILL.md +630 -0
- package/dist/skills/defaults/data-engineering/airflow-dag-patterns.md +436 -0
- package/dist/skills/defaults/data-engineering/airflow.md +519 -0
- package/dist/skills/defaults/data-engineering/data-quality.md +583 -0
- package/dist/skills/defaults/data-engineering/dbt-transformation-patterns.md +482 -0
- package/dist/skills/defaults/data-engineering/dbt.md +556 -0
- package/dist/skills/defaults/data-engineering/ml-pipeline-workflow/SKILL.md +247 -0
- package/dist/skills/defaults/data-engineering/spark-optimization.md +348 -0
- package/dist/skills/defaults/data-engineering/spark.md +411 -0
- package/dist/skills/defaults/database/postgresql.md +202 -0
- package/dist/skills/defaults/debugging/systematic-debugging.md +249 -0
- package/dist/skills/defaults/devops/architecture-decision-records.md +448 -0
- package/dist/skills/defaults/devops/changelog-automation.md +580 -0
- package/dist/skills/defaults/devops/cicd.md +314 -0
- package/dist/skills/defaults/devops/cloud.md +263 -0
- package/dist/skills/defaults/devops/code-review-excellence.md +299 -0
- package/dist/skills/defaults/devops/cost-optimization.md +295 -0
- package/dist/skills/defaults/devops/deployment-pipeline-design.md +356 -0
- package/dist/skills/defaults/devops/docker.md +281 -0
- package/dist/skills/defaults/devops/git-workflows.md +205 -0
- package/dist/skills/defaults/devops/github-actions.md +311 -0
- package/dist/skills/defaults/devops/gitlab-ci-patterns.md +266 -0
- package/dist/skills/defaults/devops/hybrid-cloud-networking.md +241 -0
- package/dist/skills/defaults/devops/istio-traffic-management.md +327 -0
- package/dist/skills/defaults/devops/kubernetes.md +339 -0
- package/dist/skills/defaults/devops/linkerd-patterns.md +311 -0
- package/dist/skills/defaults/devops/multi-cloud-architecture.md +181 -0
- package/dist/skills/defaults/devops/observability.md +243 -0
- package/dist/skills/defaults/devops/openapi-spec-generation.md +1024 -0
- package/dist/skills/defaults/devops/postmortem-writing.md +396 -0
- package/dist/skills/defaults/devops/prometheus-configuration.md +265 -0
- package/dist/skills/defaults/devops/secrets-management.md +341 -0
- package/dist/skills/defaults/devops/service-mesh-observability.md +385 -0
- package/dist/skills/defaults/devops/terraform-module-library.md +244 -0
- package/dist/skills/defaults/finance/backtesting-frameworks/SKILL.md +663 -0
- package/dist/skills/defaults/finance/risk-metrics-calculation/SKILL.md +557 -0
- package/dist/skills/defaults/frontend/accessibility-compliance.md +420 -0
- package/dist/skills/defaults/frontend/design-system-patterns.md +337 -0
- package/dist/skills/defaults/frontend/interaction-design.md +327 -0
- package/dist/skills/defaults/frontend/javascript.md +311 -0
- package/dist/skills/defaults/frontend/modern-javascript-patterns.md +927 -0
- package/dist/skills/defaults/frontend/react-native-design.md +440 -0
- package/dist/skills/defaults/frontend/react.md +345 -0
- package/dist/skills/defaults/frontend/responsive-design.md +472 -0
- package/dist/skills/defaults/frontend/tailwind-design-system.md +337 -0
- package/dist/skills/defaults/frontend/typescript-advanced-types.md +724 -0
- package/dist/skills/defaults/frontend/typescript.md +334 -0
- package/dist/skills/defaults/frontend/visual-design-foundations.md +326 -0
- package/dist/skills/defaults/frontend/web-component-design.md +279 -0
- package/dist/skills/defaults/game-development/godot-gdscript-patterns.md +188 -0
- package/dist/skills/defaults/game-development/unity-ecs-patterns.md +594 -0
- package/dist/skills/defaults/kubernetes/gitops-workflow.md +285 -0
- package/dist/skills/defaults/kubernetes/gitops.md +280 -0
- package/dist/skills/defaults/kubernetes/helm-chart-scaffolding.md +553 -0
- package/dist/skills/defaults/kubernetes/helm.md +343 -0
- package/dist/skills/defaults/kubernetes/k8s-manifest-generator.md +501 -0
- package/dist/skills/defaults/kubernetes/k8s-security-policies.md +342 -0
- package/dist/skills/defaults/kubernetes/manifests.md +330 -0
- package/dist/skills/defaults/kubernetes/security.md +337 -0
- package/dist/skills/defaults/llm-application/embedding-strategies.md +608 -0
- package/dist/skills/defaults/llm-application/hybrid-search-implementation.md +570 -0
- package/dist/skills/defaults/llm-application/hybrid-search.md +570 -0
- package/dist/skills/defaults/llm-application/langchain-architecture.md +666 -0
- package/dist/skills/defaults/llm-application/langchain.md +259 -0
- package/dist/skills/defaults/llm-application/llm-evaluation.md +695 -0
- package/dist/skills/defaults/llm-application/prompt-engineering-patterns.md +449 -0
- package/dist/skills/defaults/llm-application/prompt-engineering.md +219 -0
- package/dist/skills/defaults/llm-application/rag-implementation.md +434 -0
- package/dist/skills/defaults/llm-application/similarity-search-patterns.md +560 -0
- package/dist/skills/defaults/llm-application/similarity-search.md +560 -0
- package/dist/skills/defaults/llm-application/vector-index-tuning.md +523 -0
- package/dist/skills/defaults/mobile/mobile-android-design.md +440 -0
- package/dist/skills/defaults/mobile/mobile-ios-design.md +266 -0
- package/dist/skills/defaults/monitoring/distributed-tracing.md +436 -0
- package/dist/skills/defaults/monitoring/grafana-dashboards.md +370 -0
- package/dist/skills/defaults/monitoring/prometheus-configuration.md +379 -0
- package/dist/skills/defaults/monitoring/slo-implementation.md +323 -0
- package/dist/skills/defaults/refactoring/code-refactoring.md +349 -0
- package/dist/skills/defaults/security/anti-reversing-techniques/SKILL.md +559 -0
- package/dist/skills/defaults/security/auditor.md +168 -0
- package/dist/skills/defaults/security/binary-analysis-patterns/SKILL.md +438 -0
- package/dist/skills/defaults/security/memory-forensics/SKILL.md +483 -0
- package/dist/skills/defaults/security/mtls-configuration.md +349 -0
- package/dist/skills/defaults/security/protocol-reverse-engineering/SKILL.md +520 -0
- package/dist/skills/defaults/security/sast-configuration.md +182 -0
- package/dist/skills/defaults/security/security.md +313 -0
- package/dist/skills/defaults/security/stride-analysis.md +273 -0
- package/dist/skills/defaults/security/threat-mitigation-mapping.md +290 -0
- package/dist/skills/defaults/systems/bash-defensive-patterns/SKILL.md +539 -0
- package/dist/skills/defaults/systems/bats-testing-patterns/SKILL.md +631 -0
- package/dist/skills/defaults/systems/go-concurrency-patterns.md +657 -0
- package/dist/skills/defaults/systems/memory-safety-patterns.md +605 -0
- package/dist/skills/defaults/systems/rust-async-patterns.md +519 -0
- package/dist/skills/defaults/systems/shellcheck-configuration/SKILL.md +456 -0
- package/dist/skills/defaults/team-collaboration/multi-reviewer-patterns.md +126 -0
- package/dist/skills/defaults/team-collaboration/parallel-feature-development.md +151 -0
- package/dist/skills/defaults/testing/javascript-testing-patterns.md +1021 -0
- package/dist/skills/defaults/testing/python-testing-patterns.md +351 -0
- package/dist/skills/defaults/testing/testing.md +332 -0
- package/dist/skills/defaults/workflows/context-driven-development.md +384 -0
- package/dist/skills/defaults/workflows/track-management.md +592 -0
- package/dist/skills/defaults/workflows/workflow-patterns.md +622 -0
- package/dist/skills/index.d.ts +11 -0
- package/dist/skills/index.d.ts.map +1 -0
- package/dist/skills/index.js +129 -0
- package/dist/skills/index.js.map +1 -0
- package/dist/utils/character.js +4 -4
- package/dist/utils/character.js.map +1 -1
- package/dist/utils/inputbar.d.ts.map +1 -1
- package/dist/utils/inputbar.js +7 -0
- package/dist/utils/inputbar.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: spark-optimization
|
|
3
|
+
description: Optimize Apache Spark jobs with partitioning, caching, shuffle optimization, and memory tuning. Use when improving Spark performance, debugging slow jobs, or scaling data processing pipelines.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Apache Spark Optimization
|
|
7
|
+
|
|
8
|
+
Production patterns for optimizing Apache Spark jobs including partitioning strategies, memory management, shuffle optimization, and performance tuning.
|
|
9
|
+
|
|
10
|
+
## When to Use This Skill
|
|
11
|
+
|
|
12
|
+
- Optimizing slow Spark jobs
|
|
13
|
+
- Tuning memory and executor configuration
|
|
14
|
+
- Implementing efficient partitioning strategies
|
|
15
|
+
- Debugging Spark performance issues
|
|
16
|
+
- Scaling Spark pipelines for large datasets
|
|
17
|
+
- Reducing shuffle and data skew
|
|
18
|
+
|
|
19
|
+
## Core Concepts
|
|
20
|
+
|
|
21
|
+
### 1. Spark Execution Model
|
|
22
|
+
|
|
23
|
+
```
|
|
24
|
+
Driver Program
|
|
25
|
+
↓
|
|
26
|
+
Job (triggered by action)
|
|
27
|
+
↓
|
|
28
|
+
Stages (separated by shuffles)
|
|
29
|
+
↓
|
|
30
|
+
Tasks (one per partition)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### 2. Key Performance Factors
|
|
34
|
+
|
|
35
|
+
| Factor | Impact | Solution |
|
|
36
|
+
| ----------------- | --------------------- | ----------------------------- |
|
|
37
|
+
| **Shuffle** | Network I/O, disk I/O | Minimize wide transformations |
|
|
38
|
+
| **Data Skew** | Uneven task duration | Salting, broadcast joins |
|
|
39
|
+
| **Serialization** | CPU overhead | Use Kryo, columnar formats |
|
|
40
|
+
| **Memory** | GC pressure, spills | Tune executor memory |
|
|
41
|
+
| **Partitions** | Parallelism | Right-size partitions |
|
|
42
|
+
|
|
43
|
+
## Quick Start
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
from pyspark.sql import SparkSession
|
|
47
|
+
from pyspark.sql import functions as F
|
|
48
|
+
|
|
49
|
+
# Create optimized Spark session
|
|
50
|
+
spark = (SparkSession.builder
|
|
51
|
+
.appName("OptimizedJob")
|
|
52
|
+
.config("spark.sql.adaptive.enabled", "true")
|
|
53
|
+
.config("spark.sql.adaptive.coalescePartitions.enabled", "true")
|
|
54
|
+
.config("spark.sql.adaptive.skewJoin.enabled", "true")
|
|
55
|
+
.config("spark.serializer", "org.apache.spark.serializer.KryoSerializer")
|
|
56
|
+
.config("spark.sql.shuffle.partitions", "200")
|
|
57
|
+
.getOrCreate())
|
|
58
|
+
|
|
59
|
+
# Read with optimized settings
|
|
60
|
+
df = (spark.read
|
|
61
|
+
.format("parquet")
|
|
62
|
+
.option("mergeSchema", "false")
|
|
63
|
+
.load("s3://bucket/data/"))
|
|
64
|
+
|
|
65
|
+
# Efficient transformations
|
|
66
|
+
result = (df
|
|
67
|
+
.filter(F.col("date") >= "2024-01-01")
|
|
68
|
+
.select("id", "amount", "category")
|
|
69
|
+
.groupBy("category")
|
|
70
|
+
.agg(F.sum("amount").alias("total")))
|
|
71
|
+
|
|
72
|
+
result.write.mode("overwrite").parquet("s3://bucket/output/")
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
## Patterns
|
|
76
|
+
|
|
77
|
+
### Pattern 1: Optimal Partitioning
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
# Calculate optimal partition count
|
|
81
|
+
def calculate_partitions(data_size_gb: float, partition_size_mb: int = 128) -> int:
|
|
82
|
+
"""
|
|
83
|
+
Optimal partition size: 128MB - 256MB
|
|
84
|
+
Too few: Under-utilization, memory pressure
|
|
85
|
+
Too many: Task scheduling overhead
|
|
86
|
+
"""
|
|
87
|
+
return max(int(data_size_gb * 1024 / partition_size_mb), 1)
|
|
88
|
+
|
|
89
|
+
# Repartition for even distribution
|
|
90
|
+
df_repartitioned = df.repartition(200, "partition_key")
|
|
91
|
+
|
|
92
|
+
# Coalesce to reduce partitions (no shuffle)
|
|
93
|
+
df_coalesced = df.coalesce(100)
|
|
94
|
+
|
|
95
|
+
# Partition pruning with predicate pushdown
|
|
96
|
+
df = (spark.read.parquet("s3://bucket/data/")
|
|
97
|
+
.filter(F.col("date") == "2024-01-01")) # Spark pushes this down
|
|
98
|
+
|
|
99
|
+
# Write with partitioning for future queries
|
|
100
|
+
(df.write
|
|
101
|
+
.partitionBy("year", "month", "day")
|
|
102
|
+
.mode("overwrite")
|
|
103
|
+
.parquet("s3://bucket/partitioned_output/"))
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Pattern 2: Join Optimization
|
|
107
|
+
|
|
108
|
+
```python
|
|
109
|
+
from pyspark.sql import functions as F
|
|
110
|
+
from pyspark.sql.types import *
|
|
111
|
+
|
|
112
|
+
# 1. Broadcast Join - Small table joins
|
|
113
|
+
# Best when: One side < 10MB (configurable)
|
|
114
|
+
small_df = spark.read.parquet("s3://bucket/small_table/") # < 10MB
|
|
115
|
+
large_df = spark.read.parquet("s3://bucket/large_table/") # TBs
|
|
116
|
+
|
|
117
|
+
# Explicit broadcast hint
|
|
118
|
+
result = large_df.join(
|
|
119
|
+
F.broadcast(small_df),
|
|
120
|
+
on="key",
|
|
121
|
+
how="left"
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
# 2. Sort-Merge Join - Default for large tables
|
|
125
|
+
# Requires shuffle, but handles any size
|
|
126
|
+
result = large_df1.join(large_df2, on="key", how="inner")
|
|
127
|
+
|
|
128
|
+
# 3. Bucket Join - Pre-sorted, no shuffle at join time
|
|
129
|
+
# Write bucketed tables
|
|
130
|
+
(df.write
|
|
131
|
+
.bucketBy(200, "customer_id")
|
|
132
|
+
.sortBy("customer_id")
|
|
133
|
+
.mode("overwrite")
|
|
134
|
+
.saveAsTable("bucketed_orders"))
|
|
135
|
+
|
|
136
|
+
# Join bucketed tables (no shuffle!)
|
|
137
|
+
orders = spark.table("bucketed_orders")
|
|
138
|
+
customers = spark.table("bucketed_customers") # Same bucket count
|
|
139
|
+
result = orders.join(customers, on="customer_id")
|
|
140
|
+
|
|
141
|
+
# 4. Skew Join Handling
|
|
142
|
+
# Enable AQE skew join optimization
|
|
143
|
+
spark.conf.set("spark.sql.adaptive.skewJoin.enabled", "true")
|
|
144
|
+
spark.conf.set("spark.sql.adaptive.skewJoin.skewedPartitionFactor", "5")
|
|
145
|
+
spark.conf.set("spark.sql.adaptive.skewJoin.skewedPartitionThresholdInBytes", "256MB")
|
|
146
|
+
|
|
147
|
+
# Manual salting for severe skew
|
|
148
|
+
def salt_join(df_skewed, df_other, key_col, num_salts=10):
|
|
149
|
+
"""Add salt to distribute skewed keys"""
|
|
150
|
+
# Add salt to skewed side
|
|
151
|
+
df_salted = df_skewed.withColumn(
|
|
152
|
+
"salt",
|
|
153
|
+
(F.rand() * num_salts).cast("int")
|
|
154
|
+
).withColumn(
|
|
155
|
+
"salted_key",
|
|
156
|
+
F.concat(F.col(key_col), F.lit("_"), F.col("salt"))
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
# Explode other side with all salts
|
|
160
|
+
df_exploded = df_other.crossJoin(
|
|
161
|
+
spark.range(num_salts).withColumnRenamed("id", "salt")
|
|
162
|
+
).withColumn(
|
|
163
|
+
"salted_key",
|
|
164
|
+
F.concat(F.col(key_col), F.lit("_"), F.col("salt"))
|
|
165
|
+
)
|
|
166
|
+
|
|
167
|
+
# Join on salted key
|
|
168
|
+
return df_salted.join(df_exploded, on="salted_key", how="inner")
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Pattern 3: Caching and Persistence
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
from pyspark import StorageLevel
|
|
175
|
+
|
|
176
|
+
# Cache when reusing DataFrame multiple times
|
|
177
|
+
df = spark.read.parquet("s3://bucket/data/")
|
|
178
|
+
df_filtered = df.filter(F.col("status") == "active")
|
|
179
|
+
|
|
180
|
+
# Cache in memory (MEMORY_AND_DISK is default)
|
|
181
|
+
df_filtered.cache()
|
|
182
|
+
|
|
183
|
+
# Or with specific storage level
|
|
184
|
+
df_filtered.persist(StorageLevel.MEMORY_AND_DISK_SER)
|
|
185
|
+
|
|
186
|
+
# Force materialization
|
|
187
|
+
df_filtered.count()
|
|
188
|
+
|
|
189
|
+
# Use in multiple actions
|
|
190
|
+
agg1 = df_filtered.groupBy("category").count()
|
|
191
|
+
agg2 = df_filtered.groupBy("region").sum("amount")
|
|
192
|
+
|
|
193
|
+
# Unpersist when done
|
|
194
|
+
df_filtered.unpersist()
|
|
195
|
+
|
|
196
|
+
# Storage levels explained:
|
|
197
|
+
# MEMORY_ONLY - Fast, but may not fit
|
|
198
|
+
# MEMORY_AND_DISK - Spills to disk if needed (recommended)
|
|
199
|
+
# MEMORY_ONLY_SER - Serialized, less memory, more CPU
|
|
200
|
+
# DISK_ONLY - When memory is tight
|
|
201
|
+
# OFF_HEAP - Tungsten off-heap memory
|
|
202
|
+
|
|
203
|
+
# Checkpoint for complex lineage
|
|
204
|
+
spark.sparkContext.setCheckpointDir("s3://bucket/checkpoints/")
|
|
205
|
+
df_complex = (df
|
|
206
|
+
.join(other_df, "key")
|
|
207
|
+
.groupBy("category")
|
|
208
|
+
.agg(F.sum("amount")))
|
|
209
|
+
df_complex.checkpoint() # Breaks lineage, materializes
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Pattern 4: Memory Tuning
|
|
213
|
+
|
|
214
|
+
```python
|
|
215
|
+
# Executor memory configuration
|
|
216
|
+
# spark-submit --executor-memory 8g --executor-cores 4
|
|
217
|
+
|
|
218
|
+
# Memory breakdown (8GB executor):
|
|
219
|
+
# - spark.memory.fraction = 0.6 (60% = 4.8GB for execution + storage)
|
|
220
|
+
# - spark.memory.storageFraction = 0.5 (50% of 4.8GB = 2.4GB for cache)
|
|
221
|
+
# - Remaining 2.4GB for execution (shuffles, joins, sorts)
|
|
222
|
+
# - 40% = 3.2GB for user data structures and internal metadata
|
|
223
|
+
|
|
224
|
+
spark = (SparkSession.builder
|
|
225
|
+
.config("spark.executor.memory", "8g")
|
|
226
|
+
.config("spark.executor.memoryOverhead", "2g") # For non-JVM memory
|
|
227
|
+
.config("spark.memory.fraction", "0.6")
|
|
228
|
+
.config("spark.memory.storageFraction", "0.5")
|
|
229
|
+
.config("spark.sql.shuffle.partitions", "200")
|
|
230
|
+
# For memory-intensive operations
|
|
231
|
+
.config("spark.sql.autoBroadcastJoinThreshold", "50MB")
|
|
232
|
+
# Prevent OOM on large shuffles
|
|
233
|
+
.config("spark.sql.files.maxPartitionBytes", "128MB")
|
|
234
|
+
.getOrCreate())
|
|
235
|
+
|
|
236
|
+
# Monitor memory usage
|
|
237
|
+
def print_memory_usage(spark):
|
|
238
|
+
"""Print current memory usage"""
|
|
239
|
+
sc = spark.sparkContext
|
|
240
|
+
for executor in sc._jsc.sc().getExecutorMemoryStatus().keySet().toArray():
|
|
241
|
+
mem_status = sc._jsc.sc().getExecutorMemoryStatus().get(executor)
|
|
242
|
+
total = mem_status._1() / (1024**3)
|
|
243
|
+
free = mem_status._2() / (1024**3)
|
|
244
|
+
print(f"{executor}: {total:.2f}GB total, {free:.2f}GB free")
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Pattern 5: Shuffle Optimization
|
|
248
|
+
|
|
249
|
+
```python
|
|
250
|
+
# Reduce shuffle data size
|
|
251
|
+
spark.conf.set("spark.sql.shuffle.partitions", "auto") # With AQE
|
|
252
|
+
spark.conf.set("spark.shuffle.compress", "true")
|
|
253
|
+
spark.conf.set("spark.shuffle.spill.compress", "true")
|
|
254
|
+
|
|
255
|
+
# Pre-aggregate before shuffle
|
|
256
|
+
df_optimized = (df
|
|
257
|
+
# Local aggregation first (combiner)
|
|
258
|
+
.groupBy("key", "partition_col")
|
|
259
|
+
.agg(F.sum("value").alias("partial_sum"))
|
|
260
|
+
# Then global aggregation
|
|
261
|
+
.groupBy("key")
|
|
262
|
+
.agg(F.sum("partial_sum").alias("total")))
|
|
263
|
+
|
|
264
|
+
# Avoid shuffle with map-side operations
|
|
265
|
+
# BAD: Shuffle for each distinct
|
|
266
|
+
distinct_count = df.select("category").distinct().count()
|
|
267
|
+
|
|
268
|
+
# GOOD: Approximate distinct (no shuffle)
|
|
269
|
+
approx_count = df.select(F.approx_count_distinct("category")).collect()[0][0]
|
|
270
|
+
|
|
271
|
+
# Use coalesce instead of repartition when reducing partitions
|
|
272
|
+
df_reduced = df.coalesce(10) # No shuffle
|
|
273
|
+
|
|
274
|
+
# Optimize shuffle with compression
|
|
275
|
+
spark.conf.set("spark.io.compression.codec", "lz4") # Fast compression
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### Pattern 6: Data Format Optimization
|
|
279
|
+
|
|
280
|
+
```python
|
|
281
|
+
# Parquet optimizations
|
|
282
|
+
(df.write
|
|
283
|
+
.option("compression", "snappy") # Fast compression
|
|
284
|
+
.option("parquet.block.size", 128 * 1024 * 1024) # 128MB row groups
|
|
285
|
+
.parquet("s3://bucket/output/"))
|
|
286
|
+
|
|
287
|
+
# Column pruning - only read needed columns
|
|
288
|
+
df = (spark.read.parquet("s3://bucket/data/")
|
|
289
|
+
.select("id", "amount", "date")) # Spark only reads these columns
|
|
290
|
+
|
|
291
|
+
# Predicate pushdown - filter at storage level
|
|
292
|
+
df = (spark.read.parquet("s3://bucket/partitioned/year=2024/")
|
|
293
|
+
.filter(F.col("status") == "active")) # Pushed to Parquet reader
|
|
294
|
+
|
|
295
|
+
# Delta Lake optimizations
|
|
296
|
+
(df.write
|
|
297
|
+
.format("delta")
|
|
298
|
+
.option("optimizeWrite", "true") # Bin-packing
|
|
299
|
+
.option("autoCompact", "true") # Compact small files
|
|
300
|
+
.mode("overwrite")
|
|
301
|
+
.save("s3://bucket/delta_table/"))
|
|
302
|
+
|
|
303
|
+
# Z-ordering for multi-dimensional queries
|
|
304
|
+
spark.sql("""
|
|
305
|
+
OPTIMIZE delta.`s3://bucket/delta_table/`
|
|
306
|
+
ZORDER BY (customer_id, date)
|
|
307
|
+
""")
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
### Pattern 7: Monitoring and Debugging
|
|
311
|
+
|
|
312
|
+
```python
|
|
313
|
+
# Enable detailed metrics
|
|
314
|
+
spark.conf.set("spark.sql.codegen.wholeStage", "true")
|
|
315
|
+
spark.conf.set("spark.sql.execution.arrow.pyspark.enabled", "true")
|
|
316
|
+
|
|
317
|
+
# Explain query plan
|
|
318
|
+
df.explain(mode="extended")
|
|
319
|
+
# Modes: simple, extended, codegen, cost, formatted
|
|
320
|
+
|
|
321
|
+
# Get physical plan statistics
|
|
322
|
+
df.explain(mode="cost")
|
|
323
|
+
|
|
324
|
+
# Monitor task metrics
|
|
325
|
+
def analyze_stage_metrics(spark):
|
|
326
|
+
"""Analyze recent stage metrics"""
|
|
327
|
+
status_tracker = spark.sparkContext.statusTracker()
|
|
328
|
+
|
|
329
|
+
for stage_id in status_tracker.getActiveStageIds():
|
|
330
|
+
stage_info = status_tracker.getStageInfo(stage_id)
|
|
331
|
+
print(f"Stage {stage_id}:")
|
|
332
|
+
print(f" Tasks: {stage_info.numTasks}")
|
|
333
|
+
print(f" Completed: {stage_info.numCompletedTasks}")
|
|
334
|
+
print(f" Failed: {stage_info.numFailedTasks}")
|
|
335
|
+
|
|
336
|
+
# Identify data skew
|
|
337
|
+
def check_partition_skew(df):
|
|
338
|
+
"""Check for partition skew"""
|
|
339
|
+
partition_counts = (df
|
|
340
|
+
.withColumn("partition_id", F.spark_partition_id())
|
|
341
|
+
.groupBy("partition_id")
|
|
342
|
+
.count()
|
|
343
|
+
.orderBy(F.desc("count")))
|
|
344
|
+
|
|
345
|
+
partition_counts.show(20)
|
|
346
|
+
|
|
347
|
+
stats = partition_counts.select(
|
|
348
|
+
F.min("count").alias("min"),
|
|
349
|
+
F.max("count").alias("max"),
|
|
350
|
+
F.avg("count").alias("avg"),
|
|
351
|
+
F.stddev("count").alias("stddev")
|
|
352
|
+
).collect()[0]
|
|
353
|
+
|
|
354
|
+
skew_ratio = stats["max"] / stats["avg"]
|
|
355
|
+
print(f"Skew ratio: {skew_ratio:.2f}x (>2x indicates skew)")
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
## Configuration Cheat Sheet
|
|
359
|
+
|
|
360
|
+
```python
|
|
361
|
+
# Production configuration template
|
|
362
|
+
spark_configs = {
|
|
363
|
+
# Adaptive Query Execution (AQE)
|
|
364
|
+
"spark.sql.adaptive.enabled": "true",
|
|
365
|
+
"spark.sql.adaptive.coalescePartitions.enabled": "true",
|
|
366
|
+
"spark.sql.adaptive.skewJoin.enabled": "true",
|
|
367
|
+
|
|
368
|
+
# Memory
|
|
369
|
+
"spark.executor.memory": "8g",
|
|
370
|
+
"spark.executor.memoryOverhead": "2g",
|
|
371
|
+
"spark.memory.fraction": "0.6",
|
|
372
|
+
"spark.memory.storageFraction": "0.5",
|
|
373
|
+
|
|
374
|
+
# Parallelism
|
|
375
|
+
"spark.sql.shuffle.partitions": "200",
|
|
376
|
+
"spark.default.parallelism": "200",
|
|
377
|
+
|
|
378
|
+
# Serialization
|
|
379
|
+
"spark.serializer": "org.apache.spark.serializer.KryoSerializer",
|
|
380
|
+
"spark.sql.execution.arrow.pyspark.enabled": "true",
|
|
381
|
+
|
|
382
|
+
# Compression
|
|
383
|
+
"spark.io.compression.codec": "lz4",
|
|
384
|
+
"spark.shuffle.compress": "true",
|
|
385
|
+
|
|
386
|
+
# Broadcast
|
|
387
|
+
"spark.sql.autoBroadcastJoinThreshold": "50MB",
|
|
388
|
+
|
|
389
|
+
# File handling
|
|
390
|
+
"spark.sql.files.maxPartitionBytes": "128MB",
|
|
391
|
+
"spark.sql.files.openCostInBytes": "4MB",
|
|
392
|
+
}
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
## Best Practices
|
|
396
|
+
|
|
397
|
+
### Do's
|
|
398
|
+
|
|
399
|
+
- **Enable AQE** - Adaptive query execution handles many issues
|
|
400
|
+
- **Use Parquet/Delta** - Columnar formats with compression
|
|
401
|
+
- **Broadcast small tables** - Avoid shuffle for small joins
|
|
402
|
+
- **Monitor Spark UI** - Check for skew, spills, GC
|
|
403
|
+
- **Right-size partitions** - 128MB - 256MB per partition
|
|
404
|
+
|
|
405
|
+
### Don'ts
|
|
406
|
+
|
|
407
|
+
- **Don't collect large data** - Keep data distributed
|
|
408
|
+
- **Don't use UDFs unnecessarily** - Use built-in functions
|
|
409
|
+
- **Don't over-cache** - Memory is limited
|
|
410
|
+
- **Don't ignore data skew** - It dominates job time
|
|
411
|
+
- **Don't use `.count()` for existence** - Use `.take(1)` or `.isEmpty()`
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: postgresql-table-design
|
|
3
|
+
description: Design a PostgreSQL-specific schema. Covers best-practices, data types, indexing, constraints, performance patterns, and advanced features
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# PostgreSQL Table Design
|
|
7
|
+
|
|
8
|
+
## Core Rules
|
|
9
|
+
|
|
10
|
+
- Define a **PRIMARY KEY** for reference tables (users, orders, etc.). Not always needed for time-series/event/log data. When used, prefer `BIGINT GENERATED ALWAYS AS IDENTITY`; use `UUID` only when global uniqueness/opacity is needed.
|
|
11
|
+
- **Normalize first (to 3NF)** to eliminate data redundancy and update anomalies; denormalize **only** for measured, high-ROI reads where join performance is proven problematic. Premature denormalization creates maintenance burden.
|
|
12
|
+
- Add **NOT NULL** everywhere it's semantically required; use **DEFAULT**s for common values.
|
|
13
|
+
- Create **indexes for access paths you actually query**: PK/unique (auto), **FK columns (manual!)**, frequent filters/sorts, and join keys.
|
|
14
|
+
- Prefer **TIMESTAMPTZ** for event time; **NUMERIC** for money; **TEXT** for strings; **BIGINT** for integer values, **DOUBLE PRECISION** for floats (or `NUMERIC` for exact decimal arithmetic).
|
|
15
|
+
|
|
16
|
+
## PostgreSQL "Gotchas"
|
|
17
|
+
|
|
18
|
+
- **Identifiers**: unquoted → lowercased. Avoid quoted/mixed-case names. Convention: use `snake_case` for table/column names.
|
|
19
|
+
- **Unique + NULLs**: UNIQUE allows multiple NULLs. Use `UNIQUE (...) NULLS NOT DISTINCT` (PG15+) to restrict to one NULL.
|
|
20
|
+
- **FK indexes**: PostgreSQL **does not** auto-index FK columns. Add them.
|
|
21
|
+
- **No silent coercions**: length/precision overflows error out (no truncation). Example: inserting 999 into `NUMERIC(2,0)` fails with error, unlike some databases that silently truncate or round.
|
|
22
|
+
- **Sequences/identity have gaps** (normal; don't "fix"). Rollbacks, crashes, and concurrent transactions create gaps in ID sequences (1, 2, 5, 6...). This is expected behavior—don't try to make IDs consecutive.
|
|
23
|
+
- **Heap storage**: no clustered PK by default (unlike SQL Server/MySQL InnoDB); `CLUSTER` is one-off reorganization, not maintained on subsequent inserts. Row order on disk is insertion order unless explicitly clustered.
|
|
24
|
+
- **MVCC**: updates/deletes leave dead tuples; vacuum handles them—design to avoid hot wide-row churn.
|
|
25
|
+
|
|
26
|
+
## Data Types
|
|
27
|
+
|
|
28
|
+
- **IDs**: `BIGINT GENERATED ALWAYS AS IDENTITY` preferred (`GENERATED BY DEFAULT` also fine); `UUID` when merging/federating/used in a distributed system or for opaque IDs. Generate with `uuidv7()` (preferred if using PG18+) or `gen_random_uuid()` (if using an older PG version).
|
|
29
|
+
- **Integers**: prefer `BIGINT` unless storage space is critical; `INTEGER` for smaller ranges; avoid `SMALLINT` unless constrained.
|
|
30
|
+
- **Floats**: prefer `DOUBLE PRECISION` over `REAL` unless storage space is critical. Use `NUMERIC` for exact decimal arithmetic.
|
|
31
|
+
- **Strings**: prefer `TEXT`; if length limits needed, use `CHECK (LENGTH(col) <= n)` instead of `VARCHAR(n)`; avoid `CHAR(n)`. Use `BYTEA` for binary data. Large strings/binary (>2KB default threshold) automatically stored in TOAST with compression. TOAST storage: `PLAIN` (no TOAST), `EXTENDED` (compress + out-of-line), `EXTERNAL` (out-of-line, no compress), `MAIN` (compress, keep in-line if possible). Default `EXTENDED` usually optimal. Control with `ALTER TABLE tbl ALTER COLUMN col SET STORAGE strategy` and `ALTER TABLE tbl SET (toast_tuple_target = 4096)` for threshold. Case-insensitive: for locale/accent handling use non-deterministic collations; for plain ASCII use expression indexes on `LOWER(col)` (preferred unless column needs case-insensitive PK/FK/UNIQUE) or `CITEXT`.
|
|
32
|
+
- **Money**: `NUMERIC(p,s)` (never float).
|
|
33
|
+
- **Time**: `TIMESTAMPTZ` for timestamps; `DATE` for date-only; `INTERVAL` for durations. Avoid `TIMESTAMP` (without timezone). Use `now()` for transaction start time, `clock_timestamp()` for current wall-clock time.
|
|
34
|
+
- **Booleans**: `BOOLEAN` with `NOT NULL` constraint unless tri-state values are required.
|
|
35
|
+
- **Enums**: `CREATE TYPE ... AS ENUM` for small, stable sets (e.g. US states, days of week). For business-logic-driven and evolving values (e.g. order statuses) → use TEXT (or INT) + CHECK or lookup table.
|
|
36
|
+
- **Arrays**: `TEXT[]`, `INTEGER[]`, etc. Use for ordered lists where you query elements. Index with **GIN** for containment (`@>`, `<@`) and overlap (`&&`) queries. Access: `arr[1]` (1-indexed), `arr[1:3]` (slicing). Good for tags, categories; avoid for relations—use junction tables instead. Literal syntax: `'{val1,val2}'` or `ARRAY[val1,val2]`.
|
|
37
|
+
- **Range types**: `daterange`, `numrange`, `tstzrange` for intervals. Support overlap (`&&`), containment (`@>`), operators. Index with **GiST**. Good for scheduling, versioning, numeric ranges. Pick a bounds scheme and use it consistently; prefer `[)` (inclusive/exclusive) by default.
|
|
38
|
+
- **Network types**: `INET` for IP addresses, `CIDR` for network ranges, `MACADDR` for MAC addresses. Support network operators (`<<`, `>>`, `&&`).
|
|
39
|
+
- **Geometric types**: `POINT`, `LINE`, `POLYGON`, `CIRCLE` for 2D spatial data. Index with **GiST**. Consider **PostGIS** for advanced spatial features.
|
|
40
|
+
- **Text search**: `TSVECTOR` for full-text search documents, `TSQUERY` for search queries. Index `tsvector` with **GIN**. Always specify language: `to_tsvector('english', col)` and `to_tsquery('english', 'query')`. Never use single-argument versions. This applies to both index expressions and queries.
|
|
41
|
+
- **Domain types**: `CREATE DOMAIN email AS TEXT CHECK (VALUE ~ '^[^@]+@[^@]+$')` for reusable custom types with validation. Enforces constraints across tables.
|
|
42
|
+
- **Composite types**: `CREATE TYPE address AS (street TEXT, city TEXT, zip TEXT)` for structured data within columns. Access with `(col).field` syntax.
|
|
43
|
+
- **JSONB**: preferred over JSON; index with **GIN**. Use only for optional/semi-structured attrs. ONLY use JSON if the original ordering of the contents MUST be preserved.
|
|
44
|
+
- **Vector types**: `vector` type by `pgvector` for vector similarity search for embeddings.
|
|
45
|
+
|
|
46
|
+
### Do not use the following data types
|
|
47
|
+
|
|
48
|
+
- DO NOT use `timestamp` (without time zone); DO use `timestamptz` instead.
|
|
49
|
+
- DO NOT use `char(n)` or `varchar(n)`; DO use `text` instead.
|
|
50
|
+
- DO NOT use `money` type; DO use `numeric` instead.
|
|
51
|
+
- DO NOT use `timetz` type; DO use `timestamptz` instead.
|
|
52
|
+
- DO NOT use `timestamptz(0)` or any other precision specification; DO use `timestamptz` instead
|
|
53
|
+
- DO NOT use `serial` type; DO use `generated always as identity` instead.
|
|
54
|
+
|
|
55
|
+
## Table Types
|
|
56
|
+
|
|
57
|
+
- **Regular**: default; fully durable, logged.
|
|
58
|
+
- **TEMPORARY**: session-scoped, auto-dropped, not logged. Faster for scratch work.
|
|
59
|
+
- **UNLOGGED**: persistent but not crash-safe. Faster writes; good for caches/staging.
|
|
60
|
+
|
|
61
|
+
## Row-Level Security
|
|
62
|
+
|
|
63
|
+
Enable with `ALTER TABLE tbl ENABLE ROW LEVEL SECURITY`. Create policies: `CREATE POLICY user_access ON orders FOR SELECT TO app_users USING (user_id = current_user_id())`. Built-in user-based access control at the row level.
|
|
64
|
+
|
|
65
|
+
## Constraints
|
|
66
|
+
|
|
67
|
+
- **PK**: implicit UNIQUE + NOT NULL; creates a B-tree index.
|
|
68
|
+
- **FK**: specify `ON DELETE/UPDATE` action (`CASCADE`, `RESTRICT`, `SET NULL`, `SET DEFAULT`). Add explicit index on referencing column—speeds up joins and prevents locking issues on parent deletes/updates. Use `DEFERRABLE INITIALLY DEFERRED` for circular FK dependencies checked at transaction end.
|
|
69
|
+
- **UNIQUE**: creates a B-tree index; allows multiple NULLs unless `NULLS NOT DISTINCT` (PG15+). Standard behavior: `(1, NULL)` and `(1, NULL)` are allowed. With `NULLS NOT DISTINCT`: only one `(1, NULL)` allowed. Prefer `NULLS NOT DISTINCT` unless you specifically need duplicate NULLs.
|
|
70
|
+
- **CHECK**: row-local constraints; NULL values pass the check (three-valued logic). Example: `CHECK (price > 0)` allows NULL prices. Combine with `NOT NULL` to enforce: `price NUMERIC NOT NULL CHECK (price > 0)`.
|
|
71
|
+
- **EXCLUDE**: prevents overlapping values using operators. `EXCLUDE USING gist (room_id WITH =, booking_period WITH &&)` prevents double-booking rooms. Requires appropriate index type (often GiST).
|
|
72
|
+
|
|
73
|
+
## Indexing
|
|
74
|
+
|
|
75
|
+
- **B-tree**: default for equality/range queries (`=`, `<`, `>`, `BETWEEN`, `ORDER BY`)
|
|
76
|
+
- **Composite**: order matters—index used if equality on leftmost prefix (`WHERE a = ? AND b > ?` uses index on `(a,b)`, but `WHERE b = ?` does not). Put most selective/frequently filtered columns first.
|
|
77
|
+
- **Covering**: `CREATE INDEX ON tbl (id) INCLUDE (name, email)` - includes non-key columns for index-only scans without visiting table.
|
|
78
|
+
- **Partial**: for hot subsets (`WHERE status = 'active'` → `CREATE INDEX ON tbl (user_id) WHERE status = 'active'`). Any query with `status = 'active'` can use this index.
|
|
79
|
+
- **Expression**: for computed search keys (`CREATE INDEX ON tbl (LOWER(email))`). Expression must match exactly in WHERE clause: `WHERE LOWER(email) = 'user@example.com'`.
|
|
80
|
+
- **GIN**: JSONB containment/existence, arrays (`@>`, `?`), full-text search (`@@`)
|
|
81
|
+
- **GiST**: ranges, geometry, exclusion constraints
|
|
82
|
+
- **BRIN**: very large, naturally ordered data (time-series)—minimal storage overhead. Effective when row order on disk correlates with indexed column (insertion order or after `CLUSTER`).
|
|
83
|
+
|
|
84
|
+
## Partitioning
|
|
85
|
+
|
|
86
|
+
- Use for very large tables (>100M rows) where queries consistently filter on partition key (often time/date).
|
|
87
|
+
- Alternate use: use for tables where data maintenance tasks dictates e.g. data pruned or bulk replaced periodically
|
|
88
|
+
- **RANGE**: common for time-series (`PARTITION BY RANGE (created_at)`). Create partitions: `CREATE TABLE logs_2024_01 PARTITION OF logs FOR VALUES FROM ('2024-01-01') TO ('2024-02-01')`. **TimescaleDB** automates time-based or ID-based partitioning with retention policies and compression.
|
|
89
|
+
- **LIST**: for discrete values (`PARTITION BY LIST (region)`). Example: `FOR VALUES IN ('us-east', 'us-west')`.
|
|
90
|
+
- **HASH**: for even distribution when no natural key (`PARTITION BY HASH (user_id)`). Creates N partitions with modulus.
|
|
91
|
+
- **Constraint exclusion**: requires `CHECK` constraints on partitions for query planner to prune. Auto-created for declarative partitioning (PG10+).
|
|
92
|
+
- Prefer declarative partitioning or hypertables. Do NOT use table inheritance.
|
|
93
|
+
- **Limitations**: no global UNIQUE constraints—include partition key in PK/UNIQUE. FKs from partitioned tables not supported; use triggers.
|
|
94
|
+
|
|
95
|
+
## Special Considerations
|
|
96
|
+
|
|
97
|
+
### Update-Heavy Tables
|
|
98
|
+
|
|
99
|
+
- **Separate hot/cold columns**—put frequently updated columns in separate table to minimize bloat.
|
|
100
|
+
- **Use `fillfactor=90`** to leave space for HOT updates that avoid index maintenance.
|
|
101
|
+
- **Avoid updating indexed columns**—prevents beneficial HOT updates.
|
|
102
|
+
- **Partition by update patterns**—separate frequently updated rows in a different partition from stable data.
|
|
103
|
+
|
|
104
|
+
### Insert-Heavy Workloads
|
|
105
|
+
|
|
106
|
+
- **Minimize indexes**—only create what you query; every index slows inserts.
|
|
107
|
+
- **Use `COPY` or multi-row `INSERT`** instead of single-row inserts.
|
|
108
|
+
- **UNLOGGED tables** for rebuildable staging data—much faster writes.
|
|
109
|
+
- **Defer index creation** for bulk loads—>drop index, load data, recreate indexes.
|
|
110
|
+
- **Partition by time/hash** to distribute load. **TimescaleDB** automates partitioning and compression of insert-heavy data.
|
|
111
|
+
- **Use a natural key for primary key** such as a (timestamp, device_id) if enforcing global uniqueness is important many insert-heavy tables don't need a primary key at all.
|
|
112
|
+
- If you do need a surrogate key, **Prefer `BIGINT GENERATED ALWAYS AS IDENTITY` over `UUID`**.
|
|
113
|
+
|
|
114
|
+
### Upsert-Friendly Design
|
|
115
|
+
|
|
116
|
+
- **Requires UNIQUE index** on conflict target columns—`ON CONFLICT (col1, col2)` needs exact matching unique index (partial indexes don't work).
|
|
117
|
+
- **Use `EXCLUDED.column`** to reference would-be-inserted values; only update columns that actually changed to reduce write overhead.
|
|
118
|
+
- **`DO NOTHING` faster** than `DO UPDATE` when no actual update needed.
|
|
119
|
+
|
|
120
|
+
### Safe Schema Evolution
|
|
121
|
+
|
|
122
|
+
- **Transactional DDL**: most DDL operations can run in transactions and be rolled back—`BEGIN; ALTER TABLE...; ROLLBACK;` for safe testing.
|
|
123
|
+
- **Concurrent index creation**: `CREATE INDEX CONCURRENTLY` avoids blocking writes but can't run in transactions.
|
|
124
|
+
- **Volatile defaults cause rewrites**: adding `NOT NULL` columns with volatile defaults (e.g., `now()`, `gen_random_uuid()`) rewrites entire table. Non-volatile defaults are fast.
|
|
125
|
+
- **Drop constraints before columns**: `ALTER TABLE DROP CONSTRAINT` then `DROP COLUMN` to avoid dependency issues.
|
|
126
|
+
- **Function signature changes**: `CREATE OR REPLACE` with different arguments creates overloads, not replacements. DROP old version if no overload desired.
|
|
127
|
+
|
|
128
|
+
## Generated Columns
|
|
129
|
+
|
|
130
|
+
- `... GENERATED ALWAYS AS (<expr>) STORED` for computed, indexable fields. PG18+ adds `VIRTUAL` columns (computed on read, not stored).
|
|
131
|
+
|
|
132
|
+
## Extensions
|
|
133
|
+
|
|
134
|
+
- **`pgcrypto`**: `crypt()` for password hashing.
|
|
135
|
+
- **`uuid-ossp`**: alternative UUID functions; prefer `pgcrypto` for new projects.
|
|
136
|
+
- **`pg_trgm`**: fuzzy text search with `%` operator, `similarity()` function. Index with GIN for `LIKE '%pattern%'` acceleration.
|
|
137
|
+
- **`citext`**: case-insensitive text type. Prefer expression indexes on `LOWER(col)` unless you need case-insensitive constraints.
|
|
138
|
+
- **`btree_gin`/`btree_gist`**: enable mixed-type indexes (e.g., GIN index on both JSONB and text columns).
|
|
139
|
+
- **`hstore`**: key-value pairs; mostly superseded by JSONB but useful for simple string mappings.
|
|
140
|
+
- **`timescaledb`**: essential for time-series—automated partitioning, retention, compression, continuous aggregates.
|
|
141
|
+
- **`postgis`**: comprehensive geospatial support beyond basic geometric types—essential for location-based applications.
|
|
142
|
+
- **`pgvector`**: vector similarity search for embeddings.
|
|
143
|
+
- **`pgaudit`**: audit logging for all database activity.
|
|
144
|
+
|
|
145
|
+
## JSONB Guidance
|
|
146
|
+
|
|
147
|
+
- Prefer `JSONB` with **GIN** index.
|
|
148
|
+
- Default: `CREATE INDEX ON tbl USING GIN (jsonb_col);` → accelerates:
|
|
149
|
+
- **Containment** `jsonb_col @> '{"k":"v"}'`
|
|
150
|
+
- **Key existence** `jsonb_col ? 'k'`, **any/all keys** `?|`, `?&`
|
|
151
|
+
- **Path containment** on nested docs
|
|
152
|
+
- **Disjunction** `jsonb_col @> ANY(ARRAY['{"status":"active"}', '{"status":"pending"}'])`
|
|
153
|
+
- Heavy `@>` workloads: consider opclass `jsonb_path_ops` for smaller/faster containment-only indexes:
|
|
154
|
+
- `CREATE INDEX ON tbl USING GIN (jsonb_col jsonb_path_ops);`
|
|
155
|
+
- **Trade-off**: loses support for key existence (`?`, `?|`, `?&`) queries—only supports containment (`@>`)
|
|
156
|
+
- Equality/range on a specific scalar field: extract and index with B-tree (generated column or expression):
|
|
157
|
+
- `ALTER TABLE tbl ADD COLUMN price INT GENERATED ALWAYS AS ((jsonb_col->>'price')::INT) STORED;`
|
|
158
|
+
- `CREATE INDEX ON tbl (price);`
|
|
159
|
+
- Prefer queries like `WHERE price BETWEEN 100 AND 500` (uses B-tree) over `WHERE (jsonb_col->>'price')::INT BETWEEN 100 AND 500` without index.
|
|
160
|
+
- Arrays inside JSONB: use GIN + `@>` for containment (e.g., tags). Consider `jsonb_path_ops` if only doing containment.
|
|
161
|
+
- Keep core relations in tables; use JSONB for optional/variable attributes.
|
|
162
|
+
- Use constraints to limit allowed JSONB values in a column e.g. `config JSONB NOT NULL CHECK(jsonb_typeof(config) = 'object')`
|
|
163
|
+
|
|
164
|
+
## Examples
|
|
165
|
+
|
|
166
|
+
### Users
|
|
167
|
+
|
|
168
|
+
```sql
|
|
169
|
+
CREATE TABLE users (
|
|
170
|
+
user_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
171
|
+
email TEXT NOT NULL UNIQUE,
|
|
172
|
+
name TEXT NOT NULL,
|
|
173
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
174
|
+
);
|
|
175
|
+
CREATE UNIQUE INDEX ON users (LOWER(email));
|
|
176
|
+
CREATE INDEX ON users (created_at);
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
### Orders
|
|
180
|
+
|
|
181
|
+
```sql
|
|
182
|
+
CREATE TABLE orders (
|
|
183
|
+
order_id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
184
|
+
user_id BIGINT NOT NULL REFERENCES users(user_id),
|
|
185
|
+
status TEXT NOT NULL DEFAULT 'PENDING' CHECK (status IN ('PENDING','PAID','CANCELED')),
|
|
186
|
+
total NUMERIC(10,2) NOT NULL CHECK (total > 0),
|
|
187
|
+
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
188
|
+
);
|
|
189
|
+
CREATE INDEX ON orders (user_id);
|
|
190
|
+
CREATE INDEX ON orders (created_at);
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### JSONB
|
|
194
|
+
|
|
195
|
+
```sql
|
|
196
|
+
CREATE TABLE profiles (
|
|
197
|
+
user_id BIGINT PRIMARY KEY REFERENCES users(user_id),
|
|
198
|
+
attrs JSONB NOT NULL DEFAULT '{}',
|
|
199
|
+
theme TEXT GENERATED ALWAYS AS (attrs->>'theme') STORED
|
|
200
|
+
);
|
|
201
|
+
CREATE INDEX profiles_attrs_gin ON profiles USING GIN (attrs);
|
|
202
|
+
```
|