plpgsql

PL/pgSQL control structures for lists / arrays

吃可爱长大的小学妹 提交于 2019-12-04 06:16:10
问题 Is it possible to use something like this in Postgres? This is the example from PL/SQL what I want to do: PROCEDURE CREATE_PAYMENT(P_AMOUNT IN NUMBER, P_INVOICE_LIST IN SIMPLEARRAYTYPE, P_AMOUNT_LIST IN NUMBER_TABLE -- pass list of amounts . . .) s_chk_amnt NUMBER; invoice_list SIMPLEARRAYTYPE; amount_list NUMBER_TABLE; BEGIN -- check if amount list is null or contains zeros IF p_amount_list IS NOT NULL AND p_amount_list.COUNT <> 0 THEN FOR r IN p_amount_list.FIRST..p_amount_list.LAST LOOP s

PostgreSQL loops outside functions. Is that possible?

回眸只為那壹抹淺笑 提交于 2019-12-04 06:12:39
I'm making comparative about PostgreSQL vs. SQLServer for migrating purposes. Now I'm evaluating T-SQL vs. PL/pgSQL, the thing is that in T-SQL you can use loops or declare variables, for example: declare @counter int set @counter = 0 while @counter < 10 begin set @counter = @counter + 1 print 'The counter is ' + cast(@counter as char) end There is no need to put it inside a function or procedure. Can I do that in PostgreSQL? Searching on the web I found a negative answer doing it in MySQL but I didn't find such answer for Postgres. Erwin Brandstetter You cannot DECLARE (global) variables (

What does %% in PL/pgSQL mean?

倖福魔咒の 提交于 2019-12-04 05:51:48
I was reading over Instagrams sharding solution and I noticed the following line: SELECT nextval('insta5.table_id_seq') %% 1024 INTO seq_id; What does the %% in the SELECT line above do? I looked up PostgreSQL and the only thing I found was that %% is utilized when you want to use a literal percent character. CREATE OR REPLACE FUNCTION insta5.next_id(OUT result bigint) AS $$ DECLARE our_epoch bigint := 1314220021721; seq_id bigint; now_millis bigint; shard_id int := 5; BEGIN SELECT nextval('insta5.table_id_seq') %% 1024 INTO seq_id; SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000)

Raising error in postgreSQL

巧了我就是萌 提交于 2019-12-04 05:29:33
CREATE OR REPLACE FUNCTION msgfailerror() RETURNS trigger AS ' BEGIN IF NEW.noces< new.first_column THEN RAISE EXCEPTION 'cannot have a negative salary'; END IF; return new; END' LANGUAGE plpgsql Trigger create trigger msgfail before insert on first for each row execute procedure msgfailerror() Giving error: syntax error at or near "cannot" LINE 5: RAISE EXCEPTION 'cannot have a negative ... I have almost one validation for each field of row. I want trigger to check all validations while insertion is being done and, raise error log afterwards once for all. Should I use raise exception on raise

Optimize INSERT / UPDATE / DELETE operation

时光总嘲笑我的痴心妄想 提交于 2019-12-04 05:14:42
问题 I wonder if the following script can be optimized somehow. It does write a lot to disk because it deletes possibly up-to-date rows and reinserts them. I was thinking about applying something like "insert ... on duplicate key update" and found some possibilities for single-row updates but I don't know how to apply it in the context of INSERT INTO ... SELECT query . CREATE OR REPLACE FUNCTION update_member_search_index() RETURNS VOID AS $$ DECLARE member_content_type_id INTEGER; BEGIN member

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

三世轮回 提交于 2019-12-04 04:47:54
问题 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 [...]

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

十年热恋 提交于 2019-12-04 04:38:23
问题 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

PostgreSQL 11 - Procedures

亡梦爱人 提交于 2019-12-04 04:15:26
问题 With the latest update of PostgreSQL supporting procedures. The official blog, quoted that "As opposed to functions, procedures are not required to return a value." (https://blog.2ndquadrant.com/postgresql-11-server-side-procedures-part-1/) So my question is, is there actually any way for me to return error code or response in a procedure? (Procedures is rather new in Postgres thus there were very little resources online.) Here is an example of what I meant by returning these "error codes"

Calculate number of rows affected by batch query in PostgreSQL

此生再无相见时 提交于 2019-12-04 04:03:28
问题 First of all, yes I've read documentation for DO statement :) http://www.postgresql.org/docs/9.1/static/sql-do.html So my question: I need to execute some dynamic block of code that contains UPDATE statements and calculate the number of all affected rows. I'm using Ado.Net provider. In Oracle the solution would have 4 steps: add InputOutput parameter "N" to command add BEGIN ... END; to command add :N := :N + sql%rowcount after each statement. It's done! We can read N parameter from command,

Transaction inside a plpgsql function [duplicate]

走远了吗. 提交于 2019-12-04 03:55:39
This question already has answers here : Closed 5 years ago . Committing transactions while executing a postgreql Function (3 answers) I have written a function for automation, mentioned below, which calls some other functions based on some rules. The function is giving me the desired results, but the problem that I am facing is that it does not commit the data after each of the function is processed internally. Once the main function gets completed only then it commits the entire data. I want to do a internal transaction which should commit the data as and when the internal function execution