nextjs-hackathon-stack 0.1.40 → 0.1.42
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/index.js +3 -63
- package/package.json +1 -1
- package/template/.claude/agents/backend.md +54 -0
- package/template/.claude/agents/business-analyst.md +195 -0
- package/template/.claude/agents/code-reviewer.md +76 -0
- package/template/.claude/agents/frontend.md +85 -0
- package/template/.claude/agents/security-researcher.md +54 -0
- package/template/.claude/agents/technical-lead.md +92 -0
- package/template/.claude/agents/test-qa.md +85 -0
- package/template/.claude/rules/architecture.mdc +48 -0
- package/template/.claude/rules/coding-standards.mdc +120 -0
- package/template/.claude/rules/components.mdc +49 -0
- package/template/.claude/rules/data-fetching.mdc +115 -0
- package/template/.claude/rules/forms.mdc +100 -0
- package/template/.claude/rules/general.mdc +54 -0
- package/template/.claude/rules/migrations.mdc +11 -0
- package/template/.claude/rules/nextjs.mdc +71 -0
- package/template/.claude/rules/security.mdc +108 -0
- package/template/.claude/rules/supabase.mdc +70 -0
- package/template/.claude/rules/testing.mdc +136 -0
- package/template/.claude/settings.json +16 -0
- package/template/.claude/skills/build-feature/SKILL.md +198 -0
- package/template/.claude/skills/build-feature/references/server-action-test-template.md +103 -0
- package/template/.claude/skills/create-api-route/SKILL.md +62 -0
- package/template/.claude/skills/discover-feature/SKILL.md +200 -0
- package/template/.claude/skills/memory/SKILL.md +208 -0
- package/template/.claude/skills/review-branch/SKILL.md +43 -0
- package/template/.claude/skills/review-branch/references/review-checklist.md +36 -0
- package/template/.claude/skills/security-audit/SKILL.md +40 -0
- package/template/.claude/skills/security-audit/references/audit-steps.md +41 -0
- package/template/.claude/skills/supabase/SKILL.md +105 -0
- package/template/.claude/skills/supabase/assets/feedback-issue-template.md +17 -0
- package/template/.claude/skills/supabase/references/skill-feedback.md +17 -0
- package/template/.claude/skills/supabase-postgres-best-practices/SKILL.md +65 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp__contributing.md +170 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp__sections.md +39 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp__template.md +34 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_advanced-full-text-search.md +55 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_advanced-jsonb-indexing.md +49 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_conn-idle-timeout.md +46 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_conn-limits.md +44 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_conn-pooling.md +41 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_conn-prepared-statements.md +46 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_data-batch-inserts.md +54 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_data-n-plus-one.md +53 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_data-pagination.md +50 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_data-upsert.md +50 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_lock-advisory.md +56 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_lock-deadlock-prevention.md +68 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_lock-short-transactions.md +50 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_lock-skip-locked.md +54 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_monitor-explain-analyze.md +45 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_monitor-pg-stat-statements.md +55 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_monitor-vacuum-analyze.md +55 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_query-composite-indexes.md +44 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_query-covering-indexes.md +40 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_query-index-types.md +48 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_query-missing-indexes.md +43 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_query-partial-indexes.md +45 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_schema-constraints.md +80 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_schema-data-types.md +46 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_schema-foreign-key-indexes.md +59 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_schema-lowercase-identifiers.md +55 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_schema-partitioning.md +55 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_schema-primary-keys.md +61 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_security-privileges.md +54 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_security-rls-basics.md +50 -0
- package/template/.claude/skills/supabase-postgres-best-practices/references/pgbp_security-rls-performance.md +57 -0
- package/template/.cursor/agents/business-analyst.md +197 -0
- package/template/.cursor/agents/technical-lead.md +3 -3
- package/template/.cursor/mcp.json +6 -2
- package/template/.cursor/skills/build-feature/SKILL.md +20 -21
- package/template/.cursor/skills/discover-feature/SKILL.md +118 -29
- package/template/.cursor/skills/supabase/SKILL.md +104 -0
- package/template/.cursor/skills/supabase/assets/feedback-issue-template.md +17 -0
- package/template/.cursor/skills/supabase/references/skill-feedback.md +17 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/SKILL.md +64 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp__contributing.md +170 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp__sections.md +39 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp__template.md +34 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_advanced-full-text-search.md +55 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_advanced-jsonb-indexing.md +49 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_conn-idle-timeout.md +46 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_conn-limits.md +44 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_conn-pooling.md +41 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_conn-prepared-statements.md +46 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_data-batch-inserts.md +54 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_data-n-plus-one.md +53 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_data-pagination.md +50 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_data-upsert.md +50 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_lock-advisory.md +56 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_lock-deadlock-prevention.md +68 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_lock-short-transactions.md +50 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_lock-skip-locked.md +54 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_monitor-explain-analyze.md +45 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_monitor-pg-stat-statements.md +55 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_monitor-vacuum-analyze.md +55 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_query-composite-indexes.md +44 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_query-covering-indexes.md +40 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_query-index-types.md +48 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_query-missing-indexes.md +43 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_query-partial-indexes.md +45 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_schema-constraints.md +80 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_schema-data-types.md +46 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_schema-foreign-key-indexes.md +59 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_schema-lowercase-identifiers.md +55 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_schema-partitioning.md +55 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_schema-primary-keys.md +61 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_security-privileges.md +54 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_security-rls-basics.md +50 -0
- package/template/.cursor/skills/supabase-postgres-best-practices/references/pgbp_security-rls-performance.md +57 -0
- package/template/.mcp.json +16 -0
- package/template/.opencode/agents/backend.md +72 -0
- package/template/.opencode/agents/business-analyst.md +153 -0
- package/template/.opencode/agents/code-reviewer.md +80 -0
- package/template/.opencode/agents/frontend.md +84 -0
- package/template/.opencode/agents/security-researcher.md +58 -0
- package/template/.opencode/agents/technical-lead.md +131 -0
- package/template/.opencode/agents/test-qa.md +103 -0
- package/template/.opencode/memory/architecture-snapshot.md +127 -0
- package/template/.opencode/skills/build-feature/SKILL.md +208 -0
- package/template/.opencode/skills/create-api-route/SKILL.md +63 -0
- package/template/.opencode/skills/discover-feature/SKILL.md +194 -0
- package/template/.opencode/skills/memory/SKILL.md +199 -0
- package/template/.opencode/skills/review-branch/SKILL.md +43 -0
- package/template/.opencode/skills/security-audit/SKILL.md +40 -0
- package/template/.opencode/skills/supabase/SKILL.md +105 -0
- package/template/.opencode/skills/supabase/assets/feedback-issue-template.md +17 -0
- package/template/.opencode/skills/supabase/references/skill-feedback.md +17 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/SKILL.md +65 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp__contributing.md +170 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp__sections.md +39 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp__template.md +34 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_advanced-full-text-search.md +55 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_advanced-jsonb-indexing.md +49 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_conn-idle-timeout.md +46 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_conn-limits.md +44 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_conn-pooling.md +41 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_conn-prepared-statements.md +46 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_data-batch-inserts.md +54 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_data-n-plus-one.md +53 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_data-pagination.md +50 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_data-upsert.md +50 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_lock-advisory.md +56 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_lock-deadlock-prevention.md +68 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_lock-short-transactions.md +50 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_lock-skip-locked.md +54 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_monitor-explain-analyze.md +45 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_monitor-pg-stat-statements.md +55 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_monitor-vacuum-analyze.md +55 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_query-composite-indexes.md +44 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_query-covering-indexes.md +40 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_query-index-types.md +48 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_query-missing-indexes.md +43 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_query-partial-indexes.md +45 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_schema-constraints.md +80 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_schema-data-types.md +46 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_schema-foreign-key-indexes.md +59 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_schema-lowercase-identifiers.md +55 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_schema-partitioning.md +55 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_schema-primary-keys.md +61 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_security-privileges.md +54 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_security-rls-basics.md +50 -0
- package/template/.opencode/skills/supabase-postgres-best-practices/references/pgbp_security-rls-performance.md +57 -0
- package/template/.requirements/README.md +1 -1
- package/template/AGENTS.md +1 -1
- package/template/CLAUDE.md +1 -1
- package/template/Dockerfile.memory +7 -0
- package/template/README.md +15 -2
- package/template/_gitignore +3 -0
- package/template/docker-compose.yml +28 -0
- package/template/ia-flow.md +341 -0
- package/template/opencode.json +23 -0
- package/template/.cursor/agents/business-intelligence.md +0 -83
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Create Composite Indexes for Multi-Column Queries
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 5-10x faster multi-column queries
|
|
5
|
+
tags: indexes, composite-index, multi-column, query-optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Create Composite Indexes for Multi-Column Queries
|
|
9
|
+
|
|
10
|
+
When queries filter on multiple columns, a composite index is more efficient than separate single-column indexes.
|
|
11
|
+
|
|
12
|
+
**Incorrect (separate indexes require bitmap scan):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Two separate indexes
|
|
16
|
+
create index orders_status_idx on orders (status);
|
|
17
|
+
create index orders_created_idx on orders (created_at);
|
|
18
|
+
|
|
19
|
+
-- Query must combine both indexes (slower)
|
|
20
|
+
select * from orders where status = 'pending' and created_at > '2024-01-01';
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Correct (composite index):**
|
|
24
|
+
|
|
25
|
+
```sql
|
|
26
|
+
-- Single composite index (leftmost column first for equality checks)
|
|
27
|
+
create index orders_status_created_idx on orders (status, created_at);
|
|
28
|
+
|
|
29
|
+
-- Query uses one efficient index scan
|
|
30
|
+
select * from orders where status = 'pending' and created_at > '2024-01-01';
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**Column order matters** - place equality columns first, range columns last:
|
|
34
|
+
|
|
35
|
+
```sql
|
|
36
|
+
-- Good: status (=) before created_at (>)
|
|
37
|
+
create index idx on orders (status, created_at);
|
|
38
|
+
|
|
39
|
+
-- Works for: WHERE status = 'pending'
|
|
40
|
+
-- Works for: WHERE status = 'pending' AND created_at > '2024-01-01'
|
|
41
|
+
-- Does NOT work for: WHERE created_at > '2024-01-01' (leftmost prefix rule)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Reference: [Multicolumn Indexes](https://www.postgresql.org/docs/current/indexes-multicolumn.html)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Covering Indexes to Avoid Table Lookups
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: 2-5x faster queries by eliminating heap fetches
|
|
5
|
+
tags: indexes, covering-index, include, index-only-scan
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Covering Indexes to Avoid Table Lookups
|
|
9
|
+
|
|
10
|
+
Covering indexes include all columns needed by a query, enabling index-only scans that skip the table entirely.
|
|
11
|
+
|
|
12
|
+
**Incorrect (index scan + heap fetch):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
create index users_email_idx on users (email);
|
|
16
|
+
|
|
17
|
+
-- Must fetch name and created_at from table heap
|
|
18
|
+
select email, name, created_at from users where email = 'user@example.com';
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Correct (index-only scan with INCLUDE):**
|
|
22
|
+
|
|
23
|
+
```sql
|
|
24
|
+
-- Include non-searchable columns in the index
|
|
25
|
+
create index users_email_idx on users (email) include (name, created_at);
|
|
26
|
+
|
|
27
|
+
-- All columns served from index, no table access needed
|
|
28
|
+
select email, name, created_at from users where email = 'user@example.com';
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Use INCLUDE for columns you SELECT but don't filter on:
|
|
32
|
+
|
|
33
|
+
```sql
|
|
34
|
+
-- Searching by status, but also need customer_id and total
|
|
35
|
+
create index orders_status_idx on orders (status) include (customer_id, total);
|
|
36
|
+
|
|
37
|
+
select status, customer_id, total from orders where status = 'shipped';
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Reference: [Index-Only Scans](https://www.postgresql.org/docs/current/indexes-index-only-scans.html)
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Choose the Right Index Type for Your Data
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 10-100x improvement with correct index type
|
|
5
|
+
tags: indexes, btree, gin, gist, brin, hash, index-types
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Choose the Right Index Type for Your Data
|
|
9
|
+
|
|
10
|
+
Different index types excel at different query patterns. The default B-tree isn't always optimal.
|
|
11
|
+
|
|
12
|
+
**Incorrect (B-tree for JSONB containment):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- B-tree cannot optimize containment operators
|
|
16
|
+
create index products_attrs_idx on products (attributes);
|
|
17
|
+
select * from products where attributes @> '{"color": "red"}';
|
|
18
|
+
-- Full table scan - B-tree doesn't support @> operator
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Correct (GIN for JSONB):**
|
|
22
|
+
|
|
23
|
+
```sql
|
|
24
|
+
-- GIN supports @>, ?, ?&, ?| operators
|
|
25
|
+
create index products_attrs_idx on products using gin (attributes);
|
|
26
|
+
select * from products where attributes @> '{"color": "red"}';
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Index type guide:
|
|
30
|
+
|
|
31
|
+
```sql
|
|
32
|
+
-- B-tree (default): =, <, >, BETWEEN, IN, IS NULL
|
|
33
|
+
create index users_created_idx on users (created_at);
|
|
34
|
+
|
|
35
|
+
-- GIN: arrays, JSONB, full-text search
|
|
36
|
+
create index posts_tags_idx on posts using gin (tags);
|
|
37
|
+
|
|
38
|
+
-- GiST: geometric data, range types, nearest-neighbor (KNN) queries
|
|
39
|
+
create index locations_idx on places using gist (location);
|
|
40
|
+
|
|
41
|
+
-- BRIN: large time-series tables (10-100x smaller)
|
|
42
|
+
create index events_time_idx on events using brin (created_at);
|
|
43
|
+
|
|
44
|
+
-- Hash: equality-only (slightly faster than B-tree for =)
|
|
45
|
+
create index sessions_token_idx on sessions using hash (token);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Reference: [Index Types](https://www.postgresql.org/docs/current/indexes-types.html)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Add Indexes on WHERE and JOIN Columns
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: 100-1000x faster queries on large tables
|
|
5
|
+
tags: indexes, performance, sequential-scan, query-optimization
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Add Indexes on WHERE and JOIN Columns
|
|
9
|
+
|
|
10
|
+
Queries filtering or joining on unindexed columns cause full table scans, which become exponentially slower as tables grow.
|
|
11
|
+
|
|
12
|
+
**Incorrect (sequential scan on large table):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- No index on customer_id causes full table scan
|
|
16
|
+
select * from orders where customer_id = 123;
|
|
17
|
+
|
|
18
|
+
-- EXPLAIN shows: Seq Scan on orders (cost=0.00..25000.00 rows=100 width=85)
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
**Correct (index scan):**
|
|
22
|
+
|
|
23
|
+
```sql
|
|
24
|
+
-- Create index on frequently filtered column
|
|
25
|
+
create index orders_customer_id_idx on orders (customer_id);
|
|
26
|
+
|
|
27
|
+
select * from orders where customer_id = 123;
|
|
28
|
+
|
|
29
|
+
-- EXPLAIN shows: Index Scan using orders_customer_id_idx (cost=0.42..8.44 rows=100 width=85)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
For JOIN columns, always index the foreign key side:
|
|
33
|
+
|
|
34
|
+
```sql
|
|
35
|
+
-- Index the referencing column
|
|
36
|
+
create index orders_customer_id_idx on orders (customer_id);
|
|
37
|
+
|
|
38
|
+
select c.name, o.total
|
|
39
|
+
from customers c
|
|
40
|
+
join orders o on o.customer_id = c.id;
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Reference: [Query Optimization](https://supabase.com/docs/guides/database/query-optimization)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Partial Indexes for Filtered Queries
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 5-20x smaller indexes, faster writes and queries
|
|
5
|
+
tags: indexes, partial-index, query-optimization, storage
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Partial Indexes for Filtered Queries
|
|
9
|
+
|
|
10
|
+
Partial indexes only include rows matching a WHERE condition, making them smaller and faster when queries consistently filter on the same condition.
|
|
11
|
+
|
|
12
|
+
**Incorrect (full index includes irrelevant rows):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Index includes all rows, even soft-deleted ones
|
|
16
|
+
create index users_email_idx on users (email);
|
|
17
|
+
|
|
18
|
+
-- Query always filters active users
|
|
19
|
+
select * from users where email = 'user@example.com' and deleted_at is null;
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Correct (partial index matches query filter):**
|
|
23
|
+
|
|
24
|
+
```sql
|
|
25
|
+
-- Index only includes active users
|
|
26
|
+
create index users_active_email_idx on users (email)
|
|
27
|
+
where deleted_at is null;
|
|
28
|
+
|
|
29
|
+
-- Query uses the smaller, faster index
|
|
30
|
+
select * from users where email = 'user@example.com' and deleted_at is null;
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Common use cases for partial indexes:
|
|
34
|
+
|
|
35
|
+
```sql
|
|
36
|
+
-- Only pending orders (status rarely changes once completed)
|
|
37
|
+
create index orders_pending_idx on orders (created_at)
|
|
38
|
+
where status = 'pending';
|
|
39
|
+
|
|
40
|
+
-- Only non-null values
|
|
41
|
+
create index products_sku_idx on products (sku)
|
|
42
|
+
where sku is not null;
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Reference: [Partial Indexes](https://www.postgresql.org/docs/current/indexes-partial.html)
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Add Constraints Safely in Migrations
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: Prevents migration failures and enables idempotent schema changes
|
|
5
|
+
tags: constraints, migrations, schema, alter-table
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Add Constraints Safely in Migrations
|
|
9
|
+
|
|
10
|
+
PostgreSQL does not support `ADD CONSTRAINT IF NOT EXISTS`. Migrations using this syntax will fail.
|
|
11
|
+
|
|
12
|
+
**Incorrect (causes syntax error):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- ERROR: syntax error at or near "not" (SQLSTATE 42601)
|
|
16
|
+
alter table public.profiles
|
|
17
|
+
add constraint if not exists profiles_birthchart_id_unique unique (birthchart_id);
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
**Correct (idempotent constraint creation):**
|
|
21
|
+
|
|
22
|
+
```sql
|
|
23
|
+
-- Use DO block to check before adding
|
|
24
|
+
do $$
|
|
25
|
+
begin
|
|
26
|
+
if not exists (
|
|
27
|
+
select 1 from pg_constraint
|
|
28
|
+
where conname = 'profiles_birthchart_id_unique'
|
|
29
|
+
and conrelid = 'public.profiles'::regclass
|
|
30
|
+
) then
|
|
31
|
+
alter table public.profiles
|
|
32
|
+
add constraint profiles_birthchart_id_unique unique (birthchart_id);
|
|
33
|
+
end if;
|
|
34
|
+
end $$;
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
For all constraint types:
|
|
38
|
+
|
|
39
|
+
```sql
|
|
40
|
+
-- Check constraints
|
|
41
|
+
do $$
|
|
42
|
+
begin
|
|
43
|
+
if not exists (
|
|
44
|
+
select 1 from pg_constraint
|
|
45
|
+
where conname = 'check_age_positive'
|
|
46
|
+
) then
|
|
47
|
+
alter table users add constraint check_age_positive check (age > 0);
|
|
48
|
+
end if;
|
|
49
|
+
end $$;
|
|
50
|
+
|
|
51
|
+
-- Foreign keys
|
|
52
|
+
do $$
|
|
53
|
+
begin
|
|
54
|
+
if not exists (
|
|
55
|
+
select 1 from pg_constraint
|
|
56
|
+
where conname = 'profiles_birthchart_id_fkey'
|
|
57
|
+
) then
|
|
58
|
+
alter table profiles
|
|
59
|
+
add constraint profiles_birthchart_id_fkey
|
|
60
|
+
foreign key (birthchart_id) references birthcharts(id);
|
|
61
|
+
end if;
|
|
62
|
+
end $$;
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Check if constraint exists:
|
|
66
|
+
|
|
67
|
+
```sql
|
|
68
|
+
-- Query to check constraint existence
|
|
69
|
+
select conname, contype, pg_get_constraintdef(oid)
|
|
70
|
+
from pg_constraint
|
|
71
|
+
where conrelid = 'public.profiles'::regclass;
|
|
72
|
+
|
|
73
|
+
-- contype values:
|
|
74
|
+
-- 'p' = PRIMARY KEY
|
|
75
|
+
-- 'f' = FOREIGN KEY
|
|
76
|
+
-- 'u' = UNIQUE
|
|
77
|
+
-- 'c' = CHECK
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Reference: [Constraints](https://www.postgresql.org/docs/current/ddl-constraints.html)
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Choose Appropriate Data Types
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 50% storage reduction, faster comparisons
|
|
5
|
+
tags: data-types, schema, storage, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Choose Appropriate Data Types
|
|
9
|
+
|
|
10
|
+
Using the right data types reduces storage, improves query performance, and prevents bugs.
|
|
11
|
+
|
|
12
|
+
**Incorrect (wrong data types):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
create table users (
|
|
16
|
+
id int, -- Will overflow at 2.1 billion
|
|
17
|
+
email varchar(255), -- Unnecessary length limit
|
|
18
|
+
created_at timestamp, -- Missing timezone info
|
|
19
|
+
is_active varchar(5), -- String for boolean
|
|
20
|
+
price varchar(20) -- String for numeric
|
|
21
|
+
);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Correct (appropriate data types):**
|
|
25
|
+
|
|
26
|
+
```sql
|
|
27
|
+
create table users (
|
|
28
|
+
id bigint generated always as identity primary key, -- 9 quintillion max
|
|
29
|
+
email text, -- No artificial limit, same performance as varchar
|
|
30
|
+
created_at timestamptz, -- Always store timezone-aware timestamps
|
|
31
|
+
is_active boolean default true, -- 1 byte vs variable string length
|
|
32
|
+
price numeric(10,2) -- Exact decimal arithmetic
|
|
33
|
+
);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Key guidelines:
|
|
37
|
+
|
|
38
|
+
```sql
|
|
39
|
+
-- IDs: use bigint, not int (future-proofing)
|
|
40
|
+
-- Strings: use text, not varchar(n) unless constraint needed
|
|
41
|
+
-- Time: use timestamptz, not timestamp
|
|
42
|
+
-- Money: use numeric, not float (precision matters)
|
|
43
|
+
-- Enums: use text with check constraint or create enum type
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Reference: [Data Types](https://www.postgresql.org/docs/current/datatype.html)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Index Foreign Key Columns
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: 10-100x faster JOINs and CASCADE operations
|
|
5
|
+
tags: foreign-key, indexes, joins, schema
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Index Foreign Key Columns
|
|
9
|
+
|
|
10
|
+
Postgres does not automatically index foreign key columns. Missing indexes cause slow JOINs and CASCADE operations.
|
|
11
|
+
|
|
12
|
+
**Incorrect (unindexed foreign key):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
create table orders (
|
|
16
|
+
id bigint generated always as identity primary key,
|
|
17
|
+
customer_id bigint references customers(id) on delete cascade,
|
|
18
|
+
total numeric(10,2)
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
-- No index on customer_id!
|
|
22
|
+
-- JOINs and ON DELETE CASCADE both require full table scan
|
|
23
|
+
select * from orders where customer_id = 123; -- Seq Scan
|
|
24
|
+
delete from customers where id = 123; -- Locks table, scans all orders
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Correct (indexed foreign key):**
|
|
28
|
+
|
|
29
|
+
```sql
|
|
30
|
+
create table orders (
|
|
31
|
+
id bigint generated always as identity primary key,
|
|
32
|
+
customer_id bigint references customers(id) on delete cascade,
|
|
33
|
+
total numeric(10,2)
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
-- Always index the FK column
|
|
37
|
+
create index orders_customer_id_idx on orders (customer_id);
|
|
38
|
+
|
|
39
|
+
-- Now JOINs and cascades are fast
|
|
40
|
+
select * from orders where customer_id = 123; -- Index Scan
|
|
41
|
+
delete from customers where id = 123; -- Uses index, fast cascade
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Find missing FK indexes:
|
|
45
|
+
|
|
46
|
+
```sql
|
|
47
|
+
select
|
|
48
|
+
conrelid::regclass as table_name,
|
|
49
|
+
a.attname as fk_column
|
|
50
|
+
from pg_constraint c
|
|
51
|
+
join pg_attribute a on a.attrelid = c.conrelid and a.attnum = any(c.conkey)
|
|
52
|
+
where c.contype = 'f'
|
|
53
|
+
and not exists (
|
|
54
|
+
select 1 from pg_index i
|
|
55
|
+
where i.indrelid = c.conrelid and a.attnum = any(i.indkey)
|
|
56
|
+
);
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Reference: [Foreign Keys](https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-FK)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Use Lowercase Identifiers for Compatibility
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: Avoid case-sensitivity bugs with tools, ORMs, and AI assistants
|
|
5
|
+
tags: naming, identifiers, case-sensitivity, schema, conventions
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Use Lowercase Identifiers for Compatibility
|
|
9
|
+
|
|
10
|
+
PostgreSQL folds unquoted identifiers to lowercase. Quoted mixed-case identifiers require quotes forever and cause issues with tools, ORMs, and AI assistants that may not recognize them.
|
|
11
|
+
|
|
12
|
+
**Incorrect (mixed-case identifiers):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Quoted identifiers preserve case but require quotes everywhere
|
|
16
|
+
CREATE TABLE "Users" (
|
|
17
|
+
"userId" bigint PRIMARY KEY,
|
|
18
|
+
"firstName" text,
|
|
19
|
+
"lastName" text
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
-- Must always quote or queries fail
|
|
23
|
+
SELECT "firstName" FROM "Users" WHERE "userId" = 1;
|
|
24
|
+
|
|
25
|
+
-- This fails - Users becomes users without quotes
|
|
26
|
+
SELECT firstName FROM Users;
|
|
27
|
+
-- ERROR: relation "users" does not exist
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
**Correct (lowercase snake_case):**
|
|
31
|
+
|
|
32
|
+
```sql
|
|
33
|
+
-- Unquoted lowercase identifiers are portable and tool-friendly
|
|
34
|
+
CREATE TABLE users (
|
|
35
|
+
user_id bigint PRIMARY KEY,
|
|
36
|
+
first_name text,
|
|
37
|
+
last_name text
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
-- Works without quotes, recognized by all tools
|
|
41
|
+
SELECT first_name FROM users WHERE user_id = 1;
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Common sources of mixed-case identifiers:
|
|
45
|
+
|
|
46
|
+
```sql
|
|
47
|
+
-- ORMs often generate quoted camelCase - configure them to use snake_case
|
|
48
|
+
-- Migrations from other databases may preserve original casing
|
|
49
|
+
-- Some GUI tools quote identifiers by default - disable this
|
|
50
|
+
|
|
51
|
+
-- If stuck with mixed-case, create views as a compatibility layer
|
|
52
|
+
CREATE VIEW users AS SELECT "userId" AS user_id, "firstName" AS first_name FROM "Users";
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Reference: [Identifiers and Key Words](https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS)
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Partition Large Tables for Better Performance
|
|
3
|
+
impact: MEDIUM-HIGH
|
|
4
|
+
impactDescription: 5-20x faster queries and maintenance on large tables
|
|
5
|
+
tags: partitioning, large-tables, time-series, performance
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Partition Large Tables for Better Performance
|
|
9
|
+
|
|
10
|
+
Partitioning splits a large table into smaller pieces, improving query performance and maintenance operations.
|
|
11
|
+
|
|
12
|
+
**Incorrect (single large table):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
create table events (
|
|
16
|
+
id bigint generated always as identity,
|
|
17
|
+
created_at timestamptz,
|
|
18
|
+
data jsonb
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
-- 500M rows, queries scan everything
|
|
22
|
+
select * from events where created_at > '2024-01-01'; -- Slow
|
|
23
|
+
vacuum events; -- Takes hours, locks table
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Correct (partitioned by time range):**
|
|
27
|
+
|
|
28
|
+
```sql
|
|
29
|
+
create table events (
|
|
30
|
+
id bigint generated always as identity,
|
|
31
|
+
created_at timestamptz not null,
|
|
32
|
+
data jsonb
|
|
33
|
+
) partition by range (created_at);
|
|
34
|
+
|
|
35
|
+
-- Create partitions for each month
|
|
36
|
+
create table events_2024_01 partition of events
|
|
37
|
+
for values from ('2024-01-01') to ('2024-02-01');
|
|
38
|
+
|
|
39
|
+
create table events_2024_02 partition of events
|
|
40
|
+
for values from ('2024-02-01') to ('2024-03-01');
|
|
41
|
+
|
|
42
|
+
-- Queries only scan relevant partitions
|
|
43
|
+
select * from events where created_at > '2024-01-15'; -- Only scans events_2024_01+
|
|
44
|
+
|
|
45
|
+
-- Drop old data instantly
|
|
46
|
+
drop table events_2023_01; -- Instant vs DELETE taking hours
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
When to partition:
|
|
50
|
+
|
|
51
|
+
- Tables > 100M rows
|
|
52
|
+
- Time-series data with date-based queries
|
|
53
|
+
- Need to efficiently drop old data
|
|
54
|
+
|
|
55
|
+
Reference: [Table Partitioning](https://www.postgresql.org/docs/current/ddl-partitioning.html)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Select Optimal Primary Key Strategy
|
|
3
|
+
impact: HIGH
|
|
4
|
+
impactDescription: Better index locality, reduced fragmentation
|
|
5
|
+
tags: primary-key, identity, uuid, serial, schema
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Select Optimal Primary Key Strategy
|
|
9
|
+
|
|
10
|
+
Primary key choice affects insert performance, index size, and replication
|
|
11
|
+
efficiency.
|
|
12
|
+
|
|
13
|
+
**Incorrect (problematic PK choices):**
|
|
14
|
+
|
|
15
|
+
```sql
|
|
16
|
+
-- identity is the SQL-standard approach
|
|
17
|
+
create table users (
|
|
18
|
+
id serial primary key -- Works, but IDENTITY is recommended
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
-- Random UUIDs (v4) cause index fragmentation
|
|
22
|
+
create table orders (
|
|
23
|
+
id uuid default gen_random_uuid() primary key -- UUIDv4 = random = scattered inserts
|
|
24
|
+
);
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Correct (optimal PK strategies):**
|
|
28
|
+
|
|
29
|
+
```sql
|
|
30
|
+
-- Use IDENTITY for sequential IDs (SQL-standard, best for most cases)
|
|
31
|
+
create table users (
|
|
32
|
+
id bigint generated always as identity primary key
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
-- For distributed systems needing UUIDs, use UUIDv7 (time-ordered)
|
|
36
|
+
-- Requires pg_uuidv7 extension: create extension pg_uuidv7;
|
|
37
|
+
create table orders (
|
|
38
|
+
id uuid default uuid_generate_v7() primary key -- Time-ordered, no fragmentation
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
-- Alternative: time-prefixed IDs for sortable, distributed IDs (no extension needed)
|
|
42
|
+
create table events (
|
|
43
|
+
id text default concat(
|
|
44
|
+
to_char(now() at time zone 'utc', 'YYYYMMDDHH24MISSMS'),
|
|
45
|
+
gen_random_uuid()::text
|
|
46
|
+
) primary key
|
|
47
|
+
);
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Guidelines:
|
|
51
|
+
|
|
52
|
+
- Single database: `bigint identity` (sequential, 8 bytes, SQL-standard)
|
|
53
|
+
- Distributed/exposed IDs: UUIDv7 (requires pg_uuidv7) or ULID (time-ordered, no
|
|
54
|
+
fragmentation)
|
|
55
|
+
- `serial` works but `identity` is SQL-standard and preferred for new
|
|
56
|
+
applications
|
|
57
|
+
- Avoid random UUIDs (v4) as primary keys on large tables (causes index
|
|
58
|
+
fragmentation)
|
|
59
|
+
|
|
60
|
+
Reference:
|
|
61
|
+
[Identity Columns](https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-GENERATED-IDENTITY)
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Apply Principle of Least Privilege
|
|
3
|
+
impact: MEDIUM
|
|
4
|
+
impactDescription: Reduced attack surface, better audit trail
|
|
5
|
+
tags: privileges, security, roles, permissions
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Apply Principle of Least Privilege
|
|
9
|
+
|
|
10
|
+
Grant only the minimum permissions required. Never use superuser for application queries.
|
|
11
|
+
|
|
12
|
+
**Incorrect (overly broad permissions):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Application uses superuser connection
|
|
16
|
+
-- Or grants ALL to application role
|
|
17
|
+
grant all privileges on all tables in schema public to app_user;
|
|
18
|
+
grant all privileges on all sequences in schema public to app_user;
|
|
19
|
+
|
|
20
|
+
-- Any SQL injection becomes catastrophic
|
|
21
|
+
-- drop table users; cascades to everything
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Correct (minimal, specific grants):**
|
|
25
|
+
|
|
26
|
+
```sql
|
|
27
|
+
-- Create role with no default privileges
|
|
28
|
+
create role app_readonly nologin;
|
|
29
|
+
|
|
30
|
+
-- Grant only SELECT on specific tables
|
|
31
|
+
grant usage on schema public to app_readonly;
|
|
32
|
+
grant select on public.products, public.categories to app_readonly;
|
|
33
|
+
|
|
34
|
+
-- Create role for writes with limited scope
|
|
35
|
+
create role app_writer nologin;
|
|
36
|
+
grant usage on schema public to app_writer;
|
|
37
|
+
grant select, insert, update on public.orders to app_writer;
|
|
38
|
+
grant usage on sequence orders_id_seq to app_writer;
|
|
39
|
+
-- No DELETE permission
|
|
40
|
+
|
|
41
|
+
-- Login role inherits from these
|
|
42
|
+
create role app_user login password 'xxx';
|
|
43
|
+
grant app_writer to app_user;
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Revoke public defaults:
|
|
47
|
+
|
|
48
|
+
```sql
|
|
49
|
+
-- Revoke default public access
|
|
50
|
+
revoke all on schema public from public;
|
|
51
|
+
revoke all on all tables in schema public from public;
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Reference: [Roles and Privileges](https://supabase.com/blog/postgres-roles-and-privileges)
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Enable Row Level Security for Multi-Tenant Data
|
|
3
|
+
impact: CRITICAL
|
|
4
|
+
impactDescription: Database-enforced tenant isolation, prevent data leaks
|
|
5
|
+
tags: rls, row-level-security, multi-tenant, security
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Enable Row Level Security for Multi-Tenant Data
|
|
9
|
+
|
|
10
|
+
Row Level Security (RLS) enforces data access at the database level, ensuring users only see their own data.
|
|
11
|
+
|
|
12
|
+
**Incorrect (application-level filtering only):**
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
-- Relying only on application to filter
|
|
16
|
+
select * from orders where user_id = $current_user_id;
|
|
17
|
+
|
|
18
|
+
-- Bug or bypass means all data is exposed!
|
|
19
|
+
select * from orders; -- Returns ALL orders
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Correct (database-enforced RLS):**
|
|
23
|
+
|
|
24
|
+
```sql
|
|
25
|
+
-- Enable RLS on the table
|
|
26
|
+
alter table orders enable row level security;
|
|
27
|
+
|
|
28
|
+
-- Create policy for users to see only their orders
|
|
29
|
+
create policy orders_user_policy on orders
|
|
30
|
+
for all
|
|
31
|
+
using (user_id = current_setting('app.current_user_id')::bigint);
|
|
32
|
+
|
|
33
|
+
-- Force RLS even for table owners
|
|
34
|
+
alter table orders force row level security;
|
|
35
|
+
|
|
36
|
+
-- Set user context and query
|
|
37
|
+
set app.current_user_id = '123';
|
|
38
|
+
select * from orders; -- Only returns orders for user 123
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Policy for authenticated role:
|
|
42
|
+
|
|
43
|
+
```sql
|
|
44
|
+
create policy orders_user_policy on orders
|
|
45
|
+
for all
|
|
46
|
+
to authenticated
|
|
47
|
+
using (user_id = auth.uid());
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Reference: [Row Level Security](https://supabase.com/docs/guides/database/postgres/row-level-security)
|