plpgsql

Iterating through PostgreSQL records. How to reference data from next row?

人盡茶涼 提交于 2019-12-05 20:47:38
I'm new to PostgreSQL and writing functions here is tough as nails. So I'm hoping someone can help let me know how to do what I'm trying to do. I have a table of stock prices and dates. I want to calculate the percent change from the previous day for each entry. For the earliest day of data, there won't be a previous day, so that entry can simply be Nil. Can someone look over my function and help me with a) how to reference data from the next row and b) help me clean it up? I'm aware that the WITH statement is probably not supposed to be above the IF statement. However logically, this is how I

Transaction inside a plpgsql function [duplicate]

时光怂恿深爱的人放手 提交于 2019-12-05 20:36:58
问题 This question already has answers here : Committing transactions while executing a postgreql Function (3 answers) Closed 5 years ago . 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

How do I get the type of an array's elements?

﹥>﹥吖頭↗ 提交于 2019-12-05 19:50:50
I'm writing a polymorphic PL/pgSQL function that iterates over an array. I am interested in using FOREACH , however I cannot figure out how to declare a temporary variable with the right type. My function is below, for more information see the comment on line 4. CREATE OR REPLACE FUNCTION uniq(ary anyarray) RETURNS anyarray AS $$ DECLARE ret ary%TYPE := '{}'; v ???; -- how do I get the element type of @ary@? BEGIN IF ary IS NULL THEN return NULL; END IF; FOREACH v IN ARRAY ary LOOP IF NOT v = any(ret) THEN ret = array_append(ret, v); END IF; END LOOP; RETURN ret; END; $$ LANGUAGE plpgsql;

How to improve performance of a function with cursors in PostgreSQL?

孤者浪人 提交于 2019-12-05 18:35:29
I have function with two nested cursors. The outer cursor gets payment details of a customer from the source and inserts into the target based on some business logic. The inner cursor takes the payment details of each payment, it happens one after another. The payments table has about 125000 rows, and about 335000 rows for payment details. All of these rows are to be migrated to a target table. Executing the function takes over two hours and the database CPU usage goes up to 99%. I am working with PostgreSQL 9.2. How can I improve the performance of the function? The code I am using: CREATE OR

How to get a list of stored procedures using a specific table in PostgreSQL?

只愿长相守 提交于 2019-12-05 18:16:50
In PostgreSQL (9.3) is there a simple way to get a list of the stored procedures that use a specific table? I'm changing several tables and need to fix the stored procedures that use them. Functions which have text 'thetable' in their body. The query returns function name, line number and line containg 'thetable': select * from ( select proname, row_number() over (partition by proname) as line, textline from ( select proname, unnest(string_to_array(prosrc, chr(10))) textline from pg_proc p join pg_namespace n on n.oid = p.pronamespace where nspname = 'public' and prosrc ilike '%thetable%' )

Converting function from oracle to postgreSQL

醉酒当歌 提交于 2019-12-05 17:49:43
I am working on converting something from Oracle to PostgreSQL. In the Oracle file there is a function: instr(string,substring,starting point,nth location) or as it is in my file instr(string,chr(10),instr(string,substring),1) In PostgreSQL this does not exist, so I looked up an equivalent function. I found: position(substring in string) but this does not allow the starting position and the nth location parameters. Is there anyway to make this function start at a given point? Or is there a better function to use in PostgreSQL where I can specify starting position and the nth location? This

Generic trigger to restrict insertions based on count

你离开我真会死。 提交于 2019-12-05 15:45:31
Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships. The number of those relationships must be restricted. A couple of example tables include: CREATE TABLE authentication ( id bigserial NOT NULL, -- Primary key cookie character varying(64) NOT NULL, -- Authenticates the user with a cookie ip_address character varying(40) NOT NULL -- Device IP address (IPv6-friendly) ) CREATE TABLE tag_comment ( id bigserial NOT NULL, -- Primary key comment_id bigint, -- Foreign key to the comment table tag_name_id bigint -- Foreign key to the tag name table )

PostgreSQL regexp_replace with matched expression

不打扰是莪最后的温柔 提交于 2019-12-05 14:44:56
I am using PostgreSQL regexp_replace function to escape square brackets, parentheses and backslash in a string so that I could use that string as a regex pattern itself (there are other manipulations done on this string as well before using it, but they are outside the scope of this question. The idea is to replace: [ with \[ ] with \] ( with \( ) with \) \ with \\ Postgres documentation page on regular expressions states the following: The replacement string can contain \n, where n is 1 through 9, to indicate that the source substring matching the n'th parenthesized subexpression of the

what does “LANGUAGE 'plpgsql' VOLATILE” mean?

喜你入骨 提交于 2019-12-05 13:06:48
When I create or update a function or procedure in a Postgres database I see LANGUAGE 'plpgsql' VOLATILE at the end of function. What does this mean and what is its purpose? Akash KC From Postgres docs : VOLATILE indicates that the function value can change even within a single table scan, so no optimizations can be made. Relatively few database functions are volatile in this sense; some examples are random(), currval(), timeofday(). But note that any function that has side-effects must be classified volatile, even if its result is quite predictable, to prevent calls from being optimized away;

Function as parameter to another function in Postgres

﹥>﹥吖頭↗ 提交于 2019-12-05 10:58:39
Can I create a user defined function in Postgres either through the C-Language Function API or by using pl/pgsql which accepts a callback function as parameter? As far as I see there is no way to do this through the C-Language API since it only accepts sql datatypes and there is no datatype for function . But maybe I'm missing something? I think you can't, but since there are no anonymous functions, passing function name should do. Since each function / procedure must have an entry in pg_proc, you can use the primary key for identifying the procedure. This would also eliminate the problems