plpgsql

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

旧城冷巷雨未停 提交于 2019-12-08 06:56:44
问题 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;

How to import external sql scripts from a sql script in PostgreSQL?

戏子无情 提交于 2019-12-08 05:53:57
问题 I was wondering if it is possible/how to include one .sql file in another .sql file, as is done in other programming languages like C or Java? I am asking because I wanted to organize my sql scripts into support library files and application scripts, and so on. I searched around, and solutions seem to be about loading .sql files from the psql client (see e.g. postgreSQL - psql \i : how to execute script in a given path). What I am interested in is to load a library .sql script from inside a

Get result from query in DO satement

大兔子大兔子 提交于 2019-12-08 05:11:39
问题 How to run SQL statement within an IF condition in plpgsql? I don't want to create or replace a function. This is what I tried: DO LANGUAGE plpgsql $$ BEGIN IF 'Khosla' = 'Khosla' THEN SELECT * FROM test_log limit 10; ELSE RAISE NOTICE 'not worked'; END IF; END; $$; ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function "inline_code_block" line 3 at SQL statement I also tried this but was unable to

Dynamically generated CURSOR in Postgresql

陌路散爱 提交于 2019-12-08 04:17:56
问题 I have got a cursor, it is pointing to a SELECT, but this select is generated dynamically. I want to assign the statement after the declarement. I have done an example working and another example NOT working. This is a simple example to print some data only. This is the table: CREATE TABLE public.my_columns ( id serial NOT NULL, "name" varchar(30) NOT NULL, /* Keys */ CONSTRAINT my_columns_pkey PRIMARY KEY (id) ) WITH ( OIDS = FALSE ); CREATE INDEX my_columns_index01 ON public.my_columns (

Get result from query in DO satement

谁说我不能喝 提交于 2019-12-08 03:24:30
How to run SQL statement within an IF condition in plpgsql? I don't want to create or replace a function. This is what I tried: DO LANGUAGE plpgsql $$ BEGIN IF 'Khosla' = 'Khosla' THEN SELECT * FROM test_log limit 10; ELSE RAISE NOTICE 'not worked'; END IF; END; $$; ERROR: query has no destination for result data HINT: If you want to discard the results of a SELECT, use PERFORM instead. CONTEXT: PL/pgSQL function "inline_code_block" line 3 at SQL statement I also tried this but was unable to get data: DO LANGUAGE plpgsql $$ BEGIN if 'a' = 'a' THEN PERFORM * FROM test_log limit 10; else raise

Return type for function with array_agg()

我怕爱的太早我们不能终老 提交于 2019-12-08 02:04:35
问题 I'm trying to create a function that returns an array of string, I'm able to do it without a function and returns a record[] type, when i try to return that type of result in the function it says that is not supported. CREATE OR REPLACE FUNCTION alarmEventList(sampleid integer , starttime timestamp without time zone , stoptime timestamp without time zone) RETURNS text[] AS DECLARE result record[]; BEGIN select array_agg(result) from (select to_char("Timestamp", 'YYYY-MM-DD HH24:MI:SS'),

Give a user permission to ALTER a function

烈酒焚心 提交于 2019-12-07 21:38:37
问题 I try to ALTER a function with a new user and I get the error: ERROR: must be owner of function ACases ********** Error ********** ERROR: must be owner of function ACases SQL state: 42501 What permission do I have to give to a user so he can ALTER that function? The only way I found was to make the user the OWNER of the function. But if that is the case, only one user (owner) can ALTER the function. So how would I change the OWNER for all functions? CREATE OR REPLACE FUNCTION public."ACases"

Dynamically define returning row types based on a passed given table in plpgsql?

佐手、 提交于 2019-12-07 18:31:27
I'm dynamically building a query in a plpgsql function that accepts a source table as an incoming variable. I want to return the results of the built SELECT statement, which performs aggregations on the given table, and returns results of that table. However, at the moment I'm getting the following error: ********** Error ********** ERROR: a column definition list is required for functions returning "record" SQL state: 42601 So it looks like I need to define column types of the record rows I want to return. I found this answer where you can bypass table type declaration by providing the table

PostgreSQL 9.3 trigger function to insert into table with parameterized name

北城以北 提交于 2019-12-07 16:15:17
问题 I'm trying to dynamically partition log entries in Postgres. I have 53 child tables (1 for each week's worth of log entries), and would like to route INSERTs to a child table using a trigger. I run the function with INSERT INTO log5 VALUES (NEW.*) , and it works. I run the function with the EXECUTE statement instead, and it fails. Within the EXECUTE statement, it's recognizing NEW as a table name and not a variable passed to the trigger function. Any ideas on how to fix? Thanks! The error:

How to improve performance of a function with cursors in PostgreSQL?

耗尽温柔 提交于 2019-12-07 15:02:13
问题 I have function with two nested cursors. The outer cursor gets payment details of a customer from the source and inserts into the target based on some business logic. The inner cursor takes the payment details of each payment, it happens one after another. The payments table has about 125000 rows, and about 335000 rows for payment details. All of these rows are to be migrated to a target table. Executing the function takes over two hours and the database CPU usage goes up to 99%. I am working