Skip to content
Advertisement

Postgres function to return number of rows deleted per schema

I’m trying to adapt a Postgres stored procedure into a function in order to provide some feedback to the caller.

The procedure conditionally deletes rows in specific schemas, and I’d want the function to do the same, but also return the amount of rows that were deleted for each schema.

The original stored procedure is:

My current transform into a function is:

This is probably wrong in all kinds of ways. I’m definitely confused.

When executing this with SELECT * FROM testfun(), I get the following error:

So clearly I’m not properly assigning the row’s columns, but I’m not sure how.

I have found this question which seemed similar, but it’s bit complex for my understanding.

Advertisement

Answer

You can use GET DIAGNOSTICS after a DELETE statement to get the number of rows deleted:

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