Question

PostgreSQL 12.2 (Ubuntu 12.2-2.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0, 64-bit

Client is Data Grip and same behavior from my report server which uses the driver that comes with Jaspersoft

I am running the same function for reporting with different parameters multiple times under the same session. Also does the same thing using the same parameters.

These are being one run one after the other and not at the same time.

The result only has a few rows but does read from quite a few tables, no writes.

It is just table joins and selects no inputs or updates to the tables themselves (would like to be able to post query but can't for security reasons).

After I run the function a few times it starts to slow down and gets to an unacceptable level. For example one of the functions goes from 1 second to over 90+ seconds (that is where I stopped testing).

Troubleshooting:

I have gone to the server and terminated the session and after that it starts to run normally for a few runs.

The standard report does use a temp table but I have removed that for testing. I have run the following after it starts having the issue to try and fix the issue.

VACUUM all touched tables; -- I know this should not be required as there are no major changes to these tables but was trying pretty much anything. Language plpgsql.

SET SESSION AUTHORIZATION DEFAULT;
RESET ALL;
DEALLOCATE ALL;
CLOSE ALL;
UNLISTEN *;
SELECT pg_advisory_unlock_all();
DISCARD PLANS;
DISCARD SEQUENCES;
DISCARD TEMP;

There does not seem to be a good way to terminate idle connections without a separate application or script run through a job.

Further data point: when looking at the execution plans with auto_explain, I see plans with $1 in them from the sixth execution on.

Was it helpful?

Solution

Since the problem are bad generic plans (symptom: execution becomes slower from the sixth execution on) and you are using PostgreSQL v12, the solution is simple:

ALTER FUNCTION xy SET plan_cache_mode = force_custom_plan;
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top