plpgsql

To convert from Python arrays to PostgreSQL quickly?

旧时模样 提交于 2019-12-06 05:27:26
问题 This is a follow-up question to: How to cast to int array in PostgreSQL? I am thinking how to convert Python's datatype of array-array of signed integer into to int of PostgreSQL quickly: import numpy as np; # use any data format of Python here event = np.array([[1,2],[3,4]]); where [] should be replaced by {} and surrounded by ' if manually. In PostgreSQL, the following is accepted as the syntax of the datatype ... FOR EACH ROW EXECUTE PROCEDURE insaft_function('{{1,2},{3,4}}'); @JohnMee's

Give a user permission to ALTER a function

廉价感情. 提交于 2019-12-06 05:05:33
I try to ALTER a function with a new user and I get the error: ERROR: must be owner of function ACases ********** Error ********** ERROR: must be owner of function ACases SQL state: 42501 What permission do I have to give to a user so he can ALTER that function? The only way I found was to make the user the OWNER of the function. But if that is the case, only one user (owner) can ALTER the function. So how would I change the OWNER for all functions? CREATE OR REPLACE FUNCTION public."ACases"(caseid integer) RETURNS boolean AS $BODY$ DECLARE BEGIN RETURN FALSE; END; $BODY$ LANGUAGE plpgsql;

set-valued function called in context that cannot accept a set

怎甘沉沦 提交于 2019-12-06 04:24:27
问题 I am receiving the error: set-valued function called in context that cannot accept a set when executing this function at RETURN QUERY EXECUTE line: PLSQL $ cat lookup_email.pl CREATE OR REPLACE FUNCTION app.lookup_email(ident_id bigint,sess bigint,company_id bigint,email varchar) RETURNS SETOF RECORD as $$ DECLARE rec RECORD; comp_id bigint; server_session bigint; schema_name varchar; query varchar; BEGIN schema_name:='comp' || company_id; select app.session.session into server_session from

PL/pgSQL column name the same as variable

那年仲夏 提交于 2019-12-06 03:14:28
问题 I'm new to plpgsql and I'm trying to create function that will check if a certain value exists in table and if not will add a row. CREATE OR REPLACE FUNCTION hire( id_pracownika integer, imie character varying, nazwisko character varying, miasto character varying, pensja real) RETURNS TEXT AS $BODY$ DECLARE wynik TEXT; sprawdzenie INT; BEGIN sprawdzenie = id_pracownika; IF EXISTS (SELECT id_pracownika FROM pracownicy WHERE id_pracownika=sprawdzenie) THEN wynik = "JUZ ISTNIEJE"; RETURN wynik;

How to select all rows from refcursor returned by PL/pgSQL function?

跟風遠走 提交于 2019-12-06 02:46:22
问题 I have a function some_func() that returns refcursor : CREATE OR REPLACE FUNCTION some_func() RETURNS refcursor AS (...) I want to call this function from console and display the result set from the cursor returned by it. In Oracle I would write: SELECT * FROM TABLE(some_func()); What is the equivalent of that construction on PosgreSQL? 回答1: A refcursor is referred to by its name, either auto-generated or chosen by you. This page of the doc gives an example for each. To fetch results from a

PostgreSQL loops outside functions. Is that possible?

[亡魂溺海] 提交于 2019-12-06 01:45:55
问题 I'm making comparative about PostgreSQL vs. SQLServer for migrating purposes. Now I'm evaluating T-SQL vs. PL/pgSQL, the thing is that in T-SQL you can use loops or declare variables, for example: declare @counter int set @counter = 0 while @counter < 10 begin set @counter = @counter + 1 print 'The counter is ' + cast(@counter as char) end There is no need to put it inside a function or procedure. Can I do that in PostgreSQL? Searching on the web I found a negative answer doing it in MySQL

What does %% in PL/pgSQL mean?

旧时模样 提交于 2019-12-06 00:19:41
问题 I was reading over Instagrams sharding solution and I noticed the following line: SELECT nextval('insta5.table_id_seq') %% 1024 INTO seq_id; What does the %% in the SELECT line above do? I looked up PostgreSQL and the only thing I found was that %% is utilized when you want to use a literal percent character. CREATE OR REPLACE FUNCTION insta5.next_id(OUT result bigint) AS $$ DECLARE our_epoch bigint := 1314220021721; seq_id bigint; now_millis bigint; shard_id int := 5; BEGIN SELECT nextval(

Raising error in postgreSQL

无人久伴 提交于 2019-12-05 23:29:07
问题 CREATE OR REPLACE FUNCTION msgfailerror() RETURNS trigger AS ' BEGIN IF NEW.noces< new.first_column THEN RAISE EXCEPTION 'cannot have a negative salary'; END IF; return new; END' LANGUAGE plpgsql Trigger create trigger msgfail before insert on first for each row execute procedure msgfailerror() Giving error: syntax error at or near "cannot" LINE 5: RAISE EXCEPTION 'cannot have a negative ... I have almost one validation for each field of row. I want trigger to check all validations while

Handling EXCEPTION and return result from function

拈花ヽ惹草 提交于 2019-12-05 22:58:13
This is my code CREATE OR REPLACE FUNCTION test_excep (arg INTEGER) RETURNS INTEGER AS $$ DECLARE res INTEGER; BEGIN res := 100 / arg; BEGIN EXCEPTION WHEN division_by_zero THEN RETURN 999; END; RETURN res; END; $$ LANGUAGE plpgsql; That is, I need returned "999", if happened division by zero, but this: SELECT test_excep(0) returns error: division by zero CONTEXT: PL/pgSQL function test_excep(integer) line 4 at assignment What is wrong in my code? The EXCEPTION clause needs to be in the same block as the exception. For instance: CREATE OR REPLACE FUNCTION test_excep (arg integer) RETURNS

PostgreSQL 9.3 trigger function to insert into table with parameterized name

拟墨画扇 提交于 2019-12-05 21:11:56
I'm trying to dynamically partition log entries in Postgres. I have 53 child tables (1 for each week's worth of log entries), and would like to route INSERTs to a child table using a trigger. I run the function with INSERT INTO log5 VALUES (NEW.*) , and it works. I run the function with the EXECUTE statement instead, and it fails. Within the EXECUTE statement, it's recognizing NEW as a table name and not a variable passed to the trigger function. Any ideas on how to fix? Thanks! The error: QUERY: INSERT INTO log5 VALUES (NEW.*) CONTEXT: PL/pgSQL function log_roll_test() line 6 at EXECUTE