plpgsql

PLpgSQL function to find columns with only NULL values in a given table

最后都变了- 提交于 2019-12-02 23:49:17
问题 We have to find columns of a table with only NULL values. We are trying to build a plpgsql function that takes a table's name and returns the list of such columns. How to create such a function? We are using PgAdmin 1.16. 回答1: You can query the catalog table pg_attribute to get a list of columns which are not defined NOT NULL and therefore can hold NULL values: SELECT quote_ident(attname) AS column_can_be_null FROM pg_attribute WHERE attrelid = 'tbl'::regclass -- valid, visible table name AND

Endless loop in trigger function

六眼飞鱼酱① 提交于 2019-12-02 22:41:22
问题 This is a trigger that is called by either an insert, update or a delete on a table. It is guaranteed the calling table has all the columns impacted and a deletes table also exists. CREATE OR REPLACE FUNCTION sample_trigger_func() RETURNS TRIGGER AS $$ DECLARE operation_code char; table_name varchar(50); delete_table_name varchar(50); old_id integer; BEGIN table_name = TG_TABLE_NAME; delete_table_name = TG_TABLE_NAME || '_deletes'; SELECT SUBSTR(TG_OP, 1, 1)::CHAR INTO operation_code; IF TG

Function taking forever to run for large number of records

雨燕双飞 提交于 2019-12-02 21:37:17
问题 I have created the following function in Postgres 9.3.5: CREATE OR REPLACE FUNCTION get_result(val1 text, val2 text) RETURNS text AS $BODY $Declare result text; BEGIN select min(id) into result from table where id_used is null and id_type = val2; update table set id_used = 'Y', col1 = val1, id_used_date = now() where id_type = val2 and id = result; RETURN result; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; When I run this function in a loop of over a 1000 or more records it just does

Query returning exact number of rows

社会主义新天地 提交于 2019-12-02 19:08:33
问题 I have a table that stores two foreign keys, implementing a n:m relationship. One of them points to a person ( subject ), the other one to a specific item. Now, the amount of items a person may have is specified in a different table and I need a query which would return the same number of rows as the number of items a person may have. The rest of the records may be filled with NULL values or whatever else. It has proven to be a pain to solve this problem from the application side, so I've

How can I execute pl/pgsql code without creating a function?

梦想的初衷 提交于 2019-12-02 18:43:55
With SQL Server, I can execute code ad hoc T-SQL code with full procedural logic through SQL Server Management Studio, or any other client. I've begun working with PostgreSQL and have run into a bit of a difference in that PGSQL requires any logic to be embedded in a function. Is there a way to execute PL/PGSQL code without creating an executing a function? chotchki Postgres 9 DO $$ -- declare BEGIN /* pl/pgsql here */ END $$; No, not yet. Version 9.0 (still alpha) will have this option (do), you a have to wait until it's released. I struggled to get this working because it's fairly strict

How do I enable the PostgreSQL function profiler?

北慕城南 提交于 2019-12-02 18:29:10
This took me a while to figure out and I found the answer on a foreign-language wiki a number of weeks ago, and it was very helpful, so I thought I would share. On PostgreSQL 8.3 on Win32, the profiling plugin is installed by default, but not loaded. Just execute this SQL: LOAD '$libdir/plugins/plugin_profiler.dll'; SET plpgsql.profiler_tablename = 'bazzybar'; ...and then when you want to profile some code, drop table if exists bazzybar; -- reset the profiling stats select my_function_here('lala',123); -- this line and variations as many times as you deem fit select * from bazzybar; -- show

PL/pgSQL Array of Rows

╄→гoц情女王★ 提交于 2019-12-02 18:08:17
问题 Is the following possible? I want to have a procedure written in PL/pgSQL that takes as parameter something like "collection of row", I mean I need to pass to function a dictionary-like structure: Pseudocode: function_name({key1:val1,key2:val2, key3:val3 [, ...] }) 回答1: With modern day PostgreSQL you can simplify such a function. Test setup: CREATE TABLE tbl1 (id int, value text); No need to create a type explicitly in this case (if the type is based on a table row), it is created for every

PostgreSQL: How to pass table/view name as a parameter to function in PostgreSQL?

时光总嘲笑我的痴心妄想 提交于 2019-12-02 17:55:49
问题 For example: I have a VIEW called "view1" which contains 'name' and 'slno' columns, now i want it to be display using FUNCTION called "f1" as shown below: --Function create or replace function f1(viewname varchar) returns table (name varchar,slno integer) as $body$ begin return query select * from viewname; end; $body$ language plpgsql; 回答1: This is dynamic SQL, so you need EXECUTE . RETURN QUERY EXECUTE format('SELECT * FROM %I', "name"); Separately, that's a weird thing to want to do. 来源:

How to evaluate expression in select statement in Postgres

笑着哭i 提交于 2019-12-02 17:38:45
问题 Postgres 9.1+ database contains customers and product. In customers table, customer price is described as sql expression in priceexpression column for every customer. How to create price list from this data ? I tried code below but got error since eval() is undefined. create table customer ( id int primary key, priceexpression text ); insert into customer values (1, 'price*0.95'),(2,'cost+12.0' ); create table product ( id char(20) primary key, price numeric(12,4), cost numeric(12,4) );

How to round REAL type to NUMERIC?

安稳与你 提交于 2019-12-02 17:24:58
问题 I have table with real column type with example values: 123456,12 0,12345678 And code in stored procedure: CREATE OR REPLACE FUNCTION test3() RETURNS integer AS $BODY$ DECLARE rec RECORD; BEGIN FOR rec IN SELECT gme.abs_km as km, CAST(gme.abs_km as numeric) as cast, round(gme.abs_km:: numeric(16,2), 2) as round FROM gps_entry gme LOOP RAISE NOTICE 'Km: % , cast: % , round: %', rec.km, rec.cast, rec.round; INSERT INTO test (km, casting, rounding) VALUES (rec.km, rec.cast, rec.round); END LOOP;