plpgsql

Elegant way of handling PostgreSQL exceptions?

天大地大妈咪最大 提交于 2019-11-29 16:52:38
问题 In PostgreSQL, I would like to create a safe-wrapping mechanism which returns empty result if an exception occurs. Consider the following: SELECT * FROM myschema.mytable; I could do the safe-wrapping in the client application: try { result = execute_query('SELECT value FROM myschema.mytable').fetchall(); } catch(pg_exception) { result = [] } But could I do such a thing in SQL directly? I would like to make the following code work, but it seems like it should by put into DO $$ ... $$ block and

Return rows from a PL/pgSQL function

大憨熊 提交于 2019-11-29 16:39:01
I have a procedure in PostgreSQL: CREATE OR REPLACE FUNCTION get_geom_difference() RETURNS void AS $$ BEGIN SELECT filedata.num,st_area(ST_Difference(ST_TRANSFORM(filedata.the_geom,70066),filedata_temp.the_geom)) FROM filedata, filedata_temp Where filedata.num=filedata_temp.num end; $$ LANGUAGE 'plpgsql' I call it in Java and want to get result of this procedure. How to change this procedure to make it possible get a result? And how to work with it in JDBC? Now I use this: Integer fileId; Class.forName("org.postgresql.Driver"); Connection connect= null; connect = DriverManager.getConnection(

Get values from varying columns in a generic trigger

前提是你 提交于 2019-11-29 16:34:40
I am new to PostgreSQL and found a trigger which serves my purpose completely except for one little thing. The trigger is quite generic and runs across different tables and logs different field changes. I found here . What I now need to do is test for a specific field which changes as the tables change on which the trigger fires. I thought of using substr as all the column will have the same name format e.g. XXX_cust_no but the XXX can change to 2 or 4 characters. I need to log the value in the XXX_cust_no field with every record that is written to the history_ / audit table. Using a bunch of

Passing multiple values in single parameter

有些话、适合烂在心里 提交于 2019-11-29 16:30:45
Let's say I have this function: CREATE OR REPLACE FUNCTION test_function(character varaying) RETURNS integer AS $BODY$ DECLARE some_integer integer; begin Select column2 from test_table where column1 in ($1) into some_integer; end; Return some_integer; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; And I want to call it like this: Select * from test_function ('data1', 'data2','data3'); Of course, it cannot be done this way, because Postgres tries to find function with this name and three parameter which doesn't exists. I tried to put quotes around commas but in that case parameter is interpreted

Optional argument in PL/pgSQL function

依然范特西╮ 提交于 2019-11-29 16:05:44
问题 I am trying to write a PL/pgSQL function with optional arguments. It performs a query based on a filtered set of records (if specified), otherwise performs a query on the entire data set in a table. For example (PSEUDO CODE) : CREATE OR REPLACE FUNCTION foofunc(param1 integer, param2 date, param2 date, optional_list_of_ids=[]) RETURNS SETOF RECORD AS $$ IF len(optional_list_of_ids) > 0 THEN RETURN QUERY (SELECT * from foobar where f1=param1 AND f2=param2 AND id in optional_list_of_ids); ELSE

Update a table with a trigger after update

╄→尐↘猪︶ㄣ 提交于 2019-11-29 16:01:10
I have two tables batch (batch_id,start_date,end_date,batch_strength,is_locked) sem (user_id,is_active,no_of_days) I have executed the trigger procedure given below then update the table using query CREATE OR REPLACE FUNCTION em_batch_update() RETURNS trigger AS $em_sem_batch$ BEGIN UPDATE batch set is_locked='TRUE' where (start_date + (select no_of_days from sem WHERE is_active='TRUE' and user_id='OSEM') ) <= current_date; return NEW; END; $em_sem_batch$ LANGUAGE plpgsql; CREATE TRIGGER em_sem_batch BEFORE UPDATE ON batch FOR EACH ROW EXECUTE PROCEDURE em_batch_update(); update em_batch set

Return Table Type from A function in PostgreSQL

你说的曾经没有我的故事 提交于 2019-11-29 15:50:31
I have a function from which has a return type as TABLE, and I want to get certain columns from my table into that RETURN TABLE type for my functionality. When I execute the function, it gives no error but returns no records although it should return some records based on the condition that I have. Below is the code that I have written, can someone let me know where have I gone wrong? CREATE OR REPLACE FUNCTION ccdb.fn_email_details_auto() RETURNS TABLE (code integer, area smallint, action smallint, flag smallint, ucount integer, view_cnt integer) AS $BODY$ DECLARE sec_col refcursor; cnt

How to execute a string result of a stored procedure in postgres

白昼怎懂夜的黑 提交于 2019-11-29 15:38:07
I have created the following stored procedure, which basically receives a name of table, and a prefix. The function then finds all columns that share this prefix and returns as an output a 'select' query command ('myoneliner'). as follows: CREATE OR REPLACE FUNCTION mytext (mytable text, myprefix text) RETURNS text AS $myoneliner$ declare myoneliner text; BEGIN SELECT 'SELECT ' || substr(cols,2,length(cols)-2) ||' FROM '||mytable INTO myoneliner FROM ( SELECT array( SELECT DISTINCT quote_ident(column_name::text) FROM information_schema.columns WHERE table_name = mytable AND column_name LIKE

Referring to session variables (\\set var='value') from PL/PGSQL

断了今生、忘了曾经 提交于 2019-11-29 15:28:16
I can pass variables into PostgreSQL using psql --variable="var='value'" <<<'SELECT :var' ...and refer to them as, in this case, :var in SQL queries passed to psql on stdin. However, this doesn't work from code using PL/PGSQL: psql --variable=var="'value'" <<'EOF' DO $$ BEGIN SELECT :var; END; $$ EOF ...yielding the error: ERROR: syntax error at or near ":" How can this be resolved? You cannot to use a psql variables inside plpgsql code directly. The symbol substitution is blocked inside strings: postgres=> select :'xx'; ?column? ---------- AHOJ (1 row) postgres=> select ' :xx '; ?column? ----

How to copy structure of one table to another with foreign key constraints in psql?

廉价感情. 提交于 2019-11-29 13:59:59
问题 Foreign key constraints are not copied when using create table table_name ( like source_table INCLUDING ALL)' in Postgres. How can I create a copy of an existing table including all foreign keys. 回答1: There is no option to automatically create foreign keys in CREATE TABLE ... LIKE ... . For the documentation: LIKE source_table [ like_option ... ] Not-null constraints are always copied to the new table. CHECK constraints will be copied only if INCLUDING CONSTRAINTS is specified [...] Indexes,