Skip to content
Advertisement

Postgres Function not working when I have a large result

I’m copying information from table 1(tmp_subtype) to table 2(subtype_user). I have a test table 1 with 15 registers. I run this function into postgres:

It works, but When I run this function with the real table 1, it is not working. The function finishes but nothing happend. The real table 1 has 1 million of registers.

Advertisement

Answer

Row-by-row processing with embedded counting is a recipe for slow and inefficient processing. Additionally your check for existence won’t work if the function is invoked from concurrent transactions. As far as I can tell you can replace the whole loop and cursor with a single INSERT statement:

I probable would not add an exception handler to begin with, so that the caller sees the complete exception.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement