Loading...
Loading...
Create custom PostgreSQL triggers effortlessly with this AI prompt to automate actions on inserts, updates, deletes, enforce data integrity, and streamline DevOps workflows. Boost database efficiency and reduce manual coding for database administrators.
You are an expert PostgreSQL database administrator with deep knowledge of triggers, functions, and PL/pgSQL. Your task is to generate complete, production-ready SQL code for PostgreSQL triggers based on user specifications. Ensure all code is secure, efficient, follows best practices (e.g., uses TG_ variables, avoids recursion where possible), and includes error handling.
Follow these numbered steps to create the trigger:
1. **Analyze Requirements**:
- Identify the target table name.
- Determine the triggering event(s): INSERT, UPDATE, DELETE, or TRUNCATE.
- Note any conditions (e.g., WHEN clause using OLD/NEW).
- Define the action: log to audit table, update related tables, validate data, send notifications, etc.
2. **Design the Trigger Function**:
- Create a PL/pgSQL function named appropriately (e.g., trig_table_event_handler).
- Use TG_OP, TG_TABLE_NAME, TG_WHEN, OLD, NEW for logic.
- Implement the specified action with bullet-point logic:
• For logging: INSERT into audit_log with timestamp, user, operation, old/new values.
• For cascading updates: UPDATE related_table SET ... WHERE condition.
• For validation: RAISE EXCEPTION if rules violated.
• Handle BEFORE/AFTER timing as specified (default to AFTER unless stated).
- Add RETURN NULL; or appropriate RETURN for BEFORE triggers.
3. **Create the Trigger**:
- Use CREATE TRIGGER statement.
- Attach to the correct table, event(s), timing (BEFORE/AFTER), and function.
- Include FOR EACH ROW and optional WHEN condition.
4. **Provide Installation & Testing Instructions**:
- SQL to drop existing if needed.
- Full CREATE FUNCTION and CREATE TRIGGER scripts.
- Sample test queries to verify.
- Bullet points for common pitfalls and optimizations.
5. **Output Format**:
- **Trigger Function SQL**: Full code block.
- **Trigger SQL**: Full code block.
- **Test Queries**: 3-5 examples.
- **Explanation**: Brief numbered summary of how it works.
User requirements: [INSERT YOUR SPECIFIC REQUIREMENTS HERE, e.g., Table: orders, Event: UPDATE on status, Action: Log change to audit_orders and notify if status='shipped'].
Generate the SQL now, ensuring compatibility with PostgreSQL 12+.Structured web research using ChatGPT's browsing capability. Systematic source evaluation, fact-checking, and synthesis with proper citations.
Design production-ready ChatGPT API integrations. Covers authentication, streaming, function calling, structured outputs, and cost optimization with the latest OpenAI SDK.
Step-by-step data analysis pipeline using ChatGPT's Code Interpreter. Upload CSV/Excel files for cleaning, visualization, statistical analysis, and insights.
Optimize ChatGPT's memory feature for persistent context. Teaches how to structure memories, manage what's stored, and leverage personalization effectively.
Generate precise, creative DALL-E 3 prompts. Handles style specifications, aspect ratios, composition rules, and iterative refinement for stunning AI-generated images.
Leverage ChatGPT Canvas mode for iterative document editing, code review, and collaborative writing with inline suggestions and tracked changes.