plpgsql

pgsql return table ERROR: column reference is ambiguous

笑着哭i 提交于 2019-12-12 10:39:58
问题 I keep getting this ERROR: column reference "person" is ambiguous. It is required of me to return a TABLE (person integer). It works fine when I use SETOF integer but in this instance it doesn't work. My other function recurse() returns a set of integers perfectly well. CREATE OR REPLACE FUNCTION try(_group text) RETURNS TABLE (person integer) AS $$ DECLARE _init_id integer; _record integer; BEGIN SELECT id INTO _init_id FROM egroups WHERE name = _group; FOR _record in SELECT person FROM

How can I get results from a JPA entity ordered by distance?

泄露秘密 提交于 2019-12-12 10:18:24
问题 I am currently writing a mobile application where the user has to pick a location from a list. All the locations are stored in a Postgres database using JPA from a Play app. What I would like to do is get the users location in the app, and then make a request to get the first 20 or 50 locations nearest to that user. If I was using my own data structure for this, I would use a KD-Tree. However, I am very new to JPA/Play/PostgreSQL so I am unsure how to handle data persistance manually. The

PostgreSQL obtain and release LOCK inside stored function

僤鯓⒐⒋嵵緔 提交于 2019-12-12 09:36:51
问题 I have a function that needs to perform a long update on multiple large tables. During the update 2-3 tables at a time need to be locked in EXCLUSIVE mode. Since not all the tables need to be locked at the same time, ideally I'd want to LOCK only those tables I'm updating at the time, and then remove the lock once I'm done. Eg. -- Lock first pair of tables LOCK TABLE tbl1_a IN EXCLUSIVE MODE; LOCK TABLE tbl1_b IN EXCLUSIVE MODE; -- Perform the update on tbl1_a and tbl1_b -- Release the locks

How to find if a function exists in PostgreSQL?

▼魔方 西西 提交于 2019-12-12 08:21:17
问题 Unlike tables or sequences, user-defined functions cannot be found through pg_class. There are questions on how find a list of all functions to delete or grant them, but how to find an individual function (with known name and argument types) is not self-evident from them. So how to find whether a function exists or not? EDIT: I want to use it in a function, in automated manner. Which solution is the best performance-wise? Trapping errors is quite expensive, so I guess the best solution for me

How to save a data with comma in character varying that passes through a trigger?

家住魔仙堡 提交于 2019-12-12 06:59:42
问题 I have a field of type character varying but I get an error when trying to save a data that contains decimal. I want to save that data without problem. This is my trigger: CREATE TRIGGER "public.usuarios_trigger_process_audit" BEFORE INSERT OR UPDATE OR DELETE ON usuarios FOR EACH ROW EXECUTE PROCEDURE process_audit(); This the PROCEDURE: DECLARE newtable text; col information_schema.columns %ROWTYPE; txtquery text; line_old TEXT; tmpquery text; i int; columns_old text[]; BEGIN IF ( TG_TABLE

select function() in postgresql makes too much calls to function() [duplicate]

此生再无相见时 提交于 2019-12-12 06:11:45
问题 This question already has an answer here : How to avoid multiple function evals with the (func()).* syntax in an SQL query? (1 answer) Closed 4 years ago . Let's assume we have this function: create or replace function foo(a integer) returns table (b integer, c integer) language plpgsql as $$ begin raise notice 'foo()'; return query select a*2, a*4; return query select a*6, a*8; return query select a*10, a*12; end; $$; The "raise notice 'foo()'" part will be used to know how many time the

Alter every double fields of a table

橙三吉。 提交于 2019-12-12 05:47:16
问题 I need to change every double precision field of a table to numeric(15,3) type, how can i do this job quickly with a stored procedure that iterate through the field of a given table and if the type is double precision alter column to numeric ? 回答1: following query should return query to update these tables... you can add table filter if you want. Copy the result and run it. select 'ALTER TABLE ' + table_name + ' ALTER COLUMN ' + column_name + 'TYPE numeric(15,3)' from information_schema

Do nothing in a trigger procedure

倾然丶 夕夏残阳落幕 提交于 2019-12-12 05:14:08
问题 I got a trouble when a try to execute a trigger. Let's suppose we have 2 tables and I want to copy data from table A to table B but each table got a unique constraint . create table test1 ( test_name varchar); create unique index test1_uc on test1 USING btree (test_name); create table test2 ( test_name2 varchar); create unique index test2_uc on test2 USING btree (test_name2); CREATE OR REPLACE FUNCTION trig_test() RETURNS trigger AS $$ BEGIN IF pg_trigger_depth() <> 1 THEN RETURN NEW; END IF;

Pass multidimensional array as parameter to Postgresql function

五迷三道 提交于 2019-12-12 05:14:03
问题 I'm trying to maintain a Php application with a PostgreSQL database. At one point, a stored procedure is called, lets say function_x and inside function_x , function_y is called; function_y is passed a variable named parameter_1 , and the definition of parameter_1 is: parameter_1 numeric[][3] := {}; I'm trying to do a select function_y directly on the command line (or pgadmin) but I'm having problems passing an empty array into the function. according to the docs you have to use variadic but

Postgresql functions

雨燕双飞 提交于 2019-12-12 04:38:28
问题 These are the type definitions provided to me create type IR as (pattern_number integer, uoc_number integer); My current progress is: create or replace function q1(pattern text, uoc_threshold integer) returns setof IR as $$ BEGIN RETURN QUERY select count(code) from temp where code like $1; RETURN QUERY select count(code) from temp where code like $1 and uoc > $2; END; $$ language plpgsql; My output needs to be like this : Query:- select * from q1('ECO%', 6); pattern_number | uoc_number 80 |