Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alter owner of a function in postgres

I am writing a script to alter all functions of postgres(changing owner of each function). I am able to list down all the function names using postgres query but not able to list parameters for each of those functions.

My problem will be resolved if I get solution for any of the below mentioned problems:

  1. Is there any way to list down parameters' data type in each of the function.
  2. Do we have any approach to alter functions where instead of passing parameter type can I send some wild card. For Example can I write ALTER FUNCTION schemaname.func(text) OWNER TO 'newowner' as : ALTER FUNCTION schemaname.func(*) OWNER TO 'newowner'.
like image 890
user1728310 Avatar asked Apr 02 '14 06:04

user1728310


People also ask

How do I modify a function in PostgreSQL?

To start the function editor, navigate to the desired function in the schema browser, select the 'Source Code' tab and click on the 'Edit In Function Editor' button. Alternatively, you can right-click on the function in the schema browser and select 'Edit In Function Editor'.

What is alter function in SQL?

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER TABLE statement is also used to add and drop various constraints on an existing table.

What is the difference between function and stored procedure in PostgreSQL?

A procedure is a database object similar to a function. The key differences are: Procedures are defined with the CREATE PROCEDURE command, not CREATE FUNCTION . Procedures do not return a function value; hence CREATE PROCEDURE lacks a RETURNS clause.


2 Answers

Is there any way to list down parameters' data type in each of the function.

Yes, use the pg_get_function_identity_arguments() function:

The following will create a SQL script to alter all functions from the someschema schema:

select 'alter function '||nsp.nspname||'.'||p.proname||'('||pg_get_function_identity_arguments(p.oid)||') owner to newowner;'
from pg_proc p
  join pg_namespace nsp ON p.pronamespace = nsp.oid
where nsp.nspname = 'someschema';

You can spool the output of that into a file and then run that generated script.

If you have function names that would require quoting, you probably need to use quote_ident to concatenate the function names.

You can wrap all that into a function and use dynamic SQL to make life easier if you need this on a regular basis.

like image 75
a_horse_with_no_name Avatar answered Oct 19 '22 04:10

a_horse_with_no_name


I had the same problem but pg_get_function_identity_arguments(p.oid) didn't work for me. So I replaced that function with oidvectortypes(p.proargtypes).

select 'alter function '||nsp.nspname||'.'||p.proname||'('||oidvectortypes(p.proargtypes)||') owner to newowner;'
from pg_proc p 
join pg_namespace nsp ON p.pronamespace = nsp.oid
where nsp.nspname = 'someschema';
like image 1
Stefano Perduno Avatar answered Oct 19 '22 04:10

Stefano Perduno