plpgsql

How do I use variables in a postgresql function for loop query

半世苍凉 提交于 2021-02-07 18:32:06
问题 I have a rather complicated function in postgresql (Version 9.4.4) that I need a bit of help with. I have a loop (with lots of work below) declared like this inside of my function: CREATE OR REPLACE function getRSI( psymbol varchar, pstarttime timestamp with time zone, pendtime timestamp with time zone, pduration double precision, ptable varchar ) RETURNS SETOF rsi AS $BODY$ declare row_data record; -- some variables begin FOR row_data IN SELECT datetime, value FROM "4" WHERE symbol = 'AAPL'

How do I use variables in a postgresql function for loop query

拈花ヽ惹草 提交于 2021-02-07 18:32:00
问题 I have a rather complicated function in postgresql (Version 9.4.4) that I need a bit of help with. I have a loop (with lots of work below) declared like this inside of my function: CREATE OR REPLACE function getRSI( psymbol varchar, pstarttime timestamp with time zone, pendtime timestamp with time zone, pduration double precision, ptable varchar ) RETURNS SETOF rsi AS $BODY$ declare row_data record; -- some variables begin FOR row_data IN SELECT datetime, value FROM "4" WHERE symbol = 'AAPL'

How to create a trigger function dynamically in pgsql?

我怕爱的太早我们不能终老 提交于 2021-02-07 15:00:10
问题 I want to write a pgsql function to create trigger dynamically. For example, a trigger to count insertions in each table. I've tried EXECUTE like this: CREATE FUNCTION trigen(tbl text) RETURNS void AS $$ BEGIN EXECUTE format( 'CREATE FUNCTION %s_insertCnt() RETURNS TRIGGER AS $$ BEGIN UPDATE insertions SET n = n + 1 WHERE tablename = %s; END $$ LANGUAGE plpgsql', tbl, quote_nullable(tbl)); EXECUTE format('CREATE TRIGGER %s_inCnt BEFORE INSERT ON %s FOR EACH ROW EXECUTE PROCEDURE %s_insertCnt(

Can I have a postgres plpgsql function return variable-column records?

喜欢而已 提交于 2021-02-05 07:52:05
问题 I want to create a postgres function that builds the set of columns it returns on-the-fly; in short, it should take in a list of keys, build one column per-key, and return a record consisting of whatever that set of columns was. Briefly, here's the code: CREATE OR REPLACE FUNCTION reports.get_activities_for_report() RETURNS int[] AS $F$ BEGIN RETURN ARRAY(SELECT activity_id FROM public.activity WHERE activity_id NOT IN (1, 2)); END; $F$ LANGUAGE plpgsql STABLE; CREATE OR REPLACE FUNCTION

PGSQL Trigger Function Write Exception to Log Table

喜欢而已 提交于 2021-01-29 02:53:37
问题 Background I'm new to PostgreSQL and I'm having some issues with this trigger function, which I've obviously simplified considerably below. I could ask to help fix the query, but I think I can handle that, and what I'm more concerned about is that I have a lot of functions like this and I need a way to be able to have visibility into why it's failing, and which ones are failing. Question How can I catch exceptions that happen within this function and write them to some kind of log table so I

Write to a file from RAISE NOTICE in postgresql

时光毁灭记忆、已成空白 提交于 2021-01-28 15:11:06
问题 I have one sample function in postgresql and it raises a notice. Sample function - CREATE OR REPLACE FUNCTION raise_test() RETURNS TEXT AS $body$ DECLARE retStr TEXT; BEGIN SELECT current_timestamp into retStr; RAISE NOTICE '%', retStr ; RETURN retStr; END; $body$ LANGUAGE plpgsql; Is there any way to update above function so that the entire notice stored into a file? Like if I hit "call raise_test();" and in my specfic location I 'll have one out.txt with the entire notice printed. PS. I hv

Write to a file from RAISE NOTICE in postgresql

我是研究僧i 提交于 2021-01-28 14:59:38
问题 I have one sample function in postgresql and it raises a notice. Sample function - CREATE OR REPLACE FUNCTION raise_test() RETURNS TEXT AS $body$ DECLARE retStr TEXT; BEGIN SELECT current_timestamp into retStr; RAISE NOTICE '%', retStr ; RETURN retStr; END; $body$ LANGUAGE plpgsql; Is there any way to update above function so that the entire notice stored into a file? Like if I hit "call raise_test();" and in my specfic location I 'll have one out.txt with the entire notice printed. PS. I hv

Best practice to identify a jsonb null in plpgsql

删除回忆录丶 提交于 2021-01-28 11:47:16
问题 I'm aware that variants of this question have been asked before: Why can't NULL be converted to JSON's null in postgreSQL? Why does JSON null not cast to SQL null in postgres? perhaps others... What I wasn't able to glean from the above links is whether there is a best practice. Consider the following code: DO $$ DECLARE _main_jsonb jsonb = '{"i_am_null": null, "a_string": "null"}'; _sub_jsonb jsonb; BEGIN SELECT (_main_jsonb->'i_am_null') INTO _sub_jsonb; IF _sub_jsonb IS NULL THEN RAISE

Postgresql: How to use curl with function

笑着哭i 提交于 2021-01-28 08:20:22
问题 I have a function parse_sdf(text) . When I call the function like this: INSERT INTO test.table("Id", "Test") (SELECT props -> 'Id', props -> 'Test' FROM parse_sdf()); When I execute the INSERT with pgsql I want to pass as a parameter a text file which I download via curl . Like this: INSERT INTO test.table("Id", "Test") (SELECT props -> 'Id', props -> 'Test' FROM parse_sdf('curl http://test/Files/text.txt')); But it doesn't work. Is there a way to accomplish this? UPDATE: Postgres version: 10

select statement in postgres function called inside a trigger

╄→гoц情女王★ 提交于 2021-01-28 08:15:15
问题 I'm trying to develop a notification system for the backend of a social media application/website. For now I'm focusing on status updates. What I'm going to do, is putting a trigger on the postgres table that related to status updates so that every time a new status update is posted, a notification is sent to my code. So far I have been able to do that. But an extra feature that I like to implement is extracting all of the people who follow the user who posted the status update, so that I can