plpgsql

BREAK statement in PL/pgSQL

此生再无相见时 提交于 2019-12-01 02:34:57
How to have the break statement in PostgreSQL? I have the structure like this: for() { for() { if(somecondition) break; } } As per my understanding it should only break the inner for loop? There is no BREAK in PL/pgSQL . EXIT terminates the loop. CONTINUE continues at the next iteration of the loop. You can attach a <<label>> to loops and add it as parameter to each of these commands. Then you terminate / continue the labeled loop. Else, it concerns the inner loop. RETURN exits from the function (so not applicable in a DO statement). All of this applies to procedural elements of PL/pgSQL, not

EXECUTE of SELECT … INTO is not implemented

血红的双手。 提交于 2019-12-01 02:09:18
问题 I am trying to run this function in PostrgeSQL: CREATE OR REPLACE FUNCTION create_partition_and_insert() RETURNS trigger AS $BODY$ DECLARE partition VARCHAR(25); _date text; BEGIN EXECUTE 'SELECT REPLACE(' || quote_literal(NEW.date) || ',''-'',''_'') into _date'; partition := TG_RELNAME || '_' || _date || ‘p’; IF NOT EXISTS(SELECT relname FROM pg_class WHERE relname=partition) THEN RAISE NOTICE 'A partition has been created %',partition; EXECUTE 'CREATE TABLE ' || partition || ' (check (date

plpgsql - using dynamic table name in declare statement

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 01:02:19
I'm trying to write plpgsql a function of the following form (note this is a simplified version): CREATE FUNCTION check_valid(tablename regclass) RETURNS boolean AS $$ DECLARE valid_row tablename%ROWTYPE; BEGIN EXECUTE format('SELECT * FROM %s', tablename) into valid_row; IF valid_row IS NULL THEN RETURN QUERY SELECT false; ELSIF valid_row.is_valid = false; RETURN QUERY SELECT false; ELSIF valid_row.hit_count > valid_row.hit_limit; RETURN QUERY SELECT false; ELSE RETURN QUERY SELECT true; END IF; END $$ LANGUAGE plpgsql; The part that's failing is the DECLARE line. How can I declare a type

Can a function detect the trigger event type?

血红的双手。 提交于 2019-12-01 00:57:47
I am using a function CREATE FUNCTION myfunc() RETURNS trigger AS $$ ... $$ LANGUAGE plpgsql; with a trigger, CREATE TRIGGER mycheck BEFORE INSERT OR UPDATE ON t FOR EACH ROW EXECUTE PROCEDURE myfunc(); My problem now is to express in the body of myfunc() a condition about events, a plpgsql like IF TRIGGER_EVENT_WAS_INSERT THEN ...doThis... END IF; How to express this condition? (see that I have "INSERT OR UPDATE" trigger event) I am using PostgreSQL v9.1. Erwin Brandstetter Yes, TG_OP . Per documentation: TG_OP Data type text; a string of INSERT , UPDATE , DELETE , or TRUNCATE telling for

Postgres function much slower when using input variables

跟風遠走 提交于 2019-12-01 00:20:44
I have a function in Postgres 8.3.5 that selects data from multiple tables and dumps the result in a single table: create or replace function test_function_2(startdate timestamp, enddate timestamp) returns void as $$ begin delete from cl_final_report; INSERT INTO cl_final_report SELECT b.batchkey AS batchnumber, pv.productkey, p.name AS productname, avg(r.value) AS avgchemlean, sum(r.auxvalue) AS totalweight, max(o.time) AS timecompleted FROM result r LEFT JOIN physicalvalue pv ON r.physicalvaluekey = pv.physicalvaluekey LEFT JOIN product p ON pv.productkey = p.productkey LEFT JOIN object o ON

PostgreSQL modifying fields dynamically in NEW record in a trigger function

余生颓废 提交于 2019-11-30 22:24:33
I have a user table with IDs and usernames (and other details) and several other tables referring to this table with various column names ( CONSTRAINT some_name FOREIGN KEY (columnname) REFERENCES "user" (userid) ). What I need to do is add the usernames to the referring tables (in preparation for dropping the whole user table). This is of course easily accomplished with a single ALTER TABLE and UPDATE , and keeping these up-to-date with triggers is also (fairly) easy. But it's the trigger function that is causing me some annoyance. I could have used individual functions for each table, but

Query plan caching with pl/pgsql

最后都变了- 提交于 2019-11-30 22:01:33
I am having troubles understanding how the query plan caching works for pl/pgsql. I want to built all-in-one queries with JOIN s and IF s, so I will have multiple and different query parameters, and I will be searching in more that one tables. At first I thought that using pl/pgsql will produce a different plan for each parameters combination, that is not the case, because I have more than one tables SQL commands that appear directly in a PL/pgSQL function must refer to the same tables and columns on every execution; that is, you cannot use a parameter as the name of a table or column in an

Variable containing the number of rows affected by previous DELETE? (in a function)

北慕城南 提交于 2019-11-30 21:52:04
问题 I have a function that is used as an INSERT trigger. This function deletes rows that would conflict with [the serial number in] the row being inserted. It works beautifully, so I'd really rather not debate the merits of the concept. DECLARE re1 feeds_item.shareurl%TYPE; BEGIN SELECT regexp_replace(NEW.shareurl, '/[^/]+(-[0-9]+\.html)$','/[^/]+\\1') INTO re1; RAISE NOTICE 'DELETEing rows from feeds_item where shareurl ~ ''%''', re1; DELETE FROM feeds_item where shareurl ~ re1; RETURN NEW; END;

Get the count of rows from a COPY command

与世无争的帅哥 提交于 2019-11-30 20:39:10
When copying data from a file, you get the count of rows in psql with the "command tag": db=# COPY t FROM '/var/lib/postgres/test.sql'; COPY 10 I need the number of rows and would like to avoid a redundant count() on the table. Is there a way to get this count from COPY directly in a PL/pgSQL function? As far as I know there is none, but maybe I am missing something? For PostgreSQL 9.2. But any option in any version would be of interest. Not in PG 9.2, but there is in PG 9.3 courtesy of Pavel (E 1.3.1.7): Allow PL/pgSQL to access the number of rows processed by COPY (Pavel Stehule) The command

Testing PostgreSQL functions that consume and return refcursor

牧云@^-^@ 提交于 2019-11-30 20:21:41
I want to test results of a Postgres function (changing the function is not a possibility). The function receives as arguments a REFCURSOR and several other things and returns the same RECURSOR. get_function_that_returns_cursor(ret, 4100, 'SOMETHING', 123465) Now I want to create a small test in Postgres to get the results of this FUNCTION. Something Like the code below (this is my approach but it is not working): DO $$ DECLARE ret REFCURSOR; row_to_read table_it_will_return%ROWTYPE ; BEGIN PERFORM get_function_that_returns_cursor(ret, 4100, 'SOMETHING', 123465); -- OR SELECT get_function_that