CYBERTEC Logo

PostgreSQL: Sharing data across function calls

05.2018 / Category: / Tags: |

Recently I did some PostgreSQL consulting in the Berlin area (Germany) when I stumbled over an interesting request: How can data be shared across function calls in PostgreSQL? I recalled some of the older features of PostgreSQL (15+ years old or so) to solve the issue. Here is how it works.

Stored procedures in PostgreSQL

As many of you might know, PostgreSQL allows you to write stored procedures in many different languages. Two of the more popular ones are Perl and Python, which have been around for quite some time. The cool thing is: Both languages offer a way to share variables across function calls. In Perl you can make use of the $_SHARED variable, which is always there.

Here is an example:

What the code does, is to assign a value to some_name and return the assigned value. Some other function can then make use of this data, which is stored inside your database connection. Here is an example:

This function will simply increment the value and return it. As you can see the code is pretty simple and easy to write.

Assigning shared variables

The following listing shows, how the code can be used. The first call will assign a value to the function while the second one will simply increment that value:

It is especially noteworthy here that the second column will already see the changes made by the first column, which is exactly what we want here.

Shared variables and transactions

When working with shared variables in PL/Perl or PL/Python you have to keep in mind that those changes will not be transactional as all the rest in PostgreSQL is. Even if you rollback a transaction you can observe that those values will stay incremented:

This behavior makes shared values actually a nice thing to have if you want to preserve data across transactions.

 


In order to receive regular updates on important changes in PostgreSQL, subscribe to our newsletter, or follow us on Twitter, Facebook, or LinkedIn.

0 0 votes
Article Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0BADC0DE
5 years ago

As one of the most popular languages for function bodies is PL/PgSQL, what about it?

Jakub Trmota
Jakub Trmota
5 years ago
Reply to  0BADC0DE

For PL/PgSQL we are using something like this:

CREATE FUNCTION globals_set_variable(p_variable character varying, p_value character varying)
RETURNS character varying AS
$BODY$
BEGIN
PERFORM set_config('globals.' || p_variable, p_value, false);
RETURN p_value;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

CREATE FUNCTION globals_get_variable(p_variable character varying)
RETURNS character varying AS
$BODY$
DECLARE
v_variable character varying;
BEGIN
SELECT NULLIF(current_setting('globals.' || p_variable), '') INTO v_variable;
RETURN v_variable;

EXCEPTION WHEN OTHERS THEN
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;

CYBERTEC Logo white
CYBERTEC PostgreSQL International GmbH
Römerstraße 19
2752 Wöllersdorf
Austria

+43 (0) 2622 93022-0
office@cybertec.at

Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2024
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle
    2
    0
    Would love your thoughts, please comment.x
    ()
    x
    linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram