Re: Re: Changing a schema's name with function1 calling function2 - Mailing list pgsql-general

From Wilma Wantren
Subject Re: Re: Changing a schema's name with function1 calling function2
Date
Msg-id 4cb2e7242b54e84f9d0c161cc56f38e4@mail.eclipso.de
Whole thread Raw
In response to Re: Changing a schema's name with function1 calling function2  (Adrian Klaver <adrian.klaver@aklaver.com>)
Responses Re: Changing a schema's name with function1 calling function2
List pgsql-general
Thank you for your response!
No, this does not help me. I will try to explain better what I mean. Our customers use our program with a Postgres
database,user and schema, which have the same name. The customer chooses the name. Let's say one customer calls the
database,user and schema 'my_things'.  
I want to create scripts for the customers with which they can export the schema and import it into another database
witha new schema name, let's say one customer wants to import it as 'my_things1' (this schema belongs to database
'my_things1').
The export script calls pg_dump to export schema 'my_things'.
The import script calls pg_restore to restore schema 'my_things' in database 'my_things1' and then calls psql to change
theschema name to 'my_things1'. 
Now there is function1 which its search_path set to 'my_things'. Because the search_path is still set to 'my_things'
afterrenaming the schema the script must now call psql to change the function's search_path to 'my_things1'.  
This is not just one line more in the import script.
It is a fact that I must know - if I did not know about function1's search_path then there would be an error in the
schemaafter renaming the schema.  
And imagine if one day a colleague of mine implements a new function which needs a search_path but the colleague
forgetsto adjust the import script then again there is an error in the schema after renaming the schema.  
Therefore it would be great if there was a variable which I could set in a search_path (like the variable "$user")
whichdenotes the function's schema and which is only evaluated when the function is executed, i.e. the variable would
bethe value of the function's search_path in the function's meta data. This variable, e.g. "$function_schema" would
stilldenote the correct schema after renaming the schema. 

--- Ursprüngliche Nachricht ---
Von: Adrian Klaver <adrian.klaver@aklaver.com>
Datum: 21.12.2023 17:30:02
An: Wilma Wantren <wilma.wantren@eclipso.de>,  pgsql-general@lists.postgresql.org
Betreff: Re: Changing a schema's name with function1 calling function2

On 12/21/23 05:47, Wilma Wantren wrote:
> Now as text mail, sorry.
> I accidentally posted the following to the bugs mailing list first.
Sorry for this, now as a question here.
> The company I work for used to only use Oracle databases for their program
and now also supports PostgreSQL.
> With Postgres, we create a database, a user and a schema, which all
have the same name. The name is chosen by the customers.
> Sometimes, the customers would like to export the data and definitions
and import them under a new name.
> This works well by using the -O (no owner) option for pg_dump/pg_restore
and only exporting and importing the schema. After the import the schema
is renamed. So far so simple and easy!
> But there is one problem: we have a database function function1, which
calls another function function2. To ensure that the function call is safe,
we set a search_path for function1.
> Both functions are created in the main schema (the one that is named
by the customer), the search_path is therefore set to this schema:
> ALTER FUNCTION function1 SET SEARCH_PATH TO <the schema name that
the customer has chosen>
> Since the search_path of the function is not renamed when the schema
is renamed, I need to know that there is such a search_path, which I then
manually change to the new schema name.
> Would it be possible that there is a variable that designates the schema
in which a function is located? Like this, for example:
> ALTER FUNCTION function1 SET SEARCH_PATH TO $FUNCTION_SCHEMA;
> Since $FUNCTION_SCHEMA would change to denote the new schema when renaming
the schema I wouldn't have to change the search_path manually.

From

https://www.postgresql.org/docs/current/sql-alterfunction.html

"
configuration_parameter
value

     Add or change the assignment to be made to a configuration
parameter when the function is called. If value is DEFAULT or,
equivalently, RESET is used, the function-local setting is removed, so
that the function executes with the value present in its environment.
Use RESET ALL to clear all function-local settings. SET FROM CURRENT
saves the value of the parameter that is current when ALTER FUNCTION is

executed as the value to be applied when the function is entered.

     See SET and Chapter 20 for more information about allowed parameter

names and values.
"

Not sure if that would meet your requirements.

I could see doing in a session:

SET search_path = 'main_schema';

ALTER FUNCTION function SET search_path FROM CURRENT;

> ________________________________________________________
> Your E-Mail. Your Cloud. Your Office. eclipso Mail & Cloud. https://www.eclipso.de

>
>
>
>

--
Adrian Klaver
adrian.klaver@aklaver.com



________________________________________________________
Your E-Mail. Your Cloud. Your Office. eclipso Mail & Cloud. https://www.eclipso.de





pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: Building Extension on Linux fails with relocation error
Next
From: Adrian Klaver
Date:
Subject: Re: Changing a schema's name with function1 calling function2