plpgsql

Concurrency Postgresql UPDATE

为君一笑 提交于 2019-12-25 00:15:02
问题 I have this situation with thread process. Have table like example : id | type | external_key | balance | amount | date 1 TOPUP MT1 10 2019-01-1 13:01:00.500110 2 USAGE MT1 -1 2019-01-1 13:01:01.300100 3 TOPUP MT3 5 2019-01-1 13:01:02.400300 every new data on each row i run this trigger to update field balance to store current balance every row : create or replace function function_update_balance() returns trigger language plpgsql as $$ BEGIN IF new.type = 'CBA_ADJ' THEN update example set

PostgreSQL: column names into array PL/pgSQL

眉间皱痕 提交于 2019-12-24 22:03:32
问题 create or replace function extr( tabname text ) returns text[] as $$ declare cols text[]; begin execute 'array(select column_name::text from information_schema.columns where table_name = '||quote_literal(tabname)||');' into cols; return cols; end; $$ language 'plpgsql'; select extr('test'); One supplies a table name and wants back its column-names as an array. The above code gives 'syntax error at or near "array"'. How to fix this? 回答1: The query should start with select , not array and it

FOR loop on PLpgSQL function result

半世苍凉 提交于 2019-12-24 13:12:23
问题 I wrote a PLpgSQL function which should return SETOF products table: CREATE OR REPLACE FUNCTION get_products_by_category (selected_category_id smallint DEFAULT 1) RETURNS SETOF products AS $BODY $BEGIN RETURN QUERY (SELECT * FROM products WHERE CategoryID = selected_category_id); END; $BODY$ LANGUAGE plpgsql VOLATILE NOT LEAKPROOF COST 100 ROWS 1000; And next I want to iterate over results in another function (non-finished view, because I try to add in PgAdmin III and I have errors): DECLARE

Function to return a table of all children of a node

╄→尐↘猪︶ㄣ 提交于 2019-12-24 10:45:16
问题 Let's say I've got the following table structure: | ID | ParentID | Name | I'd like to write a recursive PostgreSQL function for getting all child nodes of a node ID passed to it as a parameter. Here's my code so far (I only have one part of the function which gets all the children of the passed ID, and now I need the recursive part): CREATE OR REPLACE FUNCTION GetAllChildren(IN NodeID INTEGER) RETURNS INTEGER AS $$ DECLARE Crs CURSOR FOR SELECT ID, ParentID, Name FROM Tree WHERE ParentID

How to put part of a SELECT statement into a Postgres function

醉酒当歌 提交于 2019-12-24 09:57:26
问题 I have part of a SELECT statement that is a pretty lengthy set of conditional statements. I want to put it into a function so I can call it much more efficiently on any table I need to use it on. So instead of: SELECT itemnumber, itemname, base, CASE WHEN labor < 100 AND overhead < .20 THEN WHEN ..... WHEN ..... WHEN ..... ..... END AS add_cost, gpm FROM items1; I can just do: SELECT itemnumber, itemname, base, calc_add_cost(), gpm FROM items1; Is it possible to add part of a SELECT to a

Race Condition between SELECT and INSERT for multiple columns

允我心安 提交于 2019-12-24 08:59:08
问题 Note: This is a question which is a follow up of this solution. You need to read the link to get context for this question. Also, this is for postgres v9.4 If we want to return multiple columns now instead of just 1 column, how can we achieve it? Let's take a table t: create table t(tag_id int, tag text unique); Now this is what I want: whenever I call a method f_tag_id, I want it to return all the columns for the unique row if it exists in the table t else insert it and return all the

PostgreSQL performance of ad-hoc SQL vs functions

给你一囗甜甜゛ 提交于 2019-12-24 06:29:28
问题 Is there any difference? I know SQL queries are having their execution plans cached just as good as functions. I foud someone telling: Performance is an issue, and we suspect query planning might be an underlying cause. I've rewritten the scripts from ad-hoc SQL to a Postgres functions (CREATE FUNCTION) and we saw server load go down quite a bit. But why? 回答1: The query plan for ad-hoc queries is not cached, only for prepared statements. And PL/pgSQL functions handle all SQL statements like

Replace double quotes with single quotes in Postgres (plpgsql)

荒凉一梦 提交于 2019-12-24 04:57:09
问题 I am building a trigger function in plpgsql for my partitioned table and I've got all of my logic working, but am having trouble inserting the actual record into my table. I have to reference my specific table by a variable reference, so that (as far as I understand) forces me to use an EXECUTE command, as so: EXECUTE 'INSERT INTO ' || tablename || ' VALUES ' || NEW.*; However, this does not handle unpacking the record stored in NEW in a way that Postgres' INSERT function can understand. It

PostgreSQL transaction restart

狂风中的少年 提交于 2019-12-24 03:02:14
问题 I'm starting to play with PostgreSQL and noticed that sequence s never rollback, even on failed INSERT . I've read that it is as expected to prevent duplicated sequences on concurrent transactions and I found that weird as my database experience is only with GTM where transaction restarts are common and used precisely for this. So I wanted to test restarts in PGSQL and loaded this in a database: CREATE SEQUENCE account_id_seq; CREATE TABLE account ( id integer NOT NULL DEFAULT nextval(

Allow insertion only from within a trigger

自古美人都是妖i 提交于 2019-12-24 02:09:05
问题 I'm new to SQL programming, and I couldn't find an answer to this question online. I'm working with pl/pgsql and I wish to achieve the following result: I have a table A with certain attributes. I am supposed to keep this table updated at any time - thus whenever a change was made that can affect A's values (in other tables B or C which are related to A) - a trigger is fired which updates the values (in the process - new values can be inserted into A, as well as old values can be deleted). At