plpgsql

Saving the output of a dynamic query that uses refcursor into a table

丶灬走出姿态 提交于 2019-12-02 04:24:57
In continuation to a previous case , in which a dynamic SELECT query that uses refcursor was created and then was executed - I would like to ask the following: The desired output that we got from the indicated procedure was output into the DataOutput. I would like to find a way to store the data into a new table in the db. Instead of the straight forward command: CREATE TABLE mydaughtertable AS SELECT enrich_d_dkj_p_k27ac,enrich_lr_dkj_p_k27ac,enrich_r_dkj_p_k27ac FROM dkj_p_k27ac The idea is to run something like: CREATE TABLE mydaughtertable AS myresult('dkj_p_k27ac','enri') But this

How to pass NEW.* to EXECUTE in trigger function

北城以北 提交于 2019-12-02 04:20:16
I have a simple mission is inserting huge MD5 values into tables (partitioned table), and have created a trigger and also a trigger function to instead of INSERT operation. And in function I checked the first two characters of NEW.md5 to determine which table should be inserted. DECLARE tb text; BEGIN IF TG_OP = 'INSERT' THEN tb = 'samples_' || left(NEW.md5, 2); EXECUTE(format('INSERT INTO %s VALUES (%s);', tb, NEW.*)); <- WRONG END IF; RETURN NULL; END; The question is how to concat the NEW.* into the SQL statement? Erwin Brandstetter Best with the USING clause of EXECUTE : CREATE FUNCTION

PL/PgSQL calling a function inside a loop giving error

隐身守侯 提交于 2019-12-02 04:13:18
The code bellow is giving error on w_add_ax_extra(1, 'k', 'v') previously it was w_add_ax_extra(some_id, kv.k, kv.v) I changed it to k, v to reproduce the same error declare kv record; begin -- Lines skipped for kv in select * from (select (each(extras)).*) as f(k,v) loop raise notice 'key=%,value=%',kv.k,kv.v; w_add_ax_extra(1, 'k', 'v'); end loop; -- Lines Skipped end I am getting Syntax Error but could not understand what I am missing ERROR: syntax error at or near "w_add_ax_extra" LINE 1: w_add_ax_extra(1, 'k', 'v') However If I do dummy = w_add_ax_extra(1, 'k', 'v') it works. Yes this

Why can PL/pgSQL functions have side effect, while SQL functions can't?

。_饼干妹妹 提交于 2019-12-02 04:11:45
PostgreSQL document says: The entire body of a SQL function is parsed before any of it is executed. While a SQL function can contain commands that alter the system catalogs (e.g., CREATE TABLE ), the effects of such commands will not be visible during parse analysis of later commands in the function. Thus, for example, CREATE TABLE foo (...); INSERT INTO foo VALUES(...); will not work as desired if packaged up into a single SQL function , since foo won't exist yet when the INSERT command is parsed. It's recommended to use PL/pgSQL instead of a SQL function in this type of situation. Why "It's

Declare variable of composite type in PostgreSQL using %TYPE

核能气质少年 提交于 2019-12-02 03:41:13
问题 Question: How can I declare a variable of the same type a parameter in a stored function? The simple answer is use %TYPE , this works: CREATE OR REPLACE FUNCTION test_function_1(param1 text) RETURNS integer AS $BODY$ DECLARE myVariable param1%TYPE; BEGIN return 1; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; But the problem is when param1 is a composite type: CREATE TYPE comp_type as ( field1 text ) CREATE OR REPLACE FUNCTION test_function_2(param1 comp_type) RETURNS integer AS $BODY$

How can I write to a file on disk from PL/pgSQL?

大憨熊 提交于 2019-12-02 03:39:16
I would like to do the equivalent of c or php fopen() and fwrite(). I am not trying to dump a table to disk. I am trying to do some debug logging during development. You can RAISE NOTICE or DEBUG messages in a plpgsql function or a DO statement which are written to the DB log file. You can use plpythonu f.open(), f.write(), f.close() within a postgres function to write to a file. Language extension would need to be installed., https://www.postgresql.org/docs/8.3/static/plpython.html Working example from the mailing list. https://www.postgresql.org/message-id/flat/20041106125209.55697.qmail

Cancel previous operations in user defined function

别来无恙 提交于 2019-12-02 03:29:12
问题 Is it possible to cancel previous operations in a user defined function? For example: CREATE OR REPLACE FUNCTION transact_test () RETURNS BOOLEAN AS $$ BEGIN UPDATE table1 SET ... UPDATE table2 SET ... IF some_condition THEN --Here is possible to cancel all above operations? RETURN FALSE; END IF; RETURN TRUE; END; $$ LANGUAGE plpgsql; 回答1: Both answers so far are incorrect. If you try to start a transaction or use a SAVEPOINT inside a plpgsql function you get an error message like this: ERROR

Postgres Function End Loop and return Error

℡╲_俬逩灬. 提交于 2019-12-02 03:11:48
I have tried to create this function but the system is returning a "LOOP error" and I don't know how to return 3 variables at the same time. I've tried hard to figure this out but I didn't find an answer anywhere. CREATE OR REPLACE FUNCTION conta_relatos(fator_normativo integer, fator_determinativo integer) RETURNS integer AS $BODY$ DECLARE vinculos_encontrados RECORD; rel_pri INT; rel_sec INT; rel_ref INT; no_item INT; tipo_relato TEXT; BEGIN rel_pri := 0; rel_sec := 0; rel_ref := 0; FOR vinculos_encontrados IN SELECT * FROM "Vinculos" WHERE ("Vinculos"."Fator_Normativo" = Fator_Normativo AND

Speed up plpgsql that counts doc types in a loop?

只谈情不闲聊 提交于 2019-12-02 02:58:33
Is there a way to speed up our plpgsql function that counts certain types of docs all in one query which is executed in a loop? ALL in one query? validador := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 901); validador2 := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 902); validador3 := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 905); validador4 := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra

Dynamically execute query using the output of another query

こ雲淡風輕ζ 提交于 2019-12-02 02:02:41
I have a function called generate_table, that takes 2 input parameters ( rundate::date and branch::varchar ) Now I am trying to work on a second function, using PLPGSQL, that will get a list of all branches and the newest date for each branch and pass this as a parameter to the generate_table function. The query that I have is this: select max(rundate) as rundate, branch from t_index_of_imported_files group by branch and it results on this: rundate;branch 2014-03-13;branch1 2014-03-12;branch2 2014-03-10;branch3 2014-03-13;branch4 and what I need is that the function run something like this