plpgsql

Self-managing PostgreSQL partition tables

跟風遠走 提交于 2019-11-30 16:00:15
I am trying to make a self-managing partition table setup with Postgres. It all revolves around this function but I can't seem to get Postgres to accept my table names. Any ideas or examples of self-managing partition table trigger functions? My current function: DECLARE day integer; year integer; tablename text; startdate text; enddate text; BEGIN day:=date_part('doy',to_timestamp(NEW.date)); year:=date_part('year',to_timestamp(NEW.date)); tablename:='pings_'||year||'_'||day||'_'||NEW.id; -- RAISE EXCEPTION 'tablename=%',tablename; PERFORM 'tablename' FROM pg_tables WHERE 'schemaname'

EXECUTE…INTO…USING statement in PL/pgSQL can't execute into a record?

橙三吉。 提交于 2019-11-30 15:31:49
I'm attempting to write an area of a function in PL/pgSQL that loops through an hstore and sets a record's column(the key of the hstore ) to a specific value (the value of the hstore ). I'm using Postgres 9.1. The hstore will look like: ' "column1"=>"value1","column2"=>"value2" ' Generally, here is what I want from a function that takes in an hstore and has a record with values to modify: FOR my_key, my_value IN SELECT key, value FROM EACH( in_hstore ) LOOP EXECUTE 'SELECT $1' INTO my_row.my_key USING my_value; END LOOP; The error which I am getting with this code: "myrow" has no field "my_key

How to make PostgreSQL insert a row into a table when deleted from another table?

◇◆丶佛笑我妖孽 提交于 2019-11-30 14:53:10
问题 We have an application, which will delete a row from a table based on user requests. I cannot change the application code. However, I want to insert a row into another table (kinda like a journal log) with information from a few other tables based on information of the row that is being deleted. How do I achieve this within PostgreSQL? 回答1: Write a trigger function. Something like this: CREATE OR REPLACE FUNCTION trg_backup_row() RETURNS trigger AS $BODY$ BEGIN INSERT INTO other_tbl SELECT

Postgresql batch insert or ignore

被刻印的时光 ゝ 提交于 2019-11-30 14:43:49
I have the responsibility of switching our code from sqlite to postgres. One of the queries I am having trouble with is copied below. INSERT INTO group_phones(group_id, phone_name) SELECT g.id, p.name FROM phones AS p, groups as g WHERE g.id IN ($add_groups) AND p.name IN ($phones); The problem arises when there is a duplicate record. In this table the combination of both values must be unique. I have used a few plpgsql functions in other places to do update-or-insert operations, but in this case I can do several inserts at once. I am not sure how to write a stored routine for this. Thanks for

How to write combinatorics function in postgres?

三世轮回 提交于 2019-11-30 14:18:09
I have a PostgreSQL table of this form: base_id int | mods smallint[] 3 | {7,15,48} I need to populate a table of this form: combo_id int | base_id int | mods smallint[] 1 | 3 | 2 | 3 | {7} 3 | 3 | {7,15} 4 | 3 | {7,48} 5 | 3 | {7,15,48} 6 | 3 | {15} 7 | 3 | {15,48} 8 | 3 | {48} I think I could accomplish this using a function that does almost exactly this, iterating over the first table and writing combinations to the second table: Generate all combinations in SQL But, I'm a Postgres novice and cannot for the life of me figure out how to do this using plpgsql. It doesn't need to be

Analysing/Profiling queries on PostgreSQL

我只是一个虾纸丫 提交于 2019-11-30 13:57:50
I've just inherited an old PostgreSQL installation and need to do some diagnostics to find out why this database is running slow. On MS SQL you would use a tool such as Profiler to see what queries are running and then see how their execution plan looks like. What tools, if any, exist for PostgreSQL that I can do this with? I would appreciate any help since I´m quite new with Postgres. Use pg_stat_statements extension to get long running queries. then use select* from pg_stat_statements order by total_time/calls desc limit 10 to get ten longest. then use explain to see the plan... Chris

How to make PostgreSQL insert a row into a table when deleted from another table?

╄→尐↘猪︶ㄣ 提交于 2019-11-30 12:34:37
We have an application, which will delete a row from a table based on user requests. I cannot change the application code. However, I want to insert a row into another table (kinda like a journal log) with information from a few other tables based on information of the row that is being deleted. How do I achieve this within PostgreSQL? Write a trigger function. Something like this: CREATE OR REPLACE FUNCTION trg_backup_row() RETURNS trigger AS $BODY$ BEGIN INSERT INTO other_tbl SELECT (OLD).*, t.other_col -- all columns of from old table -- SELECT OLD.col1, OLD.col2, t.other_col -- alternative

SELECT INTO with more than one attribution

会有一股神秘感。 提交于 2019-11-30 12:23:40
This instruction works: SELECT INTO unsolvedNodes array_agg(DISTINCT idDestination) FROM road WHERE idOrigin = ANY(solvedNodes) AND NOT (idDestination = ANY(solvedNodes)); But I would like to use something this way: SELECT INTO unsolvedNodes array_agg(DISTINCT idDestination), lengths array_agg(length) FROM road WHERE idOrigin = ANY(solvedNodes) AND NOT (idDestination = ANY(solvedNodes)); How to use only one "SELECT INTO" instruction to set multiple variables? In PL/pgSQL you can SELECT INTO as many variables at once as you like directly. You just had the syntax backwards: SELECT INTO

drop all tables sharing the same prefix in postgres

微笑、不失礼 提交于 2019-11-30 11:48:47
I would like to delete all tables sharing the same prefix ('supenh_agk') from the same database, using one sql command/query. Erwin Brandstetter To do this in one command you need dynamic SQL with EXECUTE in a DO statement (or function): DO $do$ DECLARE _tbl text; BEGIN FOR _tbl IN SELECT quote_ident(table_schema) || '.' || quote_ident(table_name) -- escape identifier and schema-qualify! FROM information_schema.tables WHERE table_name LIKE 'prefix' || '%' -- your table name prefix AND table_schema NOT LIKE 'pg_%' -- exclude system schemas LOOP RAISE NOTICE '%', -- EXECUTE 'DROP TABLE ' || _tbl

Optional argument in PL/pgSQL function

£可爱£侵袭症+ 提交于 2019-11-30 10:51:49
I am trying to write a PL/pgSQL function with optional arguments. It performs a query based on a filtered set of records (if specified), otherwise performs a query on the entire data set in a table. For example (PSEUDO CODE) : CREATE OR REPLACE FUNCTION foofunc(param1 integer, param2 date, param2 date, optional_list_of_ids=[]) RETURNS SETOF RECORD AS $$ IF len(optional_list_of_ids) > 0 THEN RETURN QUERY (SELECT * from foobar where f1=param1 AND f2=param2 AND id in optional_list_of_ids); ELSE RETURN QUERY (SELECT * from foobar where f1=param1 AND f2=param2); ENDIF $$ LANGUAGE SQL; What would be