plpgsql

GROUP BY and aggregate sequential numeric values

帅比萌擦擦* 提交于 2019-12-17 03:16:43
问题 Using PostgreSQL 9.0. Let's say I have a table containing the fields: company , profession and year . I want to return a result which contains unique companies and professions, but aggregates (into an array is fine) years based on numeric sequence: Example Table: +-----------------------------+ | company | profession | year | +---------+------------+------+ | Google | Programmer | 2000 | | Google | Sales | 2000 | | Google | Sales | 2001 | | Google | Sales | 2002 | | Google | Sales | 2004 | |

Unnest multiple arrays in parallel

匆匆过客 提交于 2019-12-17 02:52:12
问题 My last question Passing an array to stored to postgres was a bit unclear. Now, to clarify my objective: I want to create an Postgres stored procedure which will accept two input parameters. One will be a list of some amounts like for instance (100, 40.5, 76) and the other one will be list of some invoices ('01-2222-05','01-3333-04','01-4444-08') . After that I want to use these two lists of numbers and characters and do something with them. For example I want to take each amount from this

Unnest multiple arrays in parallel

北战南征 提交于 2019-12-17 02:52:06
问题 My last question Passing an array to stored to postgres was a bit unclear. Now, to clarify my objective: I want to create an Postgres stored procedure which will accept two input parameters. One will be a list of some amounts like for instance (100, 40.5, 76) and the other one will be list of some invoices ('01-2222-05','01-3333-04','01-4444-08') . After that I want to use these two lists of numbers and characters and do something with them. For example I want to take each amount from this

How to shuffle array in PostgreSQL 9.6 and also lower versions?

对着背影说爱祢 提交于 2019-12-14 03:55:27
问题 The following custom stored function - CREATE OR REPLACE FUNCTION words_shuffle(in_array varchar[]) RETURNS varchar[] AS $func$ SELECT array_agg(letters.x) FROM (SELECT UNNEST(in_array) x ORDER BY RANDOM()) letters; $func$ LANGUAGE sql STABLE; was shuffling character array in PostgreSQL 9.5.3: words=> select words_shuffle(ARRAY['a','b','c','d','e','f']); words_shuffle --------------- {c,d,b,a,e,f} (1 row) But now after I have switched to PostgreSQL 9.6.2 the function stopped working: words=>

Get sum of integers for UNIQUE ids

自作多情 提交于 2019-12-14 03:12:49
问题 In a game using PostgreSQL 9.3 as backend I am trying to limit the number of games played by a user per week. I have prepared an SQL Fiddle, but unfortunately it doesn't work. My (test, not production) code is here: create table pref_users ( id varchar(32) primary key, last_ip inet ); create table pref_match ( id varchar(32) references pref_users on delete cascade, completed integer default 0 check (completed >= 0), yw char(7) default to_char(current_timestamp, 'IYYY-IW'), primary key(id, yw)

execute sql inside plpgsql function

半腔热情 提交于 2019-12-14 03:08:35
问题 Using postgreSQL 9.1.9, I have some SQL requests like this: INSERT INTO "my_table" VALUES(10,'James','California'); Below a simplified table of the original names lookup table... names name_id name --- ----- 3 James but in fact I don't have to enter (into "my_table") the text value provided by the SQL request (name of person, name of the state) but its corresponding ID located in another table (ex: names table) So i was thinking on creating a trigger, calling a function that should execute

Can't drop temp table in Postgres function: “being used by active queries in this session”

爱⌒轻易说出口 提交于 2019-12-14 03:05:03
问题 It is expected to now take in a table called waypoints and follow through the function body. drop function if exists everything(waypoints); create function everything(waypoints) RETURNS TABLE(node int, xy text[]) as $$ BEGIN drop table if exists bbox; create temporary table bbox(...); insert into bbox select ... from waypoints; drop table if exists b_spaces; create temporary table b_spaces( ... ); insert into b_spaces select ... drop table if exists b_graph; -- Line the error flags. create

plpgsql function that returns multiple columns gets called multiple times

女生的网名这么多〃 提交于 2019-12-14 02:35:13
问题 I'm running PostgreSQL 9.2.1 and have a plpgsql function that returns 3 columns. It's called like this (simplified): SELECT (my_function(b.input)).*, a.other, b.columns FROM table_a a JOIN table_b b ON a.id = b.id WHERE ... The function prints out a WARNING message and I was surprised to find it printed 3 times. It looks like the function gets called 3 times - presumably once for each column. This can't be good for performance! How can I make sure it's called only once? It's already marked

How to pass OLD, NEW and identifiers to EXECUTE in a trigger function?

﹥>﹥吖頭↗ 提交于 2019-12-14 01:59:15
问题 I'm starting and trying some things in a new database, and am encountering a problem. I'm new in PostgreSQL. I'm trying to create a history for the change of values in a column of a users table. The idea is simple. Whenever there is an update, a new record is inserted in another table (which represents the history). DROP FUNCTION IF EXISTS LOCA_APP.FUNC_HISTORICO_MOD_USUARIOS() CASCADE; CREATE OR REPLACE FUNCTION LOCA_APP.FUNC_HISTORICO_MOD_USUARIOS() RETURNS TRIGGER AS $$ BEGIN EXECUTE

Syntax error in function using dblink to replicate new data

被刻印的时光 ゝ 提交于 2019-12-13 20:53:18
问题 I never created a function in Postgres, I followed some tutorials and made this code, but I don't know why it is wrong, the error in the console is: "syntax error at or near "SELECT" LINE 5: SELECT public.dblink_connect('hostaddr=127.0.0.1 port=54... I'm using the version 9.3.6 on Ubuntu. CREATE OR REPLACE FUNCTION fn_replicate_insertof_students() RETURNS text AS $BODY$ BEGIN SELECT public.dblink_connect('hostaddr=127.0.0.1 port=5433 dbname=Utiles user=postgres password=Mypass'); INSERT INTO