plpgsql

How to round to nearest X minutes with PL/pgSQL?

三世轮回 提交于 2019-11-30 19:52:57
问题 How I can round to nearest X minutes? Here's my attempt: DECLARE _stamp ALIAS FOR $1; -- timestamp _nearest ALIAS FOR $2; -- minutes (integer) _minutes decimal; _ret timestamp; BEGIN _ret := date_trunc('minute', _stamp); SELECT EXTRACT (minute FROM _ret)::integer INTO _minutes; IF (_minutes % _nearest < (_nearest / 2)) THEN RETURN _ret + _minutes * interval '1 minute'; ELSE RETURN _ret - _minutes * interval '1 minute'; END IF; RETURN _ret; END; Example: SELECT round_to_nearest_minute ('2010

Postgres function much slower when using input variables

柔情痞子 提交于 2019-11-30 19:46:08
问题 I have a function in Postgres 8.3.5 that selects data from multiple tables and dumps the result in a single table: create or replace function test_function_2(startdate timestamp, enddate timestamp) returns void as $$ begin delete from cl_final_report; INSERT INTO cl_final_report SELECT b.batchkey AS batchnumber, pv.productkey, p.name AS productname, avg(r.value) AS avgchemlean, sum(r.auxvalue) AS totalweight, max(o.time) AS timecompleted FROM result r LEFT JOIN physicalvalue pv ON r

Passing a ResultSet into a Postgresql Function

断了今生、忘了曾经 提交于 2019-11-30 19:27:42
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 It is not possible to pass an array of generic type RECORD to a plpgsql function which is essentially what you are trying to do. What you can do is pass in an array of a specific user defined TYPE or of

plpgsql - using dynamic table name in declare statement

梦想的初衷 提交于 2019-11-30 19:12:33
问题 I'm trying to write plpgsql a function of the following form (note this is a simplified version): CREATE FUNCTION check_valid(tablename regclass) RETURNS boolean AS $$ DECLARE valid_row tablename%ROWTYPE; BEGIN EXECUTE format('SELECT * FROM %s', tablename) into valid_row; IF valid_row IS NULL THEN RETURN QUERY SELECT false; ELSIF valid_row.is_valid = false; RETURN QUERY SELECT false; ELSIF valid_row.hit_count > valid_row.hit_limit; RETURN QUERY SELECT false; ELSE RETURN QUERY SELECT true; END

PostgreSQL modifying fields dynamically in NEW record in a trigger function

↘锁芯ラ 提交于 2019-11-30 17:59:39
问题 I have a user table with IDs and usernames (and other details) and several other tables referring to this table with various column names ( CONSTRAINT some_name FOREIGN KEY (columnname) REFERENCES "user" (userid) ). What I need to do is add the usernames to the referring tables (in preparation for dropping the whole user table). This is of course easily accomplished with a single ALTER TABLE and UPDATE , and keeping these up-to-date with triggers is also (fairly) easy. But it's the trigger

Query plan caching with pl/pgsql

大兔子大兔子 提交于 2019-11-30 17:26:46
问题 I am having troubles understanding how the query plan caching works for pl/pgsql. I want to built all-in-one queries with JOIN s and IF s, so I will have multiple and different query parameters, and I will be searching in more that one tables. At first I thought that using pl/pgsql will produce a different plan for each parameters combination, that is not the case, because I have more than one tables SQL commands that appear directly in a PL/pgSQL function must refer to the same tables and

PostgreSQL functions returning void

╄→гoц情女王★ 提交于 2019-11-30 17:19:55
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 function that RETURNS void . I use it just because it is the simplest way for the purposes of this

SQL state: 42601 syntax error at or near “11”

若如初见. 提交于 2019-11-30 16:15:44
问题 I have a table address_all and it is inherited by several address tables. address_history inherits from parent table history_all and keeps current address information. I am creating new table which inherits address_all table and copies information from address_history to new table. My stored procedure is like this below. I am having some error when I call it. To better explain error I am using line number. 1 CREATE OR REPLACE FUNCTION somefunc() 2 RETURNS void AS 3 $BODY$ 4 DECLARE 5 year_id

Self-managing PostgreSQL partition tables

强颜欢笑 提交于 2019-11-30 16:06:29
问题 I am trying to make a self-managing partition table setup with Postgres. It all revolves around this function but I can't seem to get Postgres to accept my table names. Any ideas or examples of self-managing partition table trigger functions? My current function: DECLARE day integer; year integer; tablename text; startdate text; enddate text; BEGIN day:=date_part('doy',to_timestamp(NEW.date)); year:=date_part('year',to_timestamp(NEW.date)); tablename:='pings_'||year||'_'||day||'_'||NEW.id; --

Dependency Tracking function

南笙酒味 提交于 2019-11-30 16:05:28
I just wonder if anyone knows how to automatize views creation after running DROP ... CASCADE ? Now I'm trying to drop view at first with classic DROP VIEW myview statement and if I cannot drop the view because other objects still depend on it then checking out all the objects names that postgres lists and save their creates and then I run drop with cascade. Sometimes it's like over a dozen objects. But maybe you have got some idea to handle this issue in more automated way? Maybe anybody has got some function? Next step... (continuation of my previous answer). function save_views(objectname