plpgsql

BREAK statement in PL/pgSQL

心不动则不痛 提交于 2019-12-19 05:08:20
问题 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? 回答1: 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

BREAK statement in PL/pgSQL

旧城冷巷雨未停 提交于 2019-12-19 05:08:10
问题 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? 回答1: 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

Postgres pl/pgsql ERROR: column “column_name” does not exist

蓝咒 提交于 2019-12-19 04:22:11
问题 i have a storerd procedure like below, CREATE FUNCTION select_transactions3(text, text, int) RETURNS SETOF transactions AS $body$ DECLARE rec transactions%ROWTYPE; BEGIN FOR rec IN (SELECT invoice_no, trans_date FROM transactions WHERE $1 = $2 limit $3 ) LOOP RETURN NEXT rec; END LOOP; END; $body$ LANGUAGE plpgsql VOLATILE SECURITY DEFINER; when i execute query like this : select * from select_transactions3("invoice_no", '1103300105472',10); or select * from select_transactions3(invoice_no,

Postgres pl/pgsql ERROR: column “column_name” does not exist

家住魔仙堡 提交于 2019-12-19 04:22:07
问题 i have a storerd procedure like below, CREATE FUNCTION select_transactions3(text, text, int) RETURNS SETOF transactions AS $body$ DECLARE rec transactions%ROWTYPE; BEGIN FOR rec IN (SELECT invoice_no, trans_date FROM transactions WHERE $1 = $2 limit $3 ) LOOP RETURN NEXT rec; END LOOP; END; $body$ LANGUAGE plpgsql VOLATILE SECURITY DEFINER; when i execute query like this : select * from select_transactions3("invoice_no", '1103300105472',10); or select * from select_transactions3(invoice_no,

Can a function detect the trigger event type?

北城以北 提交于 2019-12-19 04:12:16
问题 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. 回答1: Yes, TG_OP . Per documentation:

Get the count of rows from a COPY command

偶尔善良 提交于 2019-12-19 03:39:31
问题 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. 回答1: Not in PG 9.2, but there is in PG 9.3 courtesy of

Dependency Tracking function

这一生的挚爱 提交于 2019-12-18 17:31:46
问题 I just wonder if anyone knows how to automatize views creation after running DROP ... CASCADE ? Now I'm trying to drop view at first with classic DROP VIEW myview statement and if I cannot drop the view because other objects still depend on it then checking out all the objects names that postgres lists and save their creates and then I run drop with cascade. Sometimes it's like over a dozen objects. But maybe you have got some idea to handle this issue in more automated way? Maybe anybody has

Analysing/Profiling queries on PostgreSQL

北城以北 提交于 2019-12-18 15:53:35
问题 I've just inherited an old PostgreSQL installation and need to do some diagnostics to find out why this database is running slow. On MS SQL you would use a tool such as Profiler to see what queries are running and then see how their execution plan looks like. What tools, if any, exist for PostgreSQL that I can do this with? I would appreciate any help since I´m quite new with Postgres. 回答1: Use pg_stat_statements extension to get long running queries. then use select* from pg_stat_statements

SELECT INTO with more than one attribution

吃可爱长大的小学妹 提交于 2019-12-18 15:15:36
问题 This instruction works: SELECT INTO unsolvedNodes array_agg(DISTINCT idDestination) FROM road WHERE idOrigin = ANY(solvedNodes) AND NOT (idDestination = ANY(solvedNodes)); But I would like to use something this way: SELECT INTO unsolvedNodes array_agg(DISTINCT idDestination), lengths array_agg(length) FROM road WHERE idOrigin = ANY(solvedNodes) AND NOT (idDestination = ANY(solvedNodes)); How to use only one "SELECT INTO" instruction to set multiple variables? 回答1: In PL/pgSQL you can SELECT

SELECT INTO with more than one attribution

懵懂的女人 提交于 2019-12-18 15:13:15
问题 This instruction works: SELECT INTO unsolvedNodes array_agg(DISTINCT idDestination) FROM road WHERE idOrigin = ANY(solvedNodes) AND NOT (idDestination = ANY(solvedNodes)); But I would like to use something this way: SELECT INTO unsolvedNodes array_agg(DISTINCT idDestination), lengths array_agg(length) FROM road WHERE idOrigin = ANY(solvedNodes) AND NOT (idDestination = ANY(solvedNodes)); How to use only one "SELECT INTO" instruction to set multiple variables? 回答1: In PL/pgSQL you can SELECT