plpgsql

Creating a trigger for child table insertion returns confusing error

本秂侑毒 提交于 2019-11-29 12:19:05
I am trying to write a trigger function that will input values into separate child tables, however I am getting an error I have not seen before. Here is an example set up: -- create initial table CREATE TABLE public.testlog( id serial not null, col1 integer, col2 integer, col3 integer, name text ); -- create child table CREATE TABLE public.testlog_a (primary key(id)) INHERITS(public.testlog); -- make trigger function for insert CREATE OR REPLACE FUNCTION public.test_log() RETURNS trigger AS $$ DECLARE qry text; BEGIN qry := 'INSERT INTO public.testlog_' || NEW.name || ' SELECT ($1).*'; EXECUTE

Declare row type variable in PL/pgSQL

旧时模样 提交于 2019-11-29 11:34:59
问题 As I found SELECT * FROM t INTO my_data; works only if: DO $$ DECLARE my_data t%ROWTYPE; BEGIN SELECT * FROM t INTO my_data WHERE id = ?; END $$; Am I right? If I want to get only 2-3 columns instead of all columns. How can I define my_data ? That is, DO $$ DECLARE my_data <WHAT HERE??>; BEGIN SELECT id,name,surname FROM t INTO my_data WHERE id = ?; END $$; 回答1: get only 2-3 columns instead of all columns One way: use a record variable: DO $$ DECLARE _rec record; BEGIN SELECT INTO _rec id,

Is it possible to use a variable and not specify a return type in postgreSQL?

女生的网名这么多〃 提交于 2019-11-29 11:20:30
Consider this T-SQL: DECLARE @ColorID INT SET @ColorID = 3 SELECT *, left(name,3) FROM Products p WHERE ColorID = @ColorID This works but doesn't declare a variable: SELECT *, substring(name,1,3) FROM Products p WHERE ColorID = 3 I tried this: DO $$ DECLARE ColorID INT; BEGIN ColorID := 3; SELECT *, substring(name,1,3) FROM Products p WHERE ColorID = ColorID END$$; It wants me to specify the result set. I don't want to do that because it keeps changing as I'm just exploring the data. ERROR: query has no destination for result data I tried "return query" but then get this error: ERROR: cannot

How To Avoid Looping Trigger Calls In PostgreSQL 9.2.1

限于喜欢 提交于 2019-11-29 10:58:11
I have a table: CREATE TABLE field_data.soil_samples ( pgid SERIAL NOT NULL, sample_id text, project_id text, utm_zone integer, utm_easting integer, utm_northing integer, wgs84_longitude double precision, wgs84_latitude double precision, yt_albers_geom geometry(Point,3578), CONSTRAINT soil_samples_pk PRIMARY KEY (pgid) ) The PostGIS 2.0 geometry in yt_albers_geom is created using a trigger which fires on INSERTS against this table. If the record being inserted satisfies one of the following conditions, the geometry is generated: Both wgs84_latitude and wgs84_longitude fields are not null Each

Tool for translation of Oracle PL/SQL into Postgresql PL/pgSQL [closed]

会有一股神秘感。 提交于 2019-11-29 10:26:35
Is there a tool (preferably free) which will translate Oracle's PL/SQL stored procedure language into Postgresql's PL/pgSQL stored procedure language? Jimoc There is a tool available at http://ora2pg.darold.net/ which can be used to transalate Oracle Schemas to Postgres schemas, but I'm not sure if it will also translate the stored procedures. But it might provide a place to start. Having been working on an Oracle to Postgres conversion for quite some time. The only way to do it is by hand. There are subtle differences between the two languages that can trip you up. We tried using an automated

Execute multiple functions together without losing performance

被刻印的时光 ゝ 提交于 2019-11-29 09:43:15
I have this process that has to make a series of queries, using pl/pgsql: --process: SELECT function1(); SELECT function2(); SELECT function3(); SELECT function4(); To be able to execute everything in one call, I created a process function as such: CREATE OR REPLACE FUNCTION process() RETURNS text AS $BODY$ BEGIN PERFORM function1(); PERFORM function2(); PERFORM function3(); PERFORM function4(); RETURN 'process ended'; END; $BODY$ LANGUAGE plpgsql The problem is, when I sum the time that each function takes by itself, the total is 200 seconds, while the time that the function process() takes

Set empty strings ('') to NULL in the whole database

北城以北 提交于 2019-11-29 08:49:22
In my database are many text columns where values are empty strings ( '' ). The empty strings need to be set to NULL . I do not know the exact schemas, tables and columns in this database or rather I want to write a general solution which can be reused. How would I write a query / function to find all text columns in all tables in all schemas and update all columns with empty strings ( '' ) to NULL ? Erwin Brandstetter The most efficient way to achieve this: Run a single UPDATE per table. Only update nullable columns (not defined NOT NULL ) with any actual empty string. Only update rows with

Array of arrays in PostgreSQL

柔情痞子 提交于 2019-11-29 07:55:50
I'm using the %% operator on PostgreSQL's hstore type which converts a hstore (key-value type effectively) into an array whose elements alternate {{key, value}, {key value}}. When I want to return array of these flattened hstores I get this error: could not find array type for data type text[] due to PostgreSQL lack of support for an array of arrays. From a curiosity standpoint, does anyone know why these are not supported? And more importantly, is there a work around for this type of scenario? At the moment I'm concatenating the results into a string (comma separated) and parsing them on the

Using temp table in PL/pgSQL procedure for cleaning tables

◇◆丶佛笑我妖孽 提交于 2019-11-29 06:52:09
I'm trying to delete all data related to a user id from a game database. There is a table holding all games (each played by 3 players): # select * from pref_games where gid=321; gid | rounds | finished -----+--------+---------------------------- 321 | 17 | 2011-10-26 17:16:04.074402 (1 row) And there is a table holding players scores for that game #321: # select * from pref_scores where gid=321; id | gid | money | quit ----------------+-----+-------+------ OK531282114947 | 321 | 218 | f OK501857527071 | 321 | -156 | f OK429671947957 | 321 | -62 | f When I try the following SELECT INTO

Return dynamic table with unknown columns from PL/pgSQL function

和自甴很熟 提交于 2019-11-29 03:40:59
I need to create a function that checks on a given table if the infowindow field exists. If it exists the function must return select * from table but if it does not, it must return an additional id field: CREATE OR REPLACE FUNCTION getxo_ocx_cincu_preparar_infowindow( guretabla character varying) RETURNS TABLE AS $BODY$ DECLARE tabla ALIAS FOR $1; BEGIN IF EXISTS (SELECT 1 FROM pg_namespace n JOIN pg_class c ON c.relnamespace = n.oid JOIN pg_attribute a ON a.attrelid = c.oid WHERE n.nspname = current_schema() -- default to current schema AND c.relname = tabla AND a.attname = 'infowindow' AND