plpgsql

Replace looping with a single query for INSERT / UPDATE

让人想犯罪 __ 提交于 2019-12-24 00:56:16
问题 I am writing a function in PostgreSQL. It does basically 3 steps: Fetch a record from source table. check the value from the fetched record in target table, if record is found in target table then update all values of target table with fetched record otherwise insert fetched record to target table. Instead of doing this looping, if I write single query for insert/update, will it be faster than above mentioned approach? How can I achieve same result by writing single query instead looping

SELECT INTO with SELECT FOR UPDATE in PostgreSQL

徘徊边缘 提交于 2019-12-23 20:06:19
问题 Assume I have a message relation where I save messages that was created with this command: CREATE TABLE message ( id serial primary key, foo1 integer, foo2 integer, foo3 text ) And we have a function that gets a message and deletes it from the relation, like this: CREATE FUNCTION get_and_delete_message(p_foo1 integer) RETURNS TABLE(r_id integer, r_foo1 integer, r_foo2 integer, r_foo3 text) AS $$ DECLARE message_id integer; BEGIN SELECT id INTO message_id FROM message WHERE foo1 = p_foo1 LIMIT

How do I get Postgresql procedure warning messages in Golang

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 17:31:23
问题 While running a stored procedure, the procedure can raise warning messages. Is there any way to get these messages using Postgresql driver(https://github.com/lib/pq) in Golang ? 回答1: The answer appears to be no. In my tests the Postgres server did not appear to send the warning with the results. Even if it did, returning an error along with the sql.Result would be confusing at best and would require lib/pq modifications. Raising an error in the function did return an error, but (obviously) no

Get row to swap tables on a certain condition

二次信任 提交于 2019-12-23 17:19:39
问题 I currently have a parent table: CREATE TABLE members ( member_id SERIAL NOT NULL, UNIQUE, PRIMARY KEY first_name varchar(20) last_name varchar(20) address address (composite type) contact_numbers varchar(11)[3] date_joined date type varchar(5) ); and two related tables: CREATE TABLE basic_member ( activities varchar[3]) INHERITS (members) ); CREATE TABLE full_member ( activities varchar[]) INHERITS (members) ); If the type is full the details are entered to the full_member table or if type

Is it possible to create a table with a variable name in PostgreSQL?

淺唱寂寞╮ 提交于 2019-12-23 15:02:57
问题 Using PL/pgSQL or (some other mechanism), is it possible to create a table with a variable name? I would like to create multiple tables named table_1, table_2, table_3, etc... and it would be simpler if I could use a loop to create them, instead of explicitly creating each one. I suspect the answer to this is no, but I would like to confirm it. 回答1: While I would question your design if you're relying on such tricks, your question is general and you didn't give specific information to judge

PostgreSQL function with duplicate parameters

纵饮孤独 提交于 2019-12-23 09:11:30
问题 I stumbled upon a curious function signature in pg_catalog.pg_stat_get_activity : CREATE OR REPLACE FUNCTION pg_stat_get_activity( IN pid integer, OUT datid oid, OUT pid integer, -- more parameters...) RETURNS SETOF record AS 'pg_stat_get_activity' LANGUAGE internal STABLE COST 1 ROWS 100; This function declares the same parameter name twice, which is also reported from the information_schema . select parameter_mode, parameter_name from information_schema.parameters where specific_schema =

What is the scope of a PostgreSQL Temp Table?

我是研究僧i 提交于 2019-12-23 09:07:05
问题 I have googled quite a bit, and I have fairly decent reading comprehension, but I don't understand if this script will work in multiple threads on my postgres/postgis box. Here is the code: Do $do$ DECLARE x RECORD; b int; begin create temp table geoms (id serial, geom geometry) on commit drop; for x in select id,geom from asdf loop truncate table geoms; insert into geoms (geom) select someGeomfield from sometable where st_intersects(somegeomfield,x.geom); ----do something with the records in

Incrementing a number in a loop in plpgsql

送分小仙女□ 提交于 2019-12-23 07:46:20
问题 I couldn't find this immediately from the examples. I want to increment a variable in a loop, in a function. For instance: DECLARE iterator float4; BEGIN iterator = 1; while iterator < 999 ..... iterator ++; END; How would this be done? I was looking at this document about flow control: http://www.postgresql.org/docs/8.4/static/plpgsql-control-structures.html And none of them seem to be relevant for me, unless these are absolutely the only ways to simulate incrementing a variable. 回答1: To

How to drop user in postgres if it has depending objects

时光怂恿深爱的人放手 提交于 2019-12-23 07:07:11
问题 Database idd owner is role idd_owner . Database has 2 data schemas: public and firma1 . User may have directly or indirectly assigned rights in this database and objects. User is not owner of any object. It has only granted rights. How to drops such user ? I tried revoke all on all tables in schema public,firma1 from "vantaa" cascade; revoke all on all sequences in schema public,firma1 from "vantaa" cascade; revoke all on database idd from "vantaa" cascade; revoke all on all functions in

PLPGSQL Cascading Triggers?

心已入冬 提交于 2019-12-23 05:37:08
问题 I am trying to create a trigger, so that when ever I add a new record it adds another record in the same table. The session field will only take values between 1 and 4. So when I add a 1 in session I want it to add another record but with session 3 blocked. But the problem is that it leads to cascading triggers and it inserts itself again and again because the trigger is triggered when inserted. I have for example a simple table: CREATE TABLE example ( id SERIAL PRIMARY KEY ,name VARCHAR(100)