plpgsql

MySQL/Postgres query 5 minutes interval data

a 夏天 提交于 2019-12-07 10:03:43
问题 I need help with the query, let's say that this is the data in table. timestamp ------------------- 2010-11-16 10:30:00 2010-11-16 10:37:00 2010-11-16 10:40:00 2010-11-16 10:45:00 2010-11-16 10:48:00 2010-11-16 10:55:00 2010-11-16 10:56:00 I want to get every first row (timestamp) that is at least 5 minutes later than the last. In this case the query should return: timestamp ------------------- 2010-11-16 10:30:00 2010-11-16 10:37:00 2010-11-16 10:45:00 2010-11-16 10:55:00 回答1: Recursive CTE

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

一世执手 提交于 2019-12-07 09:38:58
问题 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. 回答1: 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

Converting function from oracle to postgreSQL

强颜欢笑 提交于 2019-12-07 08:56:50
问题 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

PostgreSQL regexp_replace with matched expression

纵然是瞬间 提交于 2019-12-07 08:47:19
问题 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

Function as parameter to another function in Postgres

ⅰ亾dé卋堺 提交于 2019-12-07 08:10:42
问题 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? 回答1: I think you can't, but since there are no anonymous functions, passing function name should do. 回答2: Since each function / procedure must have an entry

SELECT dynamic columns without functions in PostgreSQL

守給你的承諾、 提交于 2019-12-07 07:19:28
I need to select rows from two and more tables ("A", "B"). They have differences columns and I don't use inheritance for it. So. For example: SELECT * FROM "A" UNION SELECT * FROM "B" ERROR: each UNION query must have the same number of columns I can understand why. I try get intersected columns from root schema in root table: SELECT column_name FROM information_schema.columns WHERE table_schema = 'client_root' AND table_name ='conditions' It's ok! But I don't use query: SELECT (SELECT column_name FROM information_schema.columns WHERE table_schema = 'client_root' AND table_name ='conditions')

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

谁说我不能喝 提交于 2019-12-07 07:14:45
问题 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 =

Create PostgreSQL trigger using JDBC

妖精的绣舞 提交于 2019-12-07 06:50:21
问题 I am trying to create a PostgreSQL trigger in a Play2.0 database evolution script. The sql code is relatively easy and runs fine in pgAdminIII: CREATE OR REPLACE FUNCTION update_modified() RETURNS TRIGGER AS $$ BEGIN NEW.modified = now(); RETURN NEW; END; $$ LANGUAGE 'plpgsql'; However, I get an error when running the evolution: ERROR: unterminated dollar-quoted string at or near "$$ BEGIN NEW.modified = now()" . The SQL code seems to get truncated at the first semicolon encountered in the

what does “LANGUAGE 'plpgsql' VOLATILE” mean?

落花浮王杯 提交于 2019-12-07 05:42:15
问题 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? 回答1: 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

Select every first element of array of integer arrays to array

喜欢而已 提交于 2019-12-07 05:23:50
问题 How to select every first element of array of integer arrays to array? {{1,2,3},{2,15,32},{5,16,14},...} -> {1,2,5,...} 回答1: Since PostgreSQL will allow asking for a slice outside of the array size, and assuming there will never be more than 999 subarrays, we can use this monstrosity WITH data AS ( SELECT array[array[1,2,3], array[2,15,32], array[5,16,14]] as arr) SELECT array_agg(arr) FROM (SELECT unnest(arr[1:999][1]) as arr from data) data2; You can of course make the constant 999 larger