PostgreSQL patterns for Python with psycopg and asyncpg — connection pooling,
99
99%
Does it follow best practices?
Impact
99%
1.15xAverage score across 5 eval scenarios
Passed
No known issues
A warehouse management company needs an inventory tracking API built with Python, FastAPI, and PostgreSQL. The API manages products across multiple warehouse locations and tracks stock movements.
The PostgreSQL database has these tables:
warehouses -- id, name, city, capacity_units, created_atproducts -- id, sku (unique), name, category, weight_grams, created_at, updated_atstock -- id, warehouse_id (FK), product_id (FK), quantity, min_quantity (reorder threshold), updated_atstock_movements -- id, product_id (FK), from_warehouse_id (FK, nullable for inbound), to_warehouse_id (FK, nullable for outbound), quantity, reason, created_atThe API needs these endpoints/functions:
Import product catalog -- Accept a CSV-like list of products (500-5000 items) with sku, name, category, weight. Products that already exist (by SKU) should be updated; new products should be created.
Transfer stock -- Move a quantity of a product from one warehouse to another. This must decrease stock in the source warehouse, increase stock in the destination warehouse, and record a stock movement -- all atomically. If the source warehouse doesn't have enough stock, the transfer must fail without making any changes.
Low stock report -- Find all stock entries where quantity is below min_quantity, showing the product name, warehouse name, current quantity, and deficit. The result should include products from all warehouses.
Warehouse inventory export -- Given a warehouse ID, stream all products and their quantities for that warehouse. Some warehouses have 50,000+ products, so the response must handle large result sets efficiently.
The application uses asyncpg with FastAPI.
Produce:
app/db.py -- Async database pool setupapp/inventory.py -- The four functions: import_catalog, transfer_stock, low_stock_report, export_warehouse_inventoryapp/main.py -- FastAPI application with pool lifecycle and route definitionsevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
skills
postgresql-python-best-practices