I am getting

"ERROR: out of shared memory"

presumably because my plpgsql function drops/creates a lot of temporary tables.

Searching for a solution to the issue, I found this answer that says:

So I guess one answer is that you will need enough locks to cope with all these tables being added/dropped throughout the transaction. Alternatively, you could try to reuse the temp tables between queries, simply truncate them to remove all the temp data

In order to avoid having too many looks, I am thinking about rewriting as many temporary table code using Common Table Expressions (CTEs).

My questions are:

Do the table expressions in CTEs use locks that count towards max_locks_per_transaction?

Will replacing temp tables with CTEs solve the max_locks_per_transaction problem?




1 Answer 1


A CTE is no table and won't need an entry in the lock table.

It is probably a good idea not to create and drop temporary tables all the time. Apart from the problem you are experiencing, it can lead to a bloated pg_attribute catalog table (all those added and deleted columns).

Mind that a CTE is only for the duration of a single table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.