plpgsql

SELECT .. INTO to create a table in PL/pgSQL

浪子不回头ぞ 提交于 2019-11-29 02:53:39
I want to use SELECT INTO to make a temporary table in one of my functions. SELECT INTO works in SQL but not PL/pgSQL. This statement creates a table called mytable (If orig_table exists as a relation): SELECT * INTO TEMP TABLE mytable FROM orig_table; But put this function into PostgreSQL, and you get the error: ERROR: "temp" is not a known variable CREATE OR REPLACE FUNCTION whatever() RETURNS void AS $$ BEGIN SELECT * INTO TEMP TABLE mytable FROM orig_table; END; $$ LANGUAGE plpgsql; I can SELECT INTO a variable of type record within PL/pgSQL, but then I have to define the structure when

How to check if a row exists in a PostgreSQL stored procedure?

旧时模样 提交于 2019-11-29 02:48:33
问题 I writing a stored procedure in postgres where I need to check if a row exists then act accordingly. something along the line. IF SELECT * FROM foo WHERE x = 'abc' AND y = 'xyz' THEN -- do something here ELSE -- do something else END; I have googled a bit but got no good hits. 回答1: Use PERFORM and the FOUND automatic variable: PERFORM * FROM foo WHERE x = 'abc' AND y = 'xyz'; IF FOUND THEN .... END IF; This will succeed if one or more rows is returned. If you want to constrain the result to

Using pg_notify in PostgreSQL trigger function

你离开我真会死。 提交于 2019-11-29 02:17:55
问题 I am attempting to issue a notification from a PostgreSQL trigger function. I can successfully use the NOTIFY command, but I am not having any luck with pg_notify. Even though I receive a notification when I invoke the pg_notify function from the psql console, I never receive a notification when invoking the same from my trigger function. This version of my trigger function works as expected. I have a Java program that is LISTENing to 'mymessage', and it receives a notification with a 'fired

Commit, savepoint, rollback to in PostgreSQL?

巧了我就是萌 提交于 2019-11-29 02:13:11
Can someone please explain to me why does COMMIT in this function returns EXCEPTION ? DECLARE XNar CURSOR (forDATE Varchar) IS SELECT NARUCENO, ISPORUKA_ID FROM XDATA_NARUDZBE WHERE TO_CHAR(XDATA_NARUDZBE.DATUM, 'DD.MM.YYYY') = forDATE; LastDate DATE; OutResult INTEGER; curNAR NUMERIC; curISP VARCHAR; RXNar RECORD; BEGIN OutResult := 1; SELECT MAX(DATUM) INTO LastDate FROM XDATA_NARUDZBE; FOR RXNar IN XNar(TO_CHAR(LastDate, 'DD.MM.YYYY')) LOOP IF (RXNar.NARUCENO <> 0) AND (RXNar.ISPORUKA_ID = 'R01') THEN UPDATE NARUDZBE SET ISPORUCENO = RXNar.NARUCENO WHERE NARUDZBE.PP_ID = RXNar.PP_ID AND

Postgres SELECT … FOR UPDATE in functions

流过昼夜 提交于 2019-11-29 01:37:23
问题 I have two questions about using SELECT … FOR UPDATE row-level locking in a Postgres function: Does it matter which columns I select? Do they have any relation to what data I need to lock and then update? SELECT * FROM table WHERE x=y FOR UPDATE; vs SELECT 1 FROM table WHERE x=y FOR UPDATE; I can't do a select in a function without saving the data somewhere, so I save to a dummy variable. This seems hacky; is it the right way to do things? Here is my function: CREATE OR REPLACE FUNCTION

Store select query's output in one array in postgres

佐手、 提交于 2019-11-29 00:50:47
My code is: SELECT column_name FROM information.SCHEMA.columns WHERE table_name = 'aean' It returns column names of table aean . Now I have declared an array: DECLARE colnames text[] How can I store select's output in colnames array. Is there any need to initialize colnames? Denis de Bernardy There are two ways. One is to aggregate: SELECT array_agg(column_name::TEXT) FROM information.schema.columns WHERE table_name = 'aean' The other is to use an array constructor: SELECT ARRAY( SELECT column_name FROM information.schema.columns WHERE table_name = 'aean') I'm presuming this is for plpgsql. In

Check if sequence exists in Postgres (plpgsql)

你。 提交于 2019-11-28 21:14:42
I'm trying to test, within a stored procedure, whether a sequence already exists. IF EXISTS SEQUENCE seq_name RAISE EXCEPTION 'sequence % already exists!', seq_name END IF; I have tried several variations of the snippet above without luck. I must be giving Google the wrong terms because I can't seem to find anything on the topic. Any help is appreciated! You should be able query the pg_class table to see if the relname exists. IF EXISTS (SELECT 0 FROM pg_class where relname = '<my sequence name here>' ) THEN --stuff here END IF; The answer from @rfusca works if you're sure that the name could

How do I get the primary key(s) of a table from Postgres via plpgsql?

时光怂恿深爱的人放手 提交于 2019-11-28 20:09:30
Given a table name, how do I extract a list of primary key columns and their datatypes from a plpgsql function? The query above is very bad as it is really slow. I would recommend this official version: http://wiki.postgresql.org/wiki/Retrieve_primary_key_columns if schema is needed the query is as follows SELECT pg_attribute.attname, format_type(pg_attribute.atttypid, pg_attribute.atttypmod) FROM pg_index, pg_class, pg_attribute, pg_namespace WHERE pg_class.oid = 'foo'::regclass AND indrelid = pg_class.oid AND nspname = 'public' AND pg_class.relnamespace = pg_namespace.oid AND pg_attribute

PostgreSQL 9.1 pg_restore error regarding PLPGSQL

走远了吗. 提交于 2019-11-28 16:23:59
问题 I am using Postgres for a django project and I am currently implementing a database backup/restore system that as simple as possible performs a pg_dump when the user clicks backup and then pg_restore when they click restore backup. All seems fine and dandy until it actually tries to perform the pg_restore at which time it gives this error: pg_restore: [archiver (db)] Error from TOC entry 3206; 0 0 COMMENT EXTENSION plpgsql pg_restore: [archiver (db)] could not execute query: ERROR: must be

Ordered count of consecutive repeats / duplicates

时光毁灭记忆、已成空白 提交于 2019-11-28 14:31:07
I highly doubt I'm doing this in the most efficient manner, which is why I tagged plpgsql on here. I need to run this on 2 billion rows for a thousand measurement systems . You have measurement systems that often report the previous value when they lose connectivity, and they lose connectivity for spurts often but sometimes for a long time. You need to aggregate but when you do so, you need to look at how long it was repeating and make various filters based on that information. Say you are measuring mpg on a car but it's stuck at 20 mpg for an hour than moves around to 20.1 and so on. You'll