- Newest
- Most votes
- Most comments
The issue you're experiencing with your PostgreSQL function in Aurora RDS on your UAT environment is indeed unusual and complex. Several factors could be contributing to this behavior:
-
Query Plan Caching: PostgreSQL caches query plans for performance reasons. The fact that the function works fine for the first five executions but hangs on the sixth suggests that there might be an issue with the cached plan. The optimizer might be choosing a suboptimal plan after multiple executions.
-
Statistics and Data Distribution: If the data distribution in your UAT environment is significantly different from other environments, it could lead to the query optimizer making poor decisions. This is especially relevant if your function involves complex queries or joins.
-
Resource Constraints: The db.r6g.large instance type has limited resources. If the function is memory-intensive, especially with the JSONB column, it's possible that you're hitting memory limits after multiple executions. This could explain why resetting the function parameters helps.
-
Function Volatility: The VOLATILE setting on a function tells PostgreSQL that the function can have side effects and its output can change even with the same inputs. This might be causing the optimizer to reevaluate the function more often than necessary.
-
Work_mem Setting: As you mentioned, the work_mem parameter could be relevant. If the function requires more memory than allocated, it could lead to poor performance or hanging.
To troubleshoot this issue:
-
Review the function's EXPLAIN ANALYZE output for both successful and failing executions to identify differences in query plans.
-
Monitor resource utilization during function execution, particularly memory usage.
-
Consider adjusting the function's volatility to STABLE if it doesn't modify the database and always returns the same output for the same input.
-
Analyze the data distribution in your UAT environment and ensure statistics are up to date.
-
Review and possibly increase the work_mem setting if memory constraints are an issue.
-
Consider using pg_stat_statements to gather more detailed information about the function's performance over time.
Remember, the fact that this only occurs in the UAT environment suggests that there might be environmental factors at play. Carefully compare the configurations and data between your environments to identify any discrepancies that could be causing this behavior.
Sources
Lock:tuple - Amazon Aurora
Aurora RDS slow performance | AWS re:Post
Handle overloaded Oracle functions in Aurora PostgreSQL-Compatible - AWS Prescriptive Guidance
Relevant content
- asked 2 years ago
- asked 4 years ago

The overall load on the RDS and the relevant metrics are all good from the database perspective. The usage stats shows that they have sufficient capacity.
We also had ran the maintenance tasks like REINDEX, VACCUM, ANALYZE on the DB and schemas, but this issue still exists.