Regenerate all MDC rules with fresh Exa data and December 2025 best practices

- Refreshed all 241 library rules with current best practices
- Updated Exa search results for all libraries with 2025 content
- Fixed --regenerate-all flag logic to properly process all libraries
- Removed duplicate astro entry in rules.json (merged tags)
- Added missing docker.mdc and vim.mdc files
- All rules now include modern patterns and anti-patterns
- Updated with framework-specific recommendations for 2025

Technical improvements:
- Fixed retry_failed_only logic in generate_mdc_files.py
- Improved progress tracking for regeneration scenarios
- Ensured proper handling of --refresh-exa flag
This commit is contained in:
sanjeed5
2025-12-17 22:40:28 +05:30
parent 331fb6ffac
commit 8fbf269085
244 changed files with 66701 additions and 52708 deletions
+343 -89
View File
@@ -1,107 +1,361 @@
---
description: Enforces PostgreSQL coding standards, best practices, and performance optimization techniques to ensure maintainable, efficient, and secure database interactions. This rule covers code formatting, data integrity, security, and performance considerations.
globs: *.sql,*.plpgsql,*.c,*.h
description: This guide defines the definitive best practices for writing clean, performant, and maintainable PostgreSQL SQL, focusing on modern conventions and avoiding common pitfalls.
globs: **/*
---
- **Code Formatting and Comments:**
- Maintain consistent code formatting using a tool like `pgformatter` or similar.
- Use clear and concise comments to explain complex logic and intentions. Update comments regularly to avoid confusion.
- Use inline comments sparingly; prefer block comments for detailed explanations.
- Write comments in plain, easy-to-follow English.
- Add a space after line comments (`-- a comment`); do not add a space for commented-out code (`--raise notice`).
- Keep comments up-to-date; incorrect comments are worse than no comments.
# PostgreSQL Best Practices
- **Naming Conventions:**
- Use `snake_case` for identifiers (e.g., `user_id`, `customer_name`).
- Use plural nouns for table names (e.g., `customers`, `products`).
- Use consistent naming conventions for functions, procedures, and triggers.
- Choose descriptive and meaningful names for all database objects.
This document outlines the mandatory best practices for all PostgreSQL SQL development. Adherence ensures consistency, readability, performance, and maintainability across our codebase.
- **Data Integrity and Data Types:**
- Use appropriate data types for columns to ensure data integrity (e.g., `INTEGER`, `VARCHAR`, `TIMESTAMP`).
- Use constraints (e.g., `NOT NULL`, `UNIQUE`, `CHECK`, `FOREIGN KEY`) to enforce data integrity.
- Define primary keys for all tables.
- Use foreign keys to establish relationships between tables.
- Utilize domains to enforce data type constraints reusable across multiple columns.
## 1. Code Organization and Structure
- **SQL Injection Prevention:**
- Always use parameterized queries or prepared statements to prevent SQL injection attacks.
- Sanitize user inputs before using them in SQL queries.
- Avoid constructing SQL queries by concatenating strings directly.
- Use the `quote_literal()` and `quote_ident()` functions to properly escape strings and identifiers.
### 1.1. Naming Conventions
Always use `snake_case` for all database identifiers (tables, columns, functions, schemas). SQL keywords must be lowercase. Keep names descriptive but concise.
- **Transaction Management:**
- Use explicit transactions to ensure data consistency and atomicity.
- Start transactions with `BEGIN;` and end them with `COMMIT;` or `ROLLBACK;`.
- Handle transaction errors properly to prevent data corruption.
- Use savepoints to allow partial rollbacks within a transaction.
❌ **BAD**:
```sql
CREATE TABLE UserData (
UserID INT PRIMARY KEY,
UserName VARCHAR(255)
);
- **Indexing:**
- Create indexes on columns frequently used in `WHERE` clauses and `JOIN` conditions.
- Avoid over-indexing, as it can slow down write operations.
- Consider using partial indexes for specific query patterns.
- Use appropriate index types (e.g., `B-tree`, `Hash`, `GIN`, `GiST`) based on the data and query requirements.
- Regularly analyze and maintain indexes using `ANALYZE` and `VACUUM`.
SELECT UserID, UserName FROM UserData;
```
- **Query Optimization:**
- Use `EXPLAIN ANALYZE` to analyze query execution plans and identify performance bottlenecks.
- Avoid using `SELECT *` and specify only the necessary columns.
- Use `JOIN` operations instead of subqueries where possible.
- Optimize `WHERE` clauses to reduce the number of rows processed.
- Consider using materialized views for frequently executed, complex queries.
- Rewrite slow performing queries with more efficient alternatives.
✅ **GOOD**:
```sql
create table user_data (
user_id bigint generated always as identity primary key,
user_name text not null
);
- **PL/pgSQL Best Practices:**
- Keep PL/pgSQL functions and procedures short and focused.
- Use exception handling to gracefully handle errors.
- Use `RAISE NOTICE`, `RAISE WARNING`, and `RAISE EXCEPTION` for logging and error reporting.
- Avoid using cursors unless absolutely necessary; prefer set-based operations.
- Use the `STRICT` attribute to ensure that a function returns a value.
select user_id, user_name from user_data;
```
- **Security Best Practices:**
- Grant the least privileges necessary to database users.
- Use roles to manage permissions.
- Regularly audit database activity.
- Encrypt sensitive data at rest and in transit.
- Use the `SECURITY DEFINER` attribute for functions that require elevated privileges.
- Configure appropriate authentication mechanisms (e.g., `SCRAM-SHA-256`).
### 1.2. Formatting
Structure queries for maximum readability. Root keywords (`SELECT`, `FROM`, `WHERE`) belong on their own line, with arguments indented. Use explicit `AS` for all aliases.
- **Connection Management:**
- Use connection pooling to reduce the overhead of establishing new connections.
- Close connections when they are no longer needed.
- Configure connection timeouts to prevent idle connections from consuming resources.
❌ **BAD**:
```sql
SELECT t.client_id, DATE(t.created_at) day FROM telemetry t, users u WHERE t.user_id = u.id AND t.submission_date > '2019-07-01' GROUP BY 1, 2;
```
- **Backup and Recovery:**
- Implement a robust backup and recovery strategy.
- Regularly back up the database.
- Test the recovery process to ensure it works as expected.
- Consider using replication for high availability.
✅ **GOOD**:
```sql
select
t.client_id as client_id,
date(t.created_at) as day
from
telemetry as t
inner join
users as u
on t.user_id = u.id
where
t.submission_date > '2019-07-01'
and t.sample_id = '10'
group by
t.client_id,
day;
```
- **Code Organization and Structure:**
- Organize database objects into schemas based on functionality or application modules.
- Use version control for database scripts and migrations.
- Follow a consistent directory structure for database-related files.
### 1.3. Comments
Use block comments (`/* ... */`) for multi-line descriptions and line comments (`--`) for single-line notes.
- **Common Pitfalls and Gotchas:**
- Avoid using reserved keywords as identifiers.
- Be aware of the limitations of data types (e.g., maximum length of `VARCHAR` columns).
- Handle null values carefully.
- Test database changes thoroughly before deploying them to production.
```sql
/*
This query retrieves active users and their recent orders.
It joins the users table with the orders table.
*/
select
u.user_id, -- Unique user identifier
u.user_name,
o.order_id,
o.order_date
from
users as u
inner join
orders as o
on u.user_id = o.user_id;
```
- **Tooling and Environment:**
- Use a code editor with SQL syntax highlighting and autocompletion.
- Use a database client tool for querying and managing the database (e.g., `pgAdmin`, `DBeaver`, `psql`).
- Use a version control system (e.g., Git) to manage database scripts and migrations.
- Use a CI/CD pipeline to automate database deployments.
## 2. Common Patterns and Anti-patterns
- **C Coding Standards (When Extending PostgreSQL):**
- Adhere to the C99 standard. Only use language features available in the C99 standard.
- Follow PostgreSQL's source code conventions ([PostgreSQL Documentation](https://www.postgresql.org/docs/current/source-conventions.html)).
- Manage memory carefully; avoid memory leaks.
- Properly handle errors and exceptions.
- Document code thoroughly.
### 2.1. Explicit JOINs
Always use explicit `INNER JOIN`, `LEFT JOIN`, `RIGHT JOIN`, or `FULL JOIN`. Never use implicit joins in the `FROM` clause.
- **Use Case specific optimizations:**
- Time series data: Consider timescaledb extension
- Geospatial data: Consider postgis extension
❌ **BAD**:
```sql
select u.user_name, o.order_date from users u, orders o where u.user_id = o.user_id;
```
@file ./rules/postgresql_security_rules.mdc
✅ **GOOD**:
```sql
select
u.user_name,
o.order_date
from
users as u
inner join
orders as o
on u.user_id = o.user_id;
```
### 2.2. Common Table Expressions (CTEs)
Prefer CTEs (`WITH` clauses) over nested subqueries for improved readability and modularity.
❌ **BAD**:
```sql
select
count(*)
from (
select
user_id
from
orders
where
order_date >= '2023-01-01'
group by
user_id
having
count(*) > 5
) as frequent_buyers;
```
✅ **GOOD**:
```sql
with frequent_buyers as (
select
user_id
from
orders
where
order_date >= '2023-01-01'
group by
user_id
having
count(*) > 5
)
select
count(*)
from
frequent_buyers;
```
### 2.3. Avoid `NOT IN`
Never use `NOT IN` due to its problematic behavior with `NULL` values. Prefer `NOT EXISTS` or `LEFT JOIN ... IS NULL`.
❌ **BAD**:
```sql
select user_name from users where user_id not in (select user_id from orders where status = 'cancelled');
```
✅ **GOOD` (NOT EXISTS)`**:
```sql
select
user_name
from
users as u
where not exists (
select 1 from orders as o where o.user_id = u.user_id and o.status = 'cancelled'
);
```
✅ **GOOD` (LEFT JOIN ... IS NULL)`**:
```sql
select
u.user_name
from
users as u
left join
orders as o
on u.user_id = o.user_id and o.status = 'cancelled'
where
o.user_id is null;
```
## 3. Performance Considerations
### 3.1. Explicit Column Selection
Always specify columns explicitly. Avoid `SELECT *` in production code. This improves performance, reduces network traffic, and prevents issues when schema changes.
❌ **BAD**:
```sql
select * from products;
```
✅ **GOOD**:
```sql
select
product_id,
product_name,
price,
stock_quantity
from
products;
```
### 3.2. Query Optimization
Use `EXPLAIN ANALYZE` to understand and optimize query plans. Focus on reducing sequential scans and improving index usage.
```sql
explain analyze
select
o.order_id,
c.customer_name
from
orders as o
inner join
customers as c
on o.customer_id = c.customer_id
where
o.order_date >= '2023-01-01'
order by
o.order_date desc
limit 100;
```
## 4. Common Pitfalls and Gotchas
### 4.1. Date/Time Storage
Store all timestamps as `timestamp with time zone` (`timestamptz`) and always in UTC. Convert to local time zones only at the application layer for display.
❌ **BAD**:
```sql
create table events (
event_id int,
event_time timestamp without time zone -- Prone to timezone issues
);
```
✅ **GOOD**:
```sql
create table events (
event_id bigint generated always as identity primary key,
event_time timestamptz default now() -- Always store in UTC
);
```
### 4.2. `BETWEEN` with Timestamps
Avoid `BETWEEN` for date/time ranges, especially when precision matters. It includes both start and end points, which can lead to off-by-one errors. Use explicit `>=` and `<` operators.
❌ **BAD**:
```sql
select * from orders where order_date between '2023-01-01' and '2023-01-31';
```
✅ **GOOD**:
```sql
select
*
from
orders
where
order_date >= '2023-01-01T00:00:00Z'
and order_date < '2023-02-01T00:00:00Z'; -- Correctly handles the entire month of January
```
### 4.3. Data Type Choices
* **IDs**: Use `bigint generated always as identity` for primary keys. Never use `serial` or `bigserial`.
* **Text**: Prefer `text` over `varchar(n)` unless there's a specific, strict length constraint. Never use `char(n)`.
* **Money**: Never use the `money` type. Use `numeric` or `decimal` with explicit precision (e.g., `numeric(19, 4)`).
❌ **BAD**:
```sql
create table products (
id serial primary key,
product_code char(10),
description varchar(255),
price money
);
```
✅ **GOOD**:
```sql
create table products (
product_id bigint generated always as identity primary key,
product_code varchar(10) not null, -- Use varchar for fixed-length codes
description text,
price numeric(19, 4) not null -- Explicit precision for currency
);
```
### 4.4. Avoid Legacy Constructs
Never use PostgreSQL `rules` or `table inheritance`. Use `triggers` for event-driven logic and native `table partitioning` for large tables.
## 5. Security Best Practices
### 5.1. Prepared Statements
Always use prepared statements (e.g., via parameterized queries in your application code) to prevent SQL injection vulnerabilities.
❌ **BAD` (Application Code)`**:
```python
cursor.execute(f"select * from users where user_name = '{user_input}';")
```
✅ **GOOD` (Application Code)`**:
```python
cursor.execute("select * from users where user_name = %s;", (user_input,))
```
### 5.2. Least Privilege
Grant database roles only the minimum necessary permissions. Avoid granting `ALL PRIVILEGES`.
```sql
-- Create a read-only role
create role app_reader nologin;
grant connect on database my_app_db to app_reader;
grant usage on schema public to app_reader;
grant select on all tables in schema public to app_reader;
alter default privileges in schema public grant select on tables to app_reader;
-- Create a read-write role
create role app_writer nologin;
grant app_reader to app_writer; -- Inherit read permissions
grant insert, update, delete on all tables in schema public to app_writer;
alter default privileges in schema public grant insert, update, delete on tables to app_writer;
```
## 6. Data Modeling
### 6.1. Foreign Key Naming
Name foreign key columns as `<referenced_table>_id` (singular form of the referenced table name).
❌ **BAD**:
```sql
create table orders (
order_id bigint generated always as identity primary key,
customerid bigint not null -- Inconsistent naming
);
```
✅ **GOOD**:
```sql
create table orders (
order_id bigint generated always as identity primary key,
customer_id bigint not null, -- Correct foreign key naming
constraint fk_customer foreign key (customer_id) references customers (customer_id)
);
```
### 6.2. Table Comments
Always add descriptive comments to tables and columns using `COMMENT ON`. This is crucial for documentation and understanding schema intent.
```sql
comment on table users is 'Stores information about application users.';
comment on column users.user_name is 'Full name of the user.';
```
## 7. Testing Approaches
### 7.1. Transactional Tests
Wrap database tests in transactions that are rolled back at the end. This ensures a clean state for each test run.
```sql
begin; -- Start transaction
-- Insert test data
insert into users (user_name) values ('Test User');
-- Run assertions
select count(*) from users where user_name = 'Test User'; -- Should be 1
rollback; -- Rollback all changes
```