plpgsql

Disable DELETE on table in PostgreSQL?

旧巷老猫 提交于 2019-12-18 12:13:09
问题 For a security sensitive design, I'd like to disable DELETEs on certain tables. The DELETE should merely set a deleted flag on a row (which would be then visible on a view, which would be used by the application layer). As I understand a rule would generate additional queries - so a rule could not suppress the original query. As illustration a toy example with a trigger (not yet tested): -- data in this table should be 'undeletable' CREATE table article ( id serial, content text not null,

PostgreSQL - Writing dynamic sql in stored procedure that returns a result set

百般思念 提交于 2019-12-18 11:56:02
问题 How can I write a stored procedure that contains a dynamically built SQL statement that returns a result set? Here is my sample code: CREATE OR REPLACE FUNCTION reporting.report_get_countries_new ( starts_with varchar, ends_with varchar ) RETURNS TABLE ( country_id integer, country_name varchar ) AS $body$ DECLARE starts_with ALIAS FOR $1; ends_with ALIAS FOR $2; sql VARCHAR; BEGIN sql = 'SELECT * FROM lookups.countries WHERE lookups.countries.country_name >= ' || starts_with ; IF ends_with

How to execute a string result of a stored procedure in postgres

不打扰是莪最后的温柔 提交于 2019-12-18 09:17:15
问题 I have created the following stored procedure, which basically receives a name of table, and a prefix. The function then finds all columns that share this prefix and returns as an output a 'select' query command ('myoneliner'). as follows: CREATE OR REPLACE FUNCTION mytext (mytable text, myprefix text) RETURNS text AS $myoneliner$ declare myoneliner text; BEGIN SELECT 'SELECT ' || substr(cols,2,length(cols)-2) ||' FROM '||mytable INTO myoneliner FROM ( SELECT array( SELECT DISTINCT quote

Return Table Type from A function in PostgreSQL

会有一股神秘感。 提交于 2019-12-18 09:14:52
问题 I have a function from which has a return type as TABLE, and I want to get certain columns from my table into that RETURN TABLE type for my functionality. When I execute the function, it gives no error but returns no records although it should return some records based on the condition that I have. Below is the code that I have written, can someone let me know where have I gone wrong? CREATE OR REPLACE FUNCTION ccdb.fn_email_details_auto() RETURNS TABLE (code integer, area smallint, action

Split function-returned record into multiple columns

≡放荡痞女 提交于 2019-12-18 08:55:49
问题 In a basic Postgres function tutorial there is an example with OUT parameters like so: create or replace function hi_lo(a numeric, b numeric, c numeric, OUT hi numeric, OUT lo numeric) as $$ begin hi := greatest(a, b, c); lo := least(a, b, c); end; $$ language plpgsql; Then results look like select hi_lo(2, 3, 4); -- returns one column, "hi_lo" with value "(4, 2)". select * from hi_lo(2, 3, 4); -- returns two columns, "hi" / 4 and "lo" / 2. But suppose you want to execute the function on

Creating a trigger for child table insertion returns confusing error

[亡魂溺海] 提交于 2019-12-18 07:06:40
问题 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 $

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

丶灬走出姿态 提交于 2019-12-18 06:55:55
问题 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:

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

邮差的信 提交于 2019-12-18 05:20:33
问题 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 ? 回答1: The most efficient way to achieve this: Run a single UPDATE per table. Only update

Ordered count of consecutive repeats / duplicates

六月ゝ 毕业季﹏ 提交于 2019-12-17 21:33:16
问题 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

Group by repeating attribute

≯℡__Kan透↙ 提交于 2019-12-17 21:16:26
问题 Basically I have a table messages , with user_id field that identifies a user that created the message. When I display a conversation(set of messages) between two users, I want to be able to group the messages by user_id , but in a tricky way: Let's say there are some messages (sorted by created_at desc ): id: 1, user_id: 1 id: 2, user_id: 1 id: 3, user_id: 2 id: 4, user_id: 2 id: 5, user_id: 1 I want to get 3 message groups in the below order: [1,2], [3,4], [5] It should group by *user_id*