plpgsql

Count rows affected by DELETE

两盒软妹~` 提交于 2019-12-20 05:58:03
问题 I use this code to verify the DELETE sentence, but I am sure you know a better way: CREATE OR REPLACE FUNCTION my_schema.sp_delete_row_table(table_name character varying , id_column character varying , id_value integer) RETURNS integer AS $BODY$ DECLARE BEFORE_ROWS integer; AFTER_ROWS integer; BEGIN EXECUTE 'SELECT count(*) FROM ' || TABLE_NAME INTO BEFORE_ROWS; EXECUTE 'DELETE FROM ' || TABLE_NAME || ' WHERE ' || ID_COLUMN || ' = ' || (ID_VALUE)::varchar; EXECUTE 'SELECT count(*) FROM ' ||

Oracle DBMS_LOB.WRITEAPPEND to Postgres Conversion

人走茶凉 提交于 2019-12-20 05:45:26
问题 Can someone please let me know how to convert the below Oracle Code to Postgresql IF prodNum = 1 THEN DBMS_LOB.WRITEAPPEND(pkgFilterNode, LENGTH(pkgFilter_tab || '<PackageFilters isNewFormat="Y" > '||l_crlf), pkgFilter_tab || '<PackageFilters isNewFormat="Y" > '||l_crlf); END IF; Appreciate your time! 回答1: It depends on a size of your large object. When your large objects are less about 500MB, then you don't need to use LOBs (PostgreSQL uses term LO), and you can use a text or varchar type -

How can I write to a file on disk from PL/pgSQL?

送分小仙女□ 提交于 2019-12-20 05:27:31
问题 I would like to do the equivalent of c or php fopen() and fwrite(). I am not trying to dump a table to disk. I am trying to do some debug logging during development. 回答1: You can RAISE NOTICE or DEBUG messages in a plpgsql function or a DO statement which are written to the DB log file. 回答2: You can use plpythonu f.open(), f.write(), f.close() within a postgres function to write to a file. Language extension would need to be installed., https://www.postgresql.org/docs/8.3/static/plpython.html

How to pass NEW.* to EXECUTE in trigger function

≡放荡痞女 提交于 2019-12-20 03:51:42
问题 I have a simple mission is inserting huge MD5 values into tables (partitioned table), and have created a trigger and also a trigger function to instead of INSERT operation. And in function I checked the first two characters of NEW.md5 to determine which table should be inserted. DECLARE tb text; BEGIN IF TG_OP = 'INSERT' THEN tb = 'samples_' || left(NEW.md5, 2); EXECUTE(format('INSERT INTO %s VALUES (%s);', tb, NEW.*)); <- WRONG END IF; RETURN NULL; END; The question is how to concat the NEW.

Update multiple columns that start with a specific string

我与影子孤独终老i 提交于 2019-12-20 03:33:08
问题 I am trying to update a bunch of columns in a DB for testing purposes of a feature. I have a table that is built with hibernate so all of the columns that are created for an embedded entity begin with the same name. I.e. contact_info_address_street1 , contact_info_address_street2 , etc. I am trying to figure out if there is a way to do something to the affect of: UPDATE table SET contact_info_address_* = null; If not, I know I can do it the long way, just looking for a way to help myself out

How to get the list of functions in database in PostgreSQL along with its parameters?

随声附和 提交于 2019-12-20 02:46:34
问题 I have a database which has over 1000 functions. I want to GRANT execution permission to certain users. I can't do it manually, therefore I want to get a list of functions and write a code to generate the GRANT EXECUTE script for all the functions. I tried the following script but this is not giving me the parameters. I am unable to get the parameters with this query. SELECT 'GRANT EXECUTE ON FUNCTION '||nspname||'.'||proname||' TO gis;' FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_proc

Drop or create database from stored procedure in PostgreSQL

早过忘川 提交于 2019-12-20 02:16:11
问题 I have a function that looks like this: BEGIN DROP DATABASE IF EXISTS db_1; END; I'm getting the following error: ERROR: DROP DATABASE cannot be executed from a function or multi-command string. Is it not possible to drop a database from a stored procedure in PostgreSQL? I'm using plpgsql. 回答1: The error message is just a s clear as the manual on this: DROP DATABASE cannot be executed inside a transaction block. A plgpsql function is surrounded by a transaction block automatically. The long

How to programmatically check if row is deletable?

吃可爱长大的小学妹 提交于 2019-12-19 19:45:50
问题 Say we have a PostgreSQL table like so: CREATE TABLE master ( id INT PRIMARY KEY, ... ); and many other tables referencing it with foreign keys: CREATE TABLE other ( id INT PRIMARY KEY, id_master INT NOT NULL, ... CONSTRAINT other_id_master_fkey FOREIGN KEY (id_master) REFERENCES master (id) ON DELETE RESTRICT ); Is there a way to check (from within trigger function) if a master row is deletable without actually trying to delete it? The obvious way is to do a SELECT on all referencing tables

Getting name of the current function inside of the function with plpgsql

荒凉一梦 提交于 2019-12-19 14:55:31
问题 Is there anyway from within a plpgsql function that you can get the name of the function? Or even the OID of the function? I know there are some "special" variables (such as FOUND) within plpgsql, but there doesn't seem to be any way of getting this. (Although, I've read where it seems to be possible if your function is written in C). It's not critical, but it would make something I'm doing a little nicer/less fragile. I'm using PostgreSQL v. 9.1.5 回答1: For triggers use TG_NAME to get the

SELECT or PERFORM in a PL/pgSQL function

大兔子大兔子 提交于 2019-12-19 13:28:09
问题 I have this function in my database: CREATE OR REPLACE FUNCTION "insertarNuevoArticulo"(nombrearticulo character varying, descripcion text, idtipo integer, idfamilia bigint, artstock integer, minstock integer, maxstock integer, idmarca bigint, precio real, marcastock integer) RETURNS boolean AS $BODY$ DECLARE articulo "Articulo"%ROWTYPE; BEGIN SELECT * INTO articulo FROM "Articulo" WHERE "Nombre" = $1 AND "idTipo"=$3 AND "idFamilia"=$4; IF NOT FOUND THEN INSERT INTO "Articulo" ("Nombre",