plpgsql

How can I send some http request from postgresql function or trigger

拟墨画扇 提交于 2020-01-09 07:42:26
问题 I need to send data via http protocol (GET or POST request) from the function or trigger. Is it possible? 回答1: You could try writing the trigger in PL/Python and use urllib2 to POST. 回答2: There is an extension to do this, use with caution. pgsql-http 回答3: Any "untrusted" language with HTTP support can do this: PL/Pythonu PL/perlu PL/javau ... but you shouldn't really do it. See Does PLV8 support making http calls to other servers? and why you shouldn't send email from a trigger function. 来源:

Postgres function returning table not returning data in columns

人盡茶涼 提交于 2020-01-08 21:51:15
问题 I have a Postgres function which is returning a table: CREATE OR REPLACE FUNCTION testFunction() RETURNS TABLE(a int, b int) AS $BODY$ DECLARE a int DEFAULT 0; DECLARE b int DEFAULT 0; BEGIN CREATE TABLE tempTable AS SELECT a, b; RETURN QUERY SELECT * FROM tempTable; DROP TABLE tempTable; END; $BODY$ LANGUAGE plpgsql; This function is not returning data in row and column form. Instead it returns data as: (0,0) That is causing a problem in Coldfusion cfquery block in extracting data. How do I

Postgres function returning table not returning data in columns

我的未来我决定 提交于 2020-01-08 21:47:27
问题 I have a Postgres function which is returning a table: CREATE OR REPLACE FUNCTION testFunction() RETURNS TABLE(a int, b int) AS $BODY$ DECLARE a int DEFAULT 0; DECLARE b int DEFAULT 0; BEGIN CREATE TABLE tempTable AS SELECT a, b; RETURN QUERY SELECT * FROM tempTable; DROP TABLE tempTable; END; $BODY$ LANGUAGE plpgsql; This function is not returning data in row and column form. Instead it returns data as: (0,0) That is causing a problem in Coldfusion cfquery block in extracting data. How do I

Postgres usage of format()

 ̄綄美尐妖づ 提交于 2020-01-07 02:52:07
问题 Is the usage of format() in cases like this generally interchangeable? exec_str := format('UPDATE ' || TG_ARGV[0] || ' SET username = current_user, time = current_timestamp::timestamp(0);' ); EXECUTE exec_str; vs. exec_str := 'UPDATE ' || TG_ARGV[0] || ' SET username = current_user, time = current_timestamp::timestamp(0);' ; EXECUTE format(exec_str); 回答1: The primary benefit of the function format() is that you can use parameters: execute format(' UPDATE %I SET username = current_user, time =

Select columns with the same prefix [duplicate]

依然范特西╮ 提交于 2020-01-06 17:27:53
问题 This question already has answers here : Use text output from a function as new query (2 answers) Closed 4 years ago . Using the following code I can select a few columns that share the same prefixes (either upreg_srt or downreg_srt) from my table and delete (drop) them: DO $do$ DECLARE _column TEXT; BEGIN FOR _column IN SELECT DISTINCT quote_ident(column_name) FROM information_schema.columns WHERE table_name = 'all_se_13patients_downreg_ranks' AND column_name LIKE '%upreg_srt' OR column_name

Select columns with the same prefix [duplicate]

女生的网名这么多〃 提交于 2020-01-06 17:27:25
问题 This question already has answers here : Use text output from a function as new query (2 answers) Closed 4 years ago . Using the following code I can select a few columns that share the same prefixes (either upreg_srt or downreg_srt) from my table and delete (drop) them: DO $do$ DECLARE _column TEXT; BEGIN FOR _column IN SELECT DISTINCT quote_ident(column_name) FROM information_schema.columns WHERE table_name = 'all_se_13patients_downreg_ranks' AND column_name LIKE '%upreg_srt' OR column_name

Iterating on each element from an array of arrays on Postgresql

若如初见. 提交于 2020-01-06 15:08:11
问题 I have a VARCHAR of numbers inside my stored procedure, these numbers are organized as arrays, I will show an example below: {1,2,3,4,5,6,7,8,9},{1,2,3,4,5},{1,2,3},{9} -- This is a STRING Now with a help from another guy from here I'm using this to get integer arrays integer[] SELECT string_to_array(regexp_split_to_table( trim('{1,2,3,4,5,6,7,8,9},{1,2,3,4,5},{1,2,3},{9}', '{}') , '},{'), ',')::int[] I will have a set of records, each of them with an array, see below: {1,2,3,4,5,6,7,8,9} {1

PostgreSQL Trigger Exception

旧城冷巷雨未停 提交于 2020-01-06 14:13:44
问题 I'm having problems with creating this trigger in PostgreSQL 8.4. CREATE OR REPLACE FUNCTION tbi_Usuarios() RETURNS TRIGGER AS $tbi_Usuarios$ BEGIN IF trim(both ' ' from NEW.Nombre_usuario) = '' OR NEW.Nombre_usuario IS NULL THEN RAISE EXCEPTION 'Debes ingresar un nombre de usuario.'; END IF; IF NEW.Password = '' OR NEW.Password IS NULL THEN RAISE EXCEPTION 'Debes ingresar una contraseña correctamente'; ELSE NEW.Password := md5(NEW.Password); END IF; IF Fecha_registro IS NULL THEN NEW.Fecha

Counting number of updates

。_饼干妹妹 提交于 2020-01-06 05:47:09
问题 Hi Every one I created this trigger function to count number of rows affected by an update . create table smt ( id serial primary key, num int ) CREATE OR REPLACE FUNCTION count_updated() RETURNS trigger LANGUAGE 'plpgsql' AS $BODY$ DECLARE n int; BEGIN IF(TG_OP = 'UPDATE') THEN get diagnostics n = row_count; insert into smt (num) values (n); return null; END IF; END; $BODY$; CREATE TRIGGER count_updt AFTER UPDATE ON test FOR EACH ROW EXECUTE PROCEDURE count_updated(); what I want to do is

Loop in function does not work as expected

丶灬走出姿态 提交于 2020-01-05 21:10:23
问题 Using PostgreSQL 9.0.4 Below is a very similar structure of my table: CREATE TABLE departamento ( id bigserial NOT NULL, master_fk bigint, nome character varying(100) NOT NULL CONSTRAINT departamento_pkey PRIMARY KEY (id), CONSTRAINT departamento_master_fk_fkey FOREIGN KEY (master_fk) REFERENCES departamento (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) And the function I created: CREATE OR REPLACE FUNCTION fn_retornar_dptos_ate_raiz(bigint[]) RETURNS bigint[] AS $BODY$ DECLARE