plpgsql

Passing a ResultSet into a Postgresql Function

烂漫一生 提交于 2019-11-30 03:52:51
问题 Is it possible to pass the results of a postgres query as an input into another function? As a very contrived example, say I have one query like SELECT id, name FROM users LIMIT 50 and I want to create a function my_function that takes the resultset of the first query and returns the minimum id. Is this possible in pl/pgsql? SELECT my_function(SELECT id, name FROM Users LIMIT 50); --returns 50 回答1: It is not possible to pass an array of generic type RECORD to a plpgsql function which is

PostgreSQL functions returning void

人走茶凉 提交于 2019-11-30 00:55:26
问题 Functions written in PL/pgSQL or SQL can be defined as RETURNS void . I recently stumbled upon an odd difference in the result. Consider the following demo: CREATE OR REPLACE FUNCTION f_sql() RETURNS void AS 'SELECT NULL::void' -- "do nothing", no special meaning LANGUAGE sql; CREATE OR REPLACE FUNCTION f_plpgsql() RETURNS void AS $$ BEGIN NULL; -- "do nothing", no special meaning END; $$ LANGUAGE plpgsql; The function f_sql() uses the only possible way for a SELECT (as last command) in a SQL

Postgresql batch insert or ignore

廉价感情. 提交于 2019-11-29 22:06:04
问题 I have the responsibility of switching our code from sqlite to postgres. One of the queries I am having trouble with is copied below. INSERT INTO group_phones(group_id, phone_name) SELECT g.id, p.name FROM phones AS p, groups as g WHERE g.id IN ($add_groups) AND p.name IN ($phones); The problem arises when there is a duplicate record. In this table the combination of both values must be unique. I have used a few plpgsql functions in other places to do update-or-insert operations, but in this

How to write combinatorics function in postgres?

纵然是瞬间 提交于 2019-11-29 20:42:45
问题 I have a PostgreSQL table of this form: base_id int | mods smallint[] 3 | {7,15,48} I need to populate a table of this form: combo_id int | base_id int | mods smallint[] 1 | 3 | 2 | 3 | {7} 3 | 3 | {7,15} 4 | 3 | {7,48} 5 | 3 | {7,15,48} 6 | 3 | {15} 7 | 3 | {15,48} 8 | 3 | {48} I think I could accomplish this using a function that does almost exactly this, iterating over the first table and writing combinations to the second table: Generate all combinations in SQL But, I'm a Postgres novice

PostgreSQL 9.1 pg_restore error regarding PLPGSQL

一世执手 提交于 2019-11-29 20:23:24
I am using Postgres for a django project and I am currently implementing a database backup/restore system that as simple as possible performs a pg_dump when the user clicks backup and then pg_restore when they click restore backup. All seems fine and dandy until it actually tries to perform the pg_restore at which time it gives this error: pg_restore: [archiver (db)] Error from TOC entry 3206; 0 0 COMMENT EXTENSION plpgsql pg_restore: [archiver (db)] could not execute query: ERROR: must be owner of extension plpgsql Command was: COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language'; I

PostgreSQL equivalent of Oracle “bulk collect”

限于喜欢 提交于 2019-11-29 18:10:10
In PostgreSQL exists some ways to make a statement using bulk collect into like in Oracle? Example in Oracle: create or replace procedure prc_tst_bulk_test is type typ_person is table of tb_person%rowtype; v_tb_person typ_person; begin select * bulk collect into v_tb_person from tb_person; -- make a selection in v_tb_person, for instance select name, count(*) from v_tb_person where age > 50 union select name, count(*) from v_tb_person where gender = 1 end; Thank you There is no such syntax in PostgreSQL, nor a close functional equivalent. You can create a temporary table in your PL/PgSQL code

PostgreSQL return a function with a Custom Data Type

情到浓时终转凉″ 提交于 2019-11-29 18:03:11
I want to create a function that returns a type that I made, but when I execute it, it says that the type doesn't exists. I assume that's cause it doesn't know about the custom type. UDT: CREATE TYPE building_code AS ENUM ('IT','EMS','HSB','ENG'); Function: CREATE FUNCTION roomCode(id int ) RETURNS building_code AS $$ SELECT building_code FROM venue as v WHERE id = v.id; $$ LANGUAGE SQL; This should just work. The enum should not be a problem. Tested with Postgres 9.1 and 9.2 CREATE TYPE building_code AS ENUM ('IT','EMS','HSB','ENG'); CREATE TEMP TABLE venue (id int PRIMARY KEY, building_code

Postgres Function NULL value for row that references NEW

◇◆丶佛笑我妖孽 提交于 2019-11-29 17:51:57
I have a trigger setup to fire off after a row is updated or inserted for table A. There is also a table B that references table A, but also has another column which I want to analyze, a boolean (so this function would only be usable on an UPDATE). When I try to access the column: SELECT col1 FROM B WHERE B.aID = NEW.ID; This column col1 is always NULL. Why is this? How can I get the boolean value upon an update? I'll have to guess, because you cunningly kept the function definition a secret. (Irony intended.) Most probably you are running into a naming conflict. Parameter names ( IN and OUT

Run SQL statements in PL/pgSQL only if a row doesn't exist

独自空忆成欢 提交于 2019-11-29 17:06:20
I want to do something like this in a PL/pgSQL function in Postgres 9.6: INSERT INTO table1 (id, value) VALUES (1, 'a') ON CONFLICT DO NOTHING; --- If the above statement didn't insert a new row --- because id of 1 already existed, --- then run the following statements INSERT INTO table2 (table1_id, value) VALUES (1, 'a'); UPDATE table3 set (table1_id, time) = (1, now()); However, I don't know how to determine whether the first INSERT actually inserted a new row, or whether the the ON CONFLICT DO NOTHING was done. I could do a SELECT at the beginning of the function to see whether a record

drop all tables sharing the same prefix in postgres

只谈情不闲聊 提交于 2019-11-29 16:54:16
问题 I would like to delete all tables sharing the same prefix ('supenh_agk') from the same database, using one sql command/query. 回答1: To do this in one command you need dynamic SQL with EXECUTE in a DO statement (or function): DO $do$ DECLARE _tbl text; BEGIN FOR _tbl IN SELECT quote_ident(table_schema) || '.' || quote_ident(table_name) -- escape identifier and schema-qualify! FROM information_schema.tables WHERE table_name LIKE 'prefix' || '%' -- your table name prefix AND table_schema NOT LIKE