plpgsql

Update multiple columns that start with a specific string

让人想犯罪 __ 提交于 2019-12-02 01:25:58
I am trying to update a bunch of columns in a DB for testing purposes of a feature. I have a table that is built with hibernate so all of the columns that are created for an embedded entity begin with the same name. I.e. contact_info_address_street1 , contact_info_address_street2 , etc. I am trying to figure out if there is a way to do something to the affect of: UPDATE table SET contact_info_address_* = null; If not, I know I can do it the long way, just looking for a way to help myself out in the future if I need to do this all over again for a different set of columns. There's no handy

Switching from FOR loops in plpgsql to set-based SQL commands

让人想犯罪 __ 提交于 2019-12-02 01:16:58
I've got quite heavy query with FOR loop to rewrite and would like to do it simpler, using more SQL instead of plpgsql constructions. The query looks like: FOR big_xml IN SELECT unnest(xpath('//TAG1', my_xml)) LOOP str_xml = unnest(xpath('/TAG2/TYPE/text()', big_xml)); FOR single_xml IN SELECT unnest(xpath('/TAG2/single', big_xml)) LOOP CASE str_xml::INT WHEN 1 THEN INSERT INTO tab1(id, xml) VALUES (1, single_xml); WHEN 2 THEN INSERT INTO tab2(id, xml) VALUES (1, single_xml); WHEN 3 [...] WHEN 11 [...] ELSE RAISE EXCEPTION 'something' END CASE; END LOOP; END LOOP; RETURN xmlelement(NAME "out",

Cancel previous operations in user defined function

拈花ヽ惹草 提交于 2019-12-01 23:01:31
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; 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: cannot begin/end transactions in PL/pgSQL HINT: Use a BEGIN block with an EXCEPTION clause instead.

Select a dynamic set of columns from a table and get the sum for each

旧街凉风 提交于 2019-12-01 22:34:44
Is it possible to do the following in Postgres: SELECT column_name FROM information_schema WHERE table_name = 'somereport' AND data_type = 'integer'; SELECT SUM(coulmn_name[0]),SUM(coulmn_name[1]) ,SUM(coulmn_name[3]) FROM somereport; In other words I need to select a group of columns from a table depending on certain criteria, and then sum each of those columns in the table. I know I can do this in a loop, so I can count each column independently, but obviously that requires a query for each column returned from the information schema query. Eg: FOR r IN select column_name from information

Setting a configuration parameter for functions implemented in PL/pgSQL

非 Y 不嫁゛ 提交于 2019-12-01 21:56:12
I've written a couple of functions in PL/pgSQL and I would like to control their behavior through some configuration entries, changeable at run time too (per session). Is it possible to define new custom-defined configuration entries in postgresql.conf ? If not, what's the work around? As my search results, I came across the part of documentation which says: 18.16. Customized Options This feature was designed to allow parameters not normally known to PostgreSQL to be added by add-on modules (such as procedural languages). This allows extension modules to be configured in the standard ways. If

Setting a configuration parameter for functions implemented in PL/pgSQL

限于喜欢 提交于 2019-12-01 21:14:02
问题 I've written a couple of functions in PL/pgSQL and I would like to control their behavior through some configuration entries, changeable at run time too (per session). Is it possible to define new custom-defined configuration entries in postgresql.conf ? If not, what's the work around? As my search results, I came across the part of documentation which says: 18.16. Customized Options This feature was designed to allow parameters not normally known to PostgreSQL to be added by add-on modules

Postgres function returning one record while I have many records?

别来无恙 提交于 2019-12-01 20:47:48
I have many records which my simple query returning but when i use function it just gives me first record, firstly i create my own data type using, CREATE TYPE my_type (usr_id integer , name varchar(30)); and my function is, CREATE OR REPLACE function test() returns my_type as $$ declare rd varchar := '21'; declare personphone varchar := NULL; declare result my_type; declare SQL VARCHAR(300):=null; DECLARE radiophone_clause text = ''; BEGIN IF rd IS NOT NULL then radiophone_clause = 'and pp.radio_phone = '|| quote_literal(rd); END IF; IF personphone IS NOT NULL then radiophone_clause =

Drop or create database from stored procedure in PostgreSQL

╄→尐↘猪︶ㄣ 提交于 2019-12-01 20:45:43
I have a function that looks like this: BEGIN DROP DATABASE IF EXISTS db_1; END; I'm getting the following error: ERROR: DROP DATABASE cannot be executed from a function or multi-command string. Is it not possible to drop a database from a stored procedure in PostgreSQL? I'm using plpgsql. Erwin Brandstetter The error message is just a s clear as the manual on this: DROP DATABASE cannot be executed inside a transaction block. A plgpsql function is surrounded by a transaction block automatically. The long and the short of it: you cannot do that - directly. Is there a particular reason you can't

UPDATE a whole row in PL/pgSQL

女生的网名这么多〃 提交于 2019-12-01 19:31:29
I have plpgsql function: CREATE OR REPLACE FUNCTION test() RETURNS VOID AS $$ DECLARE my_row my_table%ROWTYPE; BEGIN SELECT * INTO my_row FROM my_table WHERE id='1'; my_row.date := now(); END; $$ LANGUAGE plpgsql; I would like to know if it's possible to directly UPDATE my_row record. The only way I've found to do it now is: UPDATE my_table SET date=now() WHERE id='1'; Note this is only an example function, the real one is far more complex than this. I'm using PostgreSQL 9.2. UPDATE: Sorry for the confusion, what I wanted to say is: SELECT * INTO my_row FROM my_table INTO my_row WHERE id='1';

How to programmatically check if row is deletable?

…衆ロ難τιáo~ 提交于 2019-12-01 19:02:45
Say we have a PostgreSQL table like so: CREATE TABLE master ( id INT PRIMARY KEY, ... ); and many other tables referencing it with foreign keys: CREATE TABLE other ( id INT PRIMARY KEY, id_master INT NOT NULL, ... CONSTRAINT other_id_master_fkey FOREIGN KEY (id_master) REFERENCES master (id) ON DELETE RESTRICT ); Is there a way to check (from within trigger function) if a master row is deletable without actually trying to delete it? The obvious way is to do a SELECT on all referencing tables one by one, but I would like to know if there is an easier way. The reason I need this is that I have a