plpgsql

How to get the key fields for a table in plpgsql function?

纵然是瞬间 提交于 2019-12-23 05:31:03
问题 I need to make a function that would be triggered after every UPDATE and INSERT operation and would check the key fields of the table that the operation is performed on vs some conditions. The function (and the trigger) needs to be an universal one, it shouldn't have the table name / fields names hardcoded. I got stuck on the part where I need to access the table name and its schema part - check what fields are part of the PRIMARY KEY. 回答1: After getting the primary key info as already posted

Trigger to delete rows from related tables before deleting rows from actual table

此生再无相见时 提交于 2019-12-23 03:47:14
问题 I have the following tables: CREATE TABLE QUESTION( id varchar(10) NOT NULL PRIMARY KEY, que_type numeric(1)); CREATE TABLE ESSAY( que_id varchar(10) NOT NULL PRIMARY KEY, ans varchar(2000), FOREIGN KEY (que_id) REFERENCES QUESTION (id)); CREATE TABLE TFFB( que_id varchar(10) NOT NULL PRIMARY KEY, ans varchar(50), FOREIGN KEY (que_id) REFERENCES QUESTION (id)); CREATE TABLE MCQ( que_id varchar(10) NOT NULL PRIMARY KEY, ans varchar(200), FOREIGN KEY (que_id) REFERENCES QUESTION (id)); and try

Create SQL function referring to a table or column that does not exist (yet)

故事扮演 提交于 2019-12-22 18:33:39
问题 I want to load some SQL functions in a empty database through psql: psql -d my_database -f fuctions.sql --set ON_ERROR_STOP=1 I use --set ON_ERROR_STOP=1 because I want that psql fails if the script contains errors. The content of functions.sql is: CREATE or REPLACE FUNCTION test_function() RETURNS INT AS $$ SELECT id from test_table; $$ LANGUAGE sql; My problem is, that psql checks if test_table exists when loading function and fails with this error: ERROR: relation "test_table" does not

Variables for identifiers inside IF EXISTS in a plpgsql function

本小妞迷上赌 提交于 2019-12-22 13:08:52
问题 CREATE OR REPLACE FUNCTION drop_now() RETURNS void AS $BODY$ DECLARE row record; BEGIN RAISE INFO 'in'; FOR row IN select relname from pg_stat_user_tables WHERE schemaname='public' AND relname LIKE '%test%' LOOP IF EXISTS(SELECT row.relname.tm FROM row.relname WHERE row.relname.tm < current_timestamp - INTERVAL '90 minutes' LIMIT 1) THEN -- EXECUTE 'DROP TABLE ' || quote_ident(row.relname); RAISE INFO 'Dropped table: %', quote_ident(row.relname); END IF; END LOOP; END; $BODY$ LANGUAGE plpgsql

How do I pass in a table parameter to this function?

人盡茶涼 提交于 2019-12-22 10:55:15
问题 I have a function organized like so: create function everything(waypoints waypoint) returns table(node int, xy text array) as $$ BEGIN create view results as ... return query (select * from results); END; $$ LANGUAGE plpgsql; And I have a table that has arguments organized the way the waypoint data type is structured. This table is not explicitly of type waypoint itself. The function gets created as it should, however, I am unable to call it by passing in my table like so: select everything

Handling EXCEPTION and return result from function

瘦欲@ 提交于 2019-12-22 09:50:04
问题 This is my code CREATE OR REPLACE FUNCTION test_excep (arg INTEGER) RETURNS INTEGER AS $$ DECLARE res INTEGER; BEGIN res := 100 / arg; BEGIN EXCEPTION WHEN division_by_zero THEN RETURN 999; END; RETURN res; END; $$ LANGUAGE plpgsql; That is, I need returned "999", if happened division by zero, but this: SELECT test_excep(0) returns error: division by zero CONTEXT: PL/pgSQL function test_excep(integer) line 4 at assignment What is wrong in my code? 回答1: The EXCEPTION clause needs to be in the

Generic trigger to restrict insertions based on count

偶尔善良 提交于 2019-12-22 09:07:24
问题 Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships. The number of those relationships must be restricted. A couple of example tables include: CREATE TABLE authentication ( id bigserial NOT NULL, -- Primary key cookie character varying(64) NOT NULL, -- Authenticates the user with a cookie ip_address character varying(40) NOT NULL -- Device IP address (IPv6-friendly) ) CREATE TABLE tag_comment ( id bigserial NOT NULL, -- Primary key comment_id

What is the difference between a prepared statement and a SQL or PL/pgSQL function, in terms of their purposes?

偶尔善良 提交于 2019-12-22 04:44:26
问题 In PostgreSQL, what is the difference between a prepared statement and a SQL or PL/pgSQL function, in terms of their purposes, advantages and disadvantages? When shall we use which? In this very simple example, do they work the same, correct? CREATE TABLE foo (id INT, name VARCHAR(80)); CREATE FUNCTION myfunc1(INT, VARCHAR(80)) RETURNS void AS ' INSERT INTO foo VALUES ($1, $2); ' LANGUAGE SQL; SELECT myfunc1(3, 'ben'); CREATE FUNCTION myfunc2(INT, VARCHAR(80)) RETURNS void AS ' BEGIN INSERT

Using prepared statement in stored function

落爺英雄遲暮 提交于 2019-12-22 04:08:28
问题 I have a table in the database: create table store ( ... n_status integer not null, t_tag varchar(4) t_name varchar, t_description varchar, dt_modified timestamp not null, ... ); In my stored function I need to execute the same select against this table multiple times: select * from store where n_place_id = [different values] and t_tag is not null and n_status > 0 and (t_name ~* t_search or t_description ~* t_search) order by dt_modified desc limit n_max; Here, t_search and n_max are

PostgreSQL crosstab/pivot problems

旧时模样 提交于 2019-12-21 21:39:42
问题 I have a prefs table, and here are the relevant columns: mydb=> SELECT pref_id, pref_name, pref_value FROM prefs; pref_id | pref_name | pref_value ---------+--------------+---------------- 1 | PagerNumber | 2125551234 2 | PagerCarrier | @att.com 3 | PagerCarrier | @something.com I want to produce something like this: section | pager_number | pager_carrier ---------+----------------+--------------- 1 | 2125551234 | 2 | | @att.com 3 | | @something.com So I used crosstab, following this example