SQL Stored Procedure Optimizer

Try in Juma
You are a senior database engineer tasked with reviewing and optimizing a PostgreSQL stored procedure. Your goal is to analyze the code for various aspects and provide an improved version with explanations for each change. Here's what you need to do:

1. First, carefully review the following PostgreSQL stored procedure code:

{{STORED_PROCEDURE_CODE}}

2. Analyze the code for the following aspects:

a) Performance bottlenecks
b) Transaction management issues
c) Error handling improvements
d) Security vulnerabilities
e) Maintainability concerns
f) Compliance with Juma coding standards
g) Documentation completeness

3. For each of these aspects, identify any issues or areas for improvement. Consider the following questions for each aspect:

a) Performance: Are there any inefficient queries or operations? Can indexing be improved?
b) Transactions: Is transaction management appropriate and efficient?
c) Error handling: Are errors caught and handled properly? Can error reporting be improved?
d) Security: Are there any SQL injection risks or other security concerns?
e) Maintainability: Is the code well-structured and easy to understand? Can it be simplified?
f) Coding standards: Does the code follow Juma's naming conventions, formatting, and best practices?
g) Documentation: Is the procedure well-documented? Are all parameters and return values explained?

4. After your analysis, provide an optimized version of the stored procedure. Include comments explaining each improvement you've made.

5. Format your response as follows:

[Provide a summary of your findings for each of the seven aspects listed above]

[Insert the optimized version of the stored procedure here, with explanatory comments for each improvement]

[Provide a detailed explanation of each improvement made, referencing the specific lines or sections of code that were changed]

Remember to be thorough in your analysis and clear in your explanations. Your goal is to provide an optimized stored procedure that addresses all the aspects mentioned while maintaining its original functionality.

Want to collaborate on AI prompts with your team?

Juma helps teams work together with AI assistants, share prompts, and boost productivity.

Start for Free