plpgsql

Postgres Create View With Record Type Function

十年热恋 提交于 2019-12-11 06:19:39
问题 I have this complex query with a user defined function conta_relatos() running very well as a select statement. But it doesn't work when I try to create a view with the same instructions. Postgres is telling me that the column "conta_relatos" has pseudo-type record. This function, conta_relatos() returns a record type variable. Addition by Editor: The return type is a well known composite type as defined in the preceding question: Postgres Function End Loop and return Error Below is the query

Handle result when dynamic SQL is in a loop

亡梦爱人 提交于 2019-12-11 05:34:49
问题 I have a bunch of table that have a "stat" column (stat for status ;-) I would like the count of each stats, and see it! My tables look like this create table a ( a_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), a_stat status_t ); create table b ( b_id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), b_stat status_t ); status_t is an enum. So I did this: DO $$ DECLARE tableName RECORD; result RECORD; BEGIN SET SEARCH_PATH = projet, public; FOR tableName IN SELECT c.relname, a.attname FROM pg

Loop through like tables in a schema

て烟熏妆下的殇ゞ 提交于 2019-12-11 05:12:43
问题 Postgres 9.1 - I have a schema that has tables "partitioned" by month (a new table is created each month, all columns the same). It is not set up as normal partitioning with a "master" table. I am currently writing a fairly large query, that I will have to run a few times each month. Schema: augmented_events tables: p201301 (January 2013) p201302 (Feb 2013) p201303 (March 2013) ... p201312 (December 2013) p201401 (January 2014) Right now I have to write my (simplified) query as: select * from

Java sum 2 negative numbers

天大地大妈咪最大 提交于 2019-12-11 05:05:29
问题 I'm trying to convert a function in java to pl/pgsql, and one problem that I found is when I'm trying to sum 2 negative numbers, and a get a positive number, more specifically : public void sum(){ int n1 = -1808642602; int n2 = -904321301; System.out.println(n1 + n2);// result is 1582003393 } And in pl/pgsql I get an integer out of range error, and if I change the variables type to bigint i get a normal sum of 2 negative numbers, that is -2712963903, instead of 1582003393 How do I do to pl

Syntax error at or near “USING”

孤者浪人 提交于 2019-12-11 04:59:11
问题 I am trying to recreate the functionality of a query found inside of a mapfile on our mapserver into a plpgsql stored procedure. Here is the query: geom from (select g.gid, g.geom, g.basin, a.\"DATE\", a.\"VALUE\" from sarffg_basins_00_regional_3sec as g join \"%prod_table%\" as a on g.basin = a.\"BASIN\" where a.\"DATE\" = '%prod_date%') as subquery using unique gid using srid=4326 Within my stored procedure, I have: RETURN QUERY EXECUTE 'SELECT geom FROM ( SELECT g.gid, g.geom, g.basin, a

Pass array of tags to a plpgsql function and use it in WHERE condition

和自甴很熟 提交于 2019-12-11 04:41:36
问题 I'd like to create a function that returns items based on their tags . However, I do not know how to format an array in the IN() clause. I believe that is why I get no result. Here is what I got: CREATE OR REPLACE FUNCTION getItemsByTag(tags text[]) RETURNS TABLE (id bigint, title text, tag text[]) AS $$ BEGIN IF array_length(tags, 1) > 0 THEN EXECUTE format(' SELECT d.id, d.title, array_agg(t.title) FROM items d INNER JOIN item_tags dt ON dt.item_id = d.id INNER JOIN tags t ON t.id = dt.tag

Generate unique random strings in plpgsql

有些话、适合烂在心里 提交于 2019-12-11 04:29:48
问题 I am trying to write a function to create unique random tokens of variable length. However, I am stumped by the plpgsql syntax. My intention is to create a function which Takes a table and column as input Generates a random string of a given length, with a given set of characters Checks if the string is already in the colum If so (and this is expected to be rare), simply generate a new random string. Otherwise, return the random string My current attempt looks like this: CREATE FUNCTION

PL/pgSQL: accessing fields of an element of an array of custom type

空扰寡人 提交于 2019-12-11 04:11:38
问题 I've defined a custom type: create type pg_temp.MYTYPE as (f1 int, f2 text); Then, inside a function or a block, I've declared an array of such type: declare ax MYTYPE[]; I can access an element through the familiar syntax ax[1].f1 , but just for reading it. When I use the same syntax for setting the field, I get a syntax error. create type pg_temp.MYTYPE as (f1 int, f2 text); do $$ declare x MYTYPE; declare ax MYTYPE[]; declare f int; begin x.f1 = 10; x.f2 = 'hello'; --assigning an element:

Trigger update another table

折月煮酒 提交于 2019-12-11 03:48:09
问题 I have been trying to write a trigger function that updates the rows in the child table when the parent is changed for a while now. I have read Trigger procedure documentation but i have not really grasped how to build the functions. This is what I have tried that does not work... CREATE FUNCTION myschema.update_child() RETURNS trigger AS $BODY$ BEGIN UPDATE myschema.child set new.number = parent.number FROM myschema.parent WHERE id = "id"; RETURN NEW; END $BODY$ LANGUAGE plpgsql Then the

PostgreSQL dynamic table access

回眸只為那壹抹淺笑 提交于 2019-12-11 03:37:03
问题 I have a products schema and some tables there. Each table in products schema has an id , and by this id I can get this table name, e.g. products \ product1 \ product2 \ product3 I need to select info from dynamic access to appropriate product, e.g. SELECT * FROM 'products.'(SELECT id from categories WHERE id = 7); Of course, this doesn't work... How I can do something like that in PostgreSQL? 回答1: OK, I found a solution: CREATE OR REPLACE FUNCTION getProductById(cid int) RETURNS RECORD AS $$