plpgsql

Calculate number of rows affected by batch query in PostgreSQL

时光毁灭记忆、已成空白 提交于 2019-12-01 18:53:32
First of all, yes I've read documentation for DO statement :) http://www.postgresql.org/docs/9.1/static/sql-do.html So my question: I need to execute some dynamic block of code that contains UPDATE statements and calculate the number of all affected rows. I'm using Ado.Net provider. In Oracle the solution would have 4 steps: add InputOutput parameter "N" to command add BEGIN ... END; to command add :N := :N + sql%rowcount after each statement. It's done! We can read N parameter from command, after execute it. How can I do it with PostgreSQL? I'm using npgsql provider but can migrate to devard

Set a default return value for a Postgres function

做~自己de王妃 提交于 2019-12-01 18:26:37
I have the following function in Postgres: CREATE OR REPLACE FUNCTION point_total(user_id integer, gametime date) RETURNS bigint AS $BODY$ SELECT sum(points) AS result FROM picks WHERE user_id = $1 AND picks.gametime > $2 AND points IS NOT NULL; $BODY$ LANGUAGE sql VOLATILE; It works correctly, but when a user starts out and has no points, it very reasonably returns NULL. How can I modify it so that it returns 0 instead. Changing the body of the function to that below results in an "ERROR: syntax error at or near "IF". SELECT sum(points) AS result FROM picks WHERE user_id = $1 AND picks

pg_stat_activity doesn't update within a procedure or transaction

匆匆过客 提交于 2019-12-01 18:15:40
I query pg_stat_activity but the contents seem to stay the same after the first time I query it within a plpgsql function body, SERIALIZABLE or REPEATABLE READ transaction. Why does pg_stat_activity not follow the rules for READ COMMITTED in a plpgsql procedure? How do I get the current status? I want to loop over a query against pg_stat_activity in plpgsql until another query running on another backend finishes. PostgreSQL makes a per-backend (per-connection, effectively) cache of the data used by the pg_stat_get_activity() function used by both pg_stat_activity and pg_stat_replication . This

Set a default return value for a Postgres function

我的未来我决定 提交于 2019-12-01 17:36:10
问题 I have the following function in Postgres: CREATE OR REPLACE FUNCTION point_total(user_id integer, gametime date) RETURNS bigint AS $BODY$ SELECT sum(points) AS result FROM picks WHERE user_id = $1 AND picks.gametime > $2 AND points IS NOT NULL; $BODY$ LANGUAGE sql VOLATILE; It works correctly, but when a user starts out and has no points, it very reasonably returns NULL. How can I modify it so that it returns 0 instead. Changing the body of the function to that below results in an "ERROR:

How do I create a nested function in PL/pgSQL?

我是研究僧i 提交于 2019-12-01 17:02:18
问题 I would like to create a function in PL/pgSQL with a couple of nested (or inner) functions within it. This way I can break the problem down into smaller pieces but not have my smaller pieces accessible outside of this function. Is it possible to do this in PL/pgSQL? If so, how? 回答1: Nested functions are not supported by PLpgSQL. The emulation has not any sense and it is nonproductive. 回答2: Try it: CREATE OR REPLACE FUNCTION outer() RETURNS void AS $outer$ DECLARE s text; BEGIN CREATE OR

SELECT or PERFORM in a PL/pgSQL function

柔情痞子 提交于 2019-12-01 16:30:33
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", "Descripcion", "idTipo", "idFamilia", "Stock", "MinStock", "MaxStock") Values ($1, $2, $3, $4, $5, $6, $7);

pg_stat_activity doesn't update within a procedure or transaction

▼魔方 西西 提交于 2019-12-01 16:28:28
问题 I query pg_stat_activity but the contents seem to stay the same after the first time I query it within a plpgsql function body, SERIALIZABLE or REPEATABLE READ transaction. Why does pg_stat_activity not follow the rules for READ COMMITTED in a plpgsql procedure? How do I get the current status? I want to loop over a query against pg_stat_activity in plpgsql until another query running on another backend finishes. 回答1: PostgreSQL makes a per-backend (per-connection, effectively) cache of the

Inserting a COALESCE(NULL,default)

China☆狼群 提交于 2019-12-01 16:24:19
I have tables that use UUIDs. I want to be able to insert a new row with or without a UUID as sometimes the client will generate the UUID other times it won't. Each table has this at it's core: CREATE TABLE IF NOT EXISTS person ( id UUID PRIMARY KEY DEFAULT gen_random_uuid() ); I'm trying to use a function to insert rows. I'd like to be able to hand a NULL id and get a default value (a generated UUID). I have something like this: CREATE OR REPLACE FUNCTION create_person( id UUID ) RETURNS BOOLEAN LANGUAGE plpgsql SECURITY DEFINER AS $$ BEGIN INSERT INTO person( id ) VALUES ( COALESCE(id

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

Deadly 提交于 2019-12-01 15:34:20
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 For triggers use TG_NAME to get the name of the trigger (not the trigger function) fired. You also have current_query() to get the top level query

Inserting a COALESCE(NULL,default)

你离开我真会死。 提交于 2019-12-01 15:11:17
问题 I have tables that use UUIDs. I want to be able to insert a new row with or without a UUID as sometimes the client will generate the UUID other times it won't. Each table has this at it's core: CREATE TABLE IF NOT EXISTS person ( id UUID PRIMARY KEY DEFAULT gen_random_uuid() ); I'm trying to use a function to insert rows. I'd like to be able to hand a NULL id and get a default value (a generated UUID). I have something like this: CREATE OR REPLACE FUNCTION create_person( id UUID ) RETURNS