Closed Kazmirchuk closed 5 hours ago
btw I'm not talking about huge amount of data here:
SELECT pg_size_pretty(table_bytes) FROM hypertable_detailed_size('tm_param');
=> 39 MB
I have managed to solve my specific problem by rewriting my function to pure SQL, but the general question of using PL/pgSQL in such context remains. Unfortunately, adding "parallel safe" made no difference.
@Kazmirchuk not sure if this indicates a real problem. The Linux OS does memory cleanup as and when needed.
@Kazmirchuk not sure if this indicates a real problem. The Linux OS does memory cleanup as and when needed.
PostgreSQL crashed every time. IMHO this is a real problem that does not occur when using standard PostgreSQL tables.
@Kazmirchuk thanks for the repro steps. I can confirm that there's a memory leak when plpgsql functions are involved in the INSERT queries. We will continue to investigate this.
When I dump the memory context I can see multiple entries for CurTransactionContext:
which is weird. And there's an additional entry every time the plpgsql function gets invoked for each tuple. So, basically a 8KB leak everytime which can quickly add up when a lot of function calls are involved.
TopTransactionContext: 8192 total in 1 blocks; 6944 free (3 chunks); 1248 used
CurTransactionContext: 8192 total in 1 blocks; 7928 free (1 chunks); 264 used
SPI Exec: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used
SPI Proc: 8192 total in 1 blocks; 7216 free (0 chunks); 976 used
PLpgSQL per-statement data: 8192 total in 1 blocks; 7928 free (0 chunks); 264 used
CurTransactionContext: 8192 total in 1 blocks; 7928 free (1 chunks); 264 used
CurTransactionContext: 8192 total in 1 blocks; 7928 free (1 chunks); 264 used
ExecutorState: 8192 total in 1 blocks; 7392 free (3 chunks); 800 used
Grand total: 65536 bytes in 8 blocks; 61192 free (9 chunks); 4344 used
this happens in SPI_finish
when we delete the SPI Proc
memory context. It has next and prevchild entries set up and then this CurTransactionContext
context which is there in the prevchild gets chained into the TopTransactionContext
leading to the leak.
many thanks for the quick fix! 👍
What type of bug is this?
Performance issue
What subsystems and features are affected?
Data ingestion, Query executor, Query planner
What happened?
A query of the form
performs badly and results in a very visible spike in RAM usage that might end up in Linux OOM killing Postgres.
The bug does not manifest, if:
htop screenshots when my system is idle:
and when the query is running:
TimescaleDB version affected
2.15.2
PostgreSQL version used
14.8
What operating system did you use?
Red Hat 8.7
What installation method did you use?
RPM
What platform did you run on?
On prem/Self-hosted
Relevant log output and stack trace
No response
How can we reproduce the bug?