Postgres: How do I save payload content to a table or a csv file? - postgresql

How do I save payload content to a table or a csv file that can be picked up by a python script or a client to save it in another DB?
Example taken from another post:
CREATE FUNCTION column_trigger_func()
RETURNS TRIGGER AS $$
BEGIN
PERFORM pg_notify('columnchannel', 'Id: '||OLD."columnID");
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
How to save pg_notify payload values to a table or a csv file??

Related

How I can call the output in the refcursor using Stored procedure in PostgreSQL?

I want to output in refcursor but it's not working.
I tried a lot of thing to get output in refcursor and sometimess its showing <Unnamed Portal 1>
CREATE OR REPLACE PROCEDURE "MasterUser".s_m_userapplication(
v_app_no text,
INOUT v_refcur refcursor DEFAULT 'rs_resultone'::refcursor)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
open v_refcur for select AM_App_ProcCD,AM_App_ProcDt
from "MasterUser".Master_Application
where "MasterUser".appnov1(CAST(AM_AAPP_NO_TYPE AS INTEGER), AM_AAPP_NO_YEAR, AM_AAPP_NO_MONTH, AM_AAPP_NO_SEQ) =(v_App_No);
END;
$BODY$;
ALTER PROCEDURE "MasterUser".s_m_userapplication(text, refcursor)
OWNER TO USERDBA;
When you are using INOUT refcursor you will be viewing always one column and one record using the call my_procedure(). Inside the column value you can view your selecting records. (See picture) If you're getting data from the cursor on the backend, you don't care about the IDE view. On the backend side this will work. If you need to view data like as select * from table then you must use function instead of procedure.

Postgres: syntax error when trying to create multiple stored procedures from a single file

I am using Postgres version 13.1. I want to have a single file where I could store all stored procedures and create them in one shot. But when I put multiple stored procedures in a single file, I get the following error. What am I missing?
In my test.sql file I have the following content:
create or replace procedure tmp1(
)
language plpgsql as
$$
declare
l_count integer;
begin
select 1 into l_count;
raise info 'count: %', l_count;
end;
$$
create or replace procedure tmp2(
)
language plpgsql as
$$
declare
l_count integer;
begin
select 1 into l_count;
raise info 'count: %', l_count;
end;
$$
If I only have the first procedure it gets created and I can call it successfully. But the moment I have the second identical procedure with a different name, it gives me an error as follows (when run from psql):
psql=> \i test.sql
psql:test.sql:23: ERROR: syntax error at or near "create"
LINE 12: create or replace procedure tmp2(
OK - right after posting I tried putting a ";" after the end of first procedure and it works

Create temp table in a STABLE stored procedure in postgresql

I would like to create a temp table in a stored procedure which has a STABLE volatility category setted to store the result of a select for later usage in the stored procedure. At the end of the stored procedure this temp table is deallocated and i am sure that this temp table does not have any affect on the database, because as far as i know with this volatility category i ensure the optimizer that this stored procedure will not affect the database.
So i would like to do something like this:
Create a stored procedure which returns with a query:
CREATE OR REPLACE FUNCTION storedproc()
RETURNS TABLE
(Egy TEXT,
Ketto TEXT)
AS $$
BEGIN
RETURN QUERY SELECT * FROM temptable;
END;
$$ LANGUAGE plpgsql;
Create a stored procedure which is using the previous query:
CREATE OR REPLACE FUNCTION stablefunction()
RETURNS TABLE
(Egy TEXT,
Ketto TEXT)
AS $$
BEGIN
-- I would like to store the results here for later usage
CREATE TEMP TABLE buba AS select * from storedproc();
-- Do other stuff
-- ...
-- Reuse the results here which was stored before
END;
$$ LANGUAGE plpgsql
STABLE;
But when i want to execute this stored procedure as this:
DO
$$
BEGIN
perform stablefunction() ;
END;
$$ LANGUAGE plpgsql;
i get the following error message:
ERROR: CREATE TABLE AS is not allowed in a non-volatile function
Maybe this is not the intended usage of the stored procedures, but then is there a way for store the result of a query inside of the stored procedure for later usage in the same stored procedure, maybe like a handle or somethings?
The documentation states clearly: A stable function cannot modify the database. A temporary table is a part of a database as well, so you cannot create it, insert into, delete from etc. Your concept seems a bit strange but I don't want to judge it. There is a trick that allows what you want to do. Perfom all actions on the temp table using other functions that do not have to be stable. Example:
create or replace function create_my_temp_table()
returns void language plpgsql volatile as $$
begin
create temp table temp_table(id int);
insert into temp_table values (123);
end $$;
create or replace function stable_function()
returns text language plpgsql stable as $$
begin
perform create_my_temp_table();
return 'ok';
end $$;
Test:
select stable_function();
stable_function
-------------
ok
(1 row)
select * from temp_table;
id
-----
123
(1 row)

generic trigger to capture table data for audit in postgres

Need help to create a generic trigger to log all tables.
i have table named "system" and need to log it.
the log table name system_audit is created with all columns of "system" table along with additional three columns named
modified_dt,modified_by and modified_type.
modified_dt will be current_timestamp
modified_by will be the user
and modified_type specifies whether its insert,update or delete.(Need to capture new data for insert/update and old for delete)
How to write a function to capture the above said data. Also it needs to be dynamics, so that i can use it across all the tables in my schema
Note: All audit tables contains the modified_dt,modified_by and modified_type as mandatory.
I got a couple of codes from the net, but it is not working, I was working on with oracle before, and new to postgres, not sure on how to code it properly.Please help
I have managed to create a generic function and its working fine.Thanks for the help.
create or replace function audit.fn__audit()
returns trigger as
$func$
declare
col_name text:='';
audit_table_name text := TG_TABLE_NAME || '_audit';
begin
if TG_OP = 'UPDATE' or TG_OP = 'INSERT' THEN
EXECUTE format('INSERT INTO audit.%1$I SELECT ($1).*,current_timestamp,user,'''||TG_OP||'''',audit_table_name) using NEW;
else
EXECUTE format('INSERT INTO audit.%1$I SELECT ($1).*,current_timestamp,user,'''||TG_OP||'''',audit_table_name) using old;
end if;
return new;
END $func$
LANGUAGE plpgsql VOLATILE;

Postgresql - remove unwanted characters from data stream using stored procedure or triggers?

I have a data stream coming from a machine and this is stored in a Postgresql DB. I need to strip out various unwanted characters and keep both the original result and the new result. eg I have data "34.5 !*" or "17.9 P-" in a field and want to store "34.5" or "17.9" . I was wondering about using a trigger to call a procedure to write the data minus the unwanted characters to a new field...
You can easily do that in a trigger with a regular expression, something like:
create or replace function clean_value()
returns trigger
language plpgsql
AS
$body$
begin
new.clean_column = regexp_replace(new.dirty_column, '[^0-9\.]', '', 'g');
return new;
end;
$body$
/
That will store a "clean" version of the input dirty_column into the column clean_column.