How do I return the result of a RETURNING clause from a plpgsql function?

Joe Shaw

I would like to have a fairly generic function that takes an INSERT, UPDATE, or DELETE, which might contain a RETURNING clause, and return that result. A fairly contrived example might be something like this:

CREATE FUNCTION my_insert(sql_insert TEXT) RETURNS record AS $$
DECLARE
    result record;
BEGIN
    EXECUTE sql_insert INTO result;
    RETURN result;
END;
$$ LANGUAGE plpgsql;

SELECT my_insert('INSERT INTO foo VALUES (1) RETURNING some_column, another_column');

While this works ok, I don't think that record is the right type here, because RETURNING typically returns a table type. I'd like to return the exact same format from my function as RETURNING does, but I'm not sure how.

Joe Shaw

Returning record is fine, as long as the SELECT from the function defines the table structure:

SELECT * FROM my_insert('INSERT INTO foo VALUES (1) RETURNING some_column, another_column)
    AS f(some_column bigint, another_column timestamp);

If the return value from the my_insert function is always going to be of a certain type, a composite type is maybe a better way to go:

CREATE TYPE my_insert_result AS (
    some_column bigint,
    another_column timestamp
);

And then change the function to return that type:

CREATE FUNCTION my_insert(sql_insert TEXT) RETURNS my_insert_result AS $$
DECLARE
    result my_insert_result;
BEGIN
    EXECUTE sql_insert INTO result;
    RETURN result;
END;
$$ LANGUAGE plpgsql;

SELECT * FROM my_insert('INSERT INTO foo VALUES (1) RETURNING some_column, another_column');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I return a result from a dialog?

How do I return result from a spirngEventListener

How to exit from plpgsql function while returning query

how do I get a function to return the result of an onclick function?

How to return refcursor from plpgsql function in c#?

How to return an integer from a plpgsql function AND rollback every modifications?

Returning set of rows from plpgsql function.

How do I get result from SELECT clause after CREATE and INSERT clause?

How do I return value from this function

How do I return a &Path from a function?

How do I return a WeightedChoice from a function?

How do I return text from a function?

How do you unwrap a Result on Ok or return from the function on Err?

How can I yield notifications and return a result from a function? (Python)

how can i return the result from function in android

How do i return a result from a async task

How do I return an empty result from an IDbCommandInterceptor in Entity Framework?

How do I return the result from the addition of a and b, in a Long method?

How do I keep information in struct after returning from function?

Take in a Java ResultSet the return from a plpgsql function

plpgSQL return record type from FUNCTION

Return multiple rows from plpgsql function

How to return a collection from dynamic SQL having RETURNING clause

How to stop function from returning new result until x seconds is over, and return previous result if called too soon

how do I exit (return) from a function inside a another function

How do I get the result from this async function?

How do I serialize my result from my .each function?

How do I dereference the return value of a function returning a pointer without using a temporary variable?

How do I return the result of a recursive fetch?