plpgsql

postgres. plpgsql stack depth limit exceeded

笑着哭i 提交于 2019-12-07 05:04:21
问题 im working on a simple function where it automatically updates something from a table. create or replace function total() returns void as $$ declare sum int; begin sum = (SELECT count(copy_id) FROM copies); update totalbooks set all_books = sum where num = 1; end; $$ language plpgsql; if i execute "select total();" it works perfectly fine so i made a function trigger so that it automatically updates: create or replace function total1() returns trigger as $$ begin perform (select total());

How to delete table *or* view from PostgreSQL database?

☆樱花仙子☆ 提交于 2019-12-07 04:17:03
问题 I have a name of table or view in PostgreSQL database and need to delete in in single pgSQL command. How can i afford it? I was able to select form system table to find out if there any table with such a name but stuck with procedural part: SELECT count(*) FROM pg_tables where tablename='user_statistics'; 回答1: DROP TABLE user_statistics; DROP VIEW user_statistics; complete syntax: DROP TABLE DROP VIEW And if you want a complete function, i tried something like this: CREATE OR REPLACE FUNCTION

ERROR: query has no destination for result data

谁都会走 提交于 2019-12-06 19:51:41
问题 CREATE OR REPLACE FUNCTION _chkLogin(userid varchar, pwd varchar) RETURNS BOOLEAN AS $BODY$ DECLARE passed BOOLEAN; BEGIN SELECT (_password = $2) FROM _vRegistration WHERE _userid = $1; RETURN passed; END; $BODY$ LANGUAGE 'plpgsql'; When am executing the code above am getting the following error, SELECT _chkLogin('username','abcd') as passed; ERROR: query has no destination for result data I've used perform then i get a different problem, PERFORM _chkLogin('username','abcd'); ERROR: syntax

How to fix postgres-utils eval() error: missing FROM-clause entry for table “foo”?

半城伤御伤魂 提交于 2019-12-06 16:40:06
I'm looking for a way to evaluate price expressions stored in database in Postgres 9.1+ I tried code below from answer in How to evaluate expression in select statement in Postgres but got error ERROR: missing FROM-clause entry for table "product" LINE 1: select product.price*0.95 how to fix ? Maybe it is possible to pass customer and product current row as eval parameters and to use them in eval expresion ? create or replace function eval( sql text ) returns text as $$ declare as_txt text; begin execute 'select ' || sql into as_txt ; return as_txt ; end; $$ language plpgsql; create table

Postgres bulk INSERT function using JSON arguments

隐身守侯 提交于 2019-12-06 12:07:27
问题 Here's a plpgsql function for postgres 9.6 . It tries to INSERT a row, and if the insert doesn't fail (due to a key constraint violation), then it runs a few more commands. CREATE FUNCTION foo(int, text, text) RETURNS void AS $$ BEGIN INSERT INTO table1 (id, val1, val2) VALUES ($1, $2, $3) ON CONFLICT DO NOTHING; IF FOUND THEN INSERT INTO table2 (table1_id, val1) VALUES ($1, $2); UPDATE table3 SET (val2, time) = ($3, now()) WHERE table1_id = $1; END IF; END $$ This function processes a single

Variables for identifiers inside IF EXISTS in a plpgsql function

落爺英雄遲暮 提交于 2019-12-06 11:28:06
CREATE OR REPLACE FUNCTION drop_now() RETURNS void AS $BODY$ DECLARE row record; BEGIN RAISE INFO 'in'; FOR row IN select relname from pg_stat_user_tables WHERE schemaname='public' AND relname LIKE '%test%' LOOP IF EXISTS(SELECT row.relname.tm FROM row.relname WHERE row.relname.tm < current_timestamp - INTERVAL '90 minutes' LIMIT 1) THEN -- EXECUTE 'DROP TABLE ' || quote_ident(row.relname); RAISE INFO 'Dropped table: %', quote_ident(row.relname); END IF; END LOOP; END; $BODY$ LANGUAGE plpgsql VOLATILE; Could you tell me how to use variables in SELECT which is inside IF EXISTS ? At the present

Split string with two delimiters and convert type

大憨熊 提交于 2019-12-06 10:21:33
问题 I have a PL/pgSQL function like this (thanks to the guy who made this possible): CREATE OR REPLACE FUNCTION public.split_string(text, text) RETURNS SETOF text LANGUAGE plpgsql AS $function$ DECLARE pos int; delim_length int := length($2); BEGIN WHILE $1 <> '' LOOP pos := strpos($1,$2); IF pos > 0 THEN RETURN NEXT substring($1 FROM 1 FOR pos - 1); $1 := substring($1 FROM pos + delim_length); ELSE RETURN NEXT $1; EXIT; END IF; END LOOP; RETURN; END; $function$ It splits a string with a

Pl/pgSQL there is no parameter $1 in EXECUTE statement

南楼画角 提交于 2019-12-06 08:35:24
问题 I can't solve this: CREATE OR REPLACE FUNCTION dpol_insert( dpol_cia integer, dpol_tipol character, dpol_nupol integer, dpol_conse integer,dpol_date timestamp) RETURNS integer AS $BODY$ DECLARE tabla text := 'dpol'||EXTRACT (YEAR FROM $5::timestamp); BEGIN EXECUTE ' INSERT INTO '|| quote_ident(tabla) ||' (dpol_cia, dpol_tipol, dpol_nupol, dpol_conse, dpol_date) VALUES ($1,$2,$3,$4,$5) '; END $BODY$ LANGUAGE plpgsql VOLATILE COST 100; When trying SELECT dpol_insert(1,'X',123456,1,'09/10/2013')

Best way to exclude outdated data from a search in PostgreSQL

感情迁移 提交于 2019-12-06 07:18:00
问题 I have a table containing the following columns: an integer column named id a text column named value a timestamp column named creation_date Currently, indexes have been created for the id and value columns. I must search this table for a given value and want to make search as fast as I can. But I don't really need to look through records that are older than one month. So, ideally I would like to exclude them from the index. What would be the best way to achieve this: Perform table

Using dynamic query + user defined datatype in Postgres

落爺英雄遲暮 提交于 2019-12-06 06:47:43
I need a function to normalize my input table features values. My features table has 9 columns out of which x1,x2...x6 are the input columns I need to scale. I'm able to do it by using a static query: create or replace function scale_function() returns void as $$ declare tav1 features%rowtype; rang1 features%rowtype; begin select avg(n),avg(x0),avg(x1),avg(x2),avg(x3),avg(x4),avg(x5),avg(x6),avg(y) into tav1 from features; select max(n)-min(n),max(x0)-min(x0),max(x1)-min(x1),max(x2)-min(x2),max(x3)-min(x3), max(x4)-min(x4),max(x5)-min(x5),max(x6)-min(x6),max(y)-min(y) into rang1 from features;