plpgsql

Avoid multiple calls on same function when expanding composite result

陌路散爱 提交于 2019-12-11 02:54:39
问题 I have an SQL function retuning a composite result. CREATE TYPE result_t AS (a int, b int, c int, d numeric); CREATE OR REPLACE FUNCTION slow_function(int) RETURNS result_t AS $$ -- just some placeholder code to make it slow SELECT 0, 0, 0, ( SELECT sum(ln(i::numeric)) FROM generate_series(1, $1) i ) $$ LANGUAGE sql IMMUTABLE; When calling the function, I would like to have the parts of the composite type expanded into several columns. That works fine when I call: SELECT (slow_function(i)).*

Trigger to insert rows in remote database after deletion

允我心安 提交于 2019-12-11 02:36:41
问题 I have created a trigger that works like this: After deleting data from table flux_tresorerie_historique it insert this row in the table flux_tresorerie_historique that is located in another database archive I use dblink to insert data in the remote database, the problem is that the creation of the query is too hard especially that the table contain more than 20 columns, and I want to create similar functions for 10 other tables. Is there another rapid way to ensure this task? Here an example

Postgres plpgsql with PERFORM data-modifying CTE queries

此生再无相见时 提交于 2019-12-11 02:29:49
问题 I tried to simulate my problem in the code example below. In the code below, I am doing a delete from test2 in a procedure. This works great: However, in my case, this delete is part of a rather complex CTE with several updates and inserts (there are no selects so I add a dummy select 1 as main query). Let's simulate this as this: with my_cte as(delete from test2) select 1 Now, as we know, we have to use the perform keyword to execute this: perform (with my_cte as(delete from test2) select 1)

COPY csv using custom filename path

本秂侑毒 提交于 2019-12-11 02:25:48
问题 I'm getting some issues while trying to export a query to CSV using the COPY function. The COPY runs ok and exports the query successfully if not using custom filenames on the TO . The issue is related to add a "datestamp" (kinda) to the filename created. declare var1 varchar(25); DECLARE STATEMENT TEXT; select into var1 current_date -1; STATEMENT := 'COPY (SELECT * from myTable) To ''E'C:\\Exports\\export_'||var1||'.csv' ''With CSV'; EXECUTE STATEMENT; In this case, var1 gets a value like

cursor loop in PostgreSQL

柔情痞子 提交于 2019-12-11 02:24:07
问题 This below code is a cursor in PostgreSQL 9.0. I want to fetch my records by joining more than one table and i am getting JSON data from that join. So I want to loop those records and parse that json using query something like SELECT "Dump"->'activities-steps'->0->'value' as "steps" FROM "ActivitySessionDump" where "Id"=42594321345021288 then i have to get data from this query and insert to some other table like insert to table name (key,value); So i prepared one readonly cursor to achieve

Store XML explain plan in PostgreSQL database

北慕城南 提交于 2019-12-11 02:12:24
问题 How can I store XML explain plan (or any other format) in PostgreSQL database? Test data: explain (verbose, format xml) select 1 Table to store results: create table tmp.explain_plan_data (expp xml); My naive test failed: insert into tmp.explain_plan_data values (explain (verbose, format xml) select 1); It seems that explain cannot be used in any other select statement, the following statement does not work either: select * from (explain (verbose, format xml) select 1) a We are using

Postgresql: How to escape single quotes in Database trigger?

╄→尐↘猪︶ㄣ 提交于 2019-12-11 02:08:02
问题 I created a database trigger to store the row data in an auditing table. During the update operation, this trigger takes data from the main table and inserts it to a history table. (history table has columns: date, operation type say Update/Delete, actual row data) But the trigger fails in some cases because of the quoted text in input data. How can I escape the quoted text in my trigger? --My trigger CREATE OR REPLACE FUNCTION audit.if_modified() RETURNS TRIGGER AS $function$ DECLARE temp

Reduce bothering notices in plpgsql

淺唱寂寞╮ 提交于 2019-12-11 02:05:18
问题 I have a function which uses temporary table, that must be dropped if exists. drop table if exists t_xy; create temp table t_xy on commit drop as select ...; Subsequently I use this function in a view. The function is called many times while select is in progress. I like to use "raise notice" command because it is almost the only reliable way to report any variables in functions for debug purposes. The problem is I must search for them in huge amount of unwanted lines like: NOTICE: table "t

PostgreSQL: month := interval '30 days';

时间秒杀一切 提交于 2019-12-11 01:26:59
问题 Trying to delete records older than 1 month from 2 tables, where 1 references the "id" column in another: create or replace function quincytrack_clean() returns void as $BODY$ begin month := interval '30 days'; delete from hide_id where id in (select id from quincytrack where age(QDATETIME) > month); delete from quincytrack where age(QDATETIME) > month; end; $BODY$ language plpgsql; but this fails with: ERROR: syntax error at or near "month" LINE 1: month := interval '30 days' ^ QUERY: month

PostgreSQL GOTO like keyword to jump to a block

主宰稳场 提交于 2019-12-11 01:26:07
问题 I have a PostgreSQL function CREATE OR REPLACE FUNCTION increment(i integer) RETURNS integer AS $$ BEGIN IF i<0 THEN RETURN i + 1; ELSE GOTO label1; END IF <<label1>> RETURN null; END; $$ LANGUAGE plpgsql; In this function I have to GOTO to a label1, but GOTO keyword is not working, can u please help me in getting the way from which I am able to jump from a particular code to label. 回答1: workaround: <<label>> LOOP ... EXIT label WHEN i > 0; ... EXIT; END LOOP label; some; But I didn't use it