plpgsql

Debug PostgreSQL function using pgAdmin

孤街醉人 提交于 2019-12-02 16:28:37
I refer this to enable the debugger in the PostgreSQL server in order to debugging the plpgsql function by stepping through the codes using the pgadmin. I have already set shared_preload_libraries = '$libdir/plugins/plugin_debugger.dll' in the postgresql.conf , run pldbgapi.sql , and restarted the server. These steps should have been run successfully and plugin_debugger.dll should be loaded successfully as can be verified by using the command show shared_preload_libraries , and I can see the debugging option in the context menu with a right click on a function in pgAdmin. When choosing

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

这一生的挚爱 提交于 2019-12-02 15:06:28
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. Erwin Brandstetter 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 attnum >= 1 -- exclude tableoid & friends AND NOT attisdropped -- exclude dropped

dblink can't update a table on the same database in an after UPDATE trigger

这一生的挚爱 提交于 2019-12-02 14:36:10
问题 I am working on a database replicated using slony and trying to create a trigger which will be triggered after an INSERT operation on a table. In this trigger I am trying to update another table of THE SAME database using dblink. But I am getting an error saying that the value I just inserted in the first table does not exist when I am trying to update the second table. I am using dblink because if I update the second table with a regular UPDATE statement, slony synchronization is not

Dynamic auditing of data with PostgreSQL trigger

℡╲_俬逩灬. 提交于 2019-12-02 13:41:26
问题 I'm interested in using the following audit mechanism in an existing PostgreSQL database. http://wiki.postgresql.org/wiki/Audit_trigger but, would like (if possible) to make one modification. I would also like to log the primary_key's value where it could be queried later. So, I would like to add a field named something like "record_id" to the "logged_actions" table. The problem is that every table in the existing database has a different primary key fieldname. The good news is that the

Speed up plpgsql that counts doc types in a loop?

瘦欲@ 提交于 2019-12-02 13:08:56
问题 Is there a way to speed up our plpgsql function that counts certain types of docs all in one query which is executed in a loop? ALL in one query? validador := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 901); validador2 := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 902); validador3 := (select count(id_doc) from webdte.doc_tip_cifra where id_doc = id_documento and id_tipo_cifra = 905);

Postgres Function End Loop and return Error

為{幸葍}努か 提交于 2019-12-02 13:04:55
问题 I have tried to create this function but the system is returning a "LOOP error" and I don't know how to return 3 variables at the same time. I've tried hard to figure this out but I didn't find an answer anywhere. CREATE OR REPLACE FUNCTION conta_relatos(fator_normativo integer, fator_determinativo integer) RETURNS integer AS $BODY$ DECLARE vinculos_encontrados RECORD; rel_pri INT; rel_sec INT; rel_ref INT; no_item INT; tipo_relato TEXT; BEGIN rel_pri := 0; rel_sec := 0; rel_ref := 0; FOR

SQLSTATE and GET STACKED DIAGNOSTICS information in another function?

陌路散爱 提交于 2019-12-02 12:47:12
问题 In one plpgsql function I have exception block like this: exception when others then txt := 'error text '; perform record_error(id, table, txt); And when exception occurs I want to perform another function, which adds information about error to log table like this: CREATE OR REPLACE FUNCTION record_error( id integer, layer text, message text) RETURNS void AS $BODY$ DECLARE l_code integer:= 'sqlstate' l_mesg varchar:= 'SQLERRM'; l_context text; l_detail text; BEGIN GET STACKED DIAGNOSTICS l

How to round REAL type to NUMERIC?

烈酒焚心 提交于 2019-12-02 12:22:55
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; RETURN 1; END; $BODY$ LANGUAGE 'plpgsql' VOLATILE; Here is output: 2014-02-05 12:49:53 CET NOTICE: Km:

PL/PgSQL calling a function inside a loop giving error

给你一囗甜甜゛ 提交于 2019-12-02 11:56:44
问题 The code bellow is giving error on w_add_ax_extra(1, 'k', 'v') previously it was w_add_ax_extra(some_id, kv.k, kv.v) I changed it to k, v to reproduce the same error declare kv record; begin -- Lines skipped for kv in select * from (select (each(extras)).*) as f(k,v) loop raise notice 'key=%,value=%',kv.k,kv.v; w_add_ax_extra(1, 'k', 'v'); end loop; -- Lines Skipped end I am getting Syntax Error but could not understand what I am missing ERROR: syntax error at or near "w_add_ax_extra" LINE 1:

dblink can't update a table on the same database in an after UPDATE trigger

主宰稳场 提交于 2019-12-02 10:14:29
I am working on a database replicated using slony and trying to create a trigger which will be triggered after an INSERT operation on a table. In this trigger I am trying to update another table of THE SAME database using dblink. But I am getting an error saying that the value I just inserted in the first table does not exist when I am trying to update the second table. I am using dblink because if I update the second table with a regular UPDATE statement, slony synchronization is not triggered. First table: CREATE TABLE "COFFRETS" ( "NUM_SERIE" character varying NOT NULL, "DATE_CREATION"