psql variable to plpgsql?

Started by Thiemo Kellneralmost 8 years ago5 messagesgeneral
Jump to latest
#1Thiemo Kellner
thiemo@gelassene-pferde.biz

Hi all

Is there a way to pass the value of a psql variable into function
code? I create a schema with help of psql variable

\set SCHEMA_NAME LOGGER
create
schema :SCHEMA_NAME;

I would like to create a function that has the Schema hard coded like
declare
V_SCHEMA_NAME text := :SCHEMA_NAME;
begin
but as the plpgsql code is within quotes, it Fails.

Kind regards

Thiemo

--
Öffentlicher PGP-Schlüssel:
http://pgp.mit.edu/pks/lookup?op=get&search=0x8F70EFD2D972CBEF

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

#2Pavel Stehule
pavel.stehule@gmail.com
In reply to: Thiemo Kellner (#1)
Re: psql variable to plpgsql?

Hi

2018-04-11 8:51 GMT+02:00 Thiemo Kellner <thiemo@gelassene-pferde.biz>:

Hi all

Is there a way to pass the value of a psql variable into function code? I
create a schema with help of psql variable

\set SCHEMA_NAME LOGGER
create
schema :SCHEMA_NAME;

I would like to create a function that has the Schema hard coded like
declare
V_SCHEMA_NAME text := :SCHEMA_NAME;
begin
but as the plpgsql code is within quotes, it Fails.

No, there is not possible to read/write client side variables from server
side.

Regards

Pavel

Show quoted text

Kind regards

Thiemo

--
Öffentlicher PGP-Schlüssel: http://pgp.mit.edu/pks/lookup?
op=get&search=0x8F70EFD2D972CBEF

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

#3Thiemo Kellner, NHC Barhufpflege
thiemo.kellner@gelassene-pferde.biz
In reply to: Pavel Stehule (#2)
Re: psql variable to plpgsql?

Zitat von Pavel Stehule <pavel.stehule@gmail.com>:

No, there is not possible to read/write client side variables from server
side.

I did not mean that the variable be replaced by its value at execution
time but at Installation time.

--
Öffentlicher PGP-Schlüssel:
http://pgp.mit.edu/pks/lookup?op=get&amp;search=0x8F70EFD2D972CBEF

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

#4Pavel Stehule
pavel.stehule@gmail.com
In reply to: Thiemo Kellner, NHC Barhufpflege (#3)
Re: psql variable to plpgsql?

2018-04-11 11:36 GMT+02:00 Thiemo Kellner, NHC Barhufpflege <
thiemo.kellner@gelassene-pferde.biz>:

Zitat von Pavel Stehule <pavel.stehule@gmail.com>:

No, there is not possible to read/write client side variables from server

side.

I did not mean that the variable be replaced by its value at execution
time but at Installation time.

no. The :xxx is not evaluated inside string. The workaround is using GUC
variables and related functions. Can be used from psql and from plpgsql too.

https://stackoverflow.com/questions/13172524/passing-user-id-to-postgresql-triggers/13172964#13172964

Regards

Pavel

Show quoted text

--
Öffentlicher PGP-Schlüssel: http://pgp.mit.edu/pks/lookup?
op=get&search=0x8F70EFD2D972CBEF

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

#5Thiemo Kellner
thiemo@gelassene-pferde.biz
In reply to: Pavel Stehule (#4)
Re: psql variable to plpgsql?

Zitat von Pavel Stehule <pavel.stehule@gmail.com>:

no. The :xxx is not evaluated inside string. The workaround is using GUC
variables and related functions. Can be used from psql and from plpgsql too.

Hi Pavel, thanks for pointing this out. However, I implemented another
solution with dedicated PostgreSQL user where I do not need such
variables in literals.

--
Öffentlicher PGP-Schlüssel:
http://pgp.mit.edu/pks/lookup?op=get&amp;search=0x8F70EFD2D972CBEF

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.