plpgsql

Can I truncate tables dynamically?

空扰寡人 提交于 2019-12-13 04:00:58
问题 I have a table with 3 different partitions that is split on the point_of_sale column. I have constructed a view ( massive_table ) to aggregate data from these partitions like so: CREATE VIEW massive_table as SELECT * FROM massive_table_UK UNION ALL SELECT * FROM massive_table_US UNION ALL SELECT * FROM massive_table_DE; The massive_table_UK/US/DE tables each have a check constraint imposed on them so that the point_of_sale column can only contain a string value of either UK , US or DE as

Postgres: invalid type name “query%ROWTYPE”

烂漫一生 提交于 2019-12-13 03:55:47
问题 I want to create a variable based on the table "query" ( l_query query%ROWTYPE ), but I got this message: invalid type name "query%ROWTYPE" I also tried to use the fully qualified table name l_query dbname.public.query%ROWTYPE , but it didn't help me ether. CREATE OR REPLACE FUNCTION somefunc() RETURNS int AS $$ DECLARE l_res dbname.public.query%ROWTYPE; BEGIN return 1; END; $$ LANGUAGE plpgsql; PS:I do have the table query. I checked it a couple of times. I have this error only on the

Best practice to check if a variable exists in plpgsql?

夙愿已清 提交于 2019-12-13 02:57:40
问题 For instance I've got a stored procedure to import data from csv files and write the read data into a SQL table. I've got a table defined as below: CREATE TABLE person (id int, name text, age int, married boolean); First I check if the record exist already, if exists I do update, if doesn't - insert. Each field of record may have a different type, so the result of a SQL command are assigned to a list of scalar variables: SELECT name, age, married INTO v_name, v_age, v_married [..] Let's

How to select inside a FOR-Loop for further computations?

落爺英雄遲暮 提交于 2019-12-13 02:35:01
问题 Working with postgresql and postgis I have 2 openstreetmap tables, containing: Point: Locations with a single coordinate Polygon: Areas with sets of coordinates Now, I'm trying to loop through the Point table and for each record I'm trying to do some computations with postgis functions, e.g. ST_Intersects() . Then trying to insert the results into another tables. So far I've only done simple SELECT queries with postgis functions, they basically work like this: SELECT a.name, b.name FROM table

How to use mixed int and numeric arguments in a Postgres 9.1+ function

谁都会走 提交于 2019-12-13 02:15:26
问题 I'm looking for a way to create an icase() function which works with any second and third parameter compatible data types. I tried in Postgres 9.4: CREATE OR REPLACE FUNCTION public.icase( cond1 boolean, res1 anyelement, conddefault anyelement) RETURNS anyelement AS ' SELECT CASE WHEN $1 THEN $2 ELSE $3 END; ' LANGUAGE sql IMMUTABLE; But: select icase( true, 1.0, 0 ) causes error: ERROR: function icase(boolean, numeric, integer) does not exist LINE 9: select icase( true, 1.0, 0 ) ^ HINT: No

Temporary table and loops in a function

夙愿已清 提交于 2019-12-13 00:13:26
问题 I have a function in plpgsql where it creates a temporary table and then it has a loop. The thing is that each time it loops it executes also the part where it creates the temporary table and therefore an error pops up saying; ERROR: relation "tmpr" already exists CONTEXT: SQL statement "CREATE TEMPORARY TABLE tmpr ( id int, source geometry, target geometry, dist_ft character varying )" Is there any way to prevent part of the code from executing more than once? Below you can find the code:

Executing queries dynamically in PL/pgSQL

不想你离开。 提交于 2019-12-12 19:19:07
问题 I have found solutions (I think) to the problem I'm about to ask for on Oracle and SQL Server, but can't seem to translate this into a Postgres solution. I am using Postgres 9.3.6. The idea is to be able to generate "metadata" about the table content for profiling purposes. This can only be done (AFAIK) by having queries run for each column so as to find out, say... min/max/count values and such. In order to automate the procedure, it is preferable to have the queries generated by the DB,

PostgreSQL - how to determine whether a transaction is active?

て烟熏妆下的殇ゞ 提交于 2019-12-12 18:25:45
问题 Let me open by saying: yes, I am aware of Determine if a transaction is active (Postgres) Unfortunately the sole answer to that question is far too specific to the use case provided, and doesn't actually indicate whether or not a transaction is active. The select txid_current(); trick suggested by How to check for pending operations in a PostgreSQL transaction doesn't appear to work - I always get the same transaction ID from adjacent calls to that function. Possibly this is because I'm

How To Restrict Delete using PL/pgSQL trigger?

穿精又带淫゛_ 提交于 2019-12-12 18:13:32
问题 If the client user is trying to delete more than 5 records from a Table i want to restrict that using a trigger. I have a basic idea to do that but i don't know how to implement the Idea. I appreciate any HELP. Basic Idea : In Trigger IF TG_OP = Delete and the count of records to be deleted are more than 5 then Restrict. CREATE TRIGGER adjust_count_trigger BEFORE DELETE ON schemaname.tablename FOR EACH ROW EXECUTE PROCEDURE public.adjust_count(); CREATE OR REPLACE FUNCTION adjust_count()

Need Assistance with a Postgres Trigger and Function

ぃ、小莉子 提交于 2019-12-12 12:06:10
问题 I have a lookup table that holds a column of sources (from various hardcoded campaigns captured through a webservice API I created) and the respective brands that should be associated with them. This is so I can give a brand to records where brand is null - so that they can be welcomed with a certain template through a marketing automation tool. I'm eventually deprecating this API and replacing it with one where brand is required, but in the meantime I have to craft a temporary solution until