plpgsql

How to access jOOQ generated Routine's field as value by using Custom Converter?

爱⌒轻易说出口 提交于 2020-07-03 13:26:30
问题 I'm having an issue with accessing field of generated Routine from PL/pgSQL user-defined function (which returns JSON data type as a result) already mentioned in this question. This is my result of get_all_orders() function created in PL/pgSQL of JSON type: { "orders": [ { "order_id": 1, "total_price": 29.99, "order_date": "2019-08-22T10:06:33", "user": { "user_id": 1, "username": "test" }, "order_items": [ { "order_item_id": 1, "amount": 1, "book": { "book_id": 1, "title": "Harry Potter and

Retreiving original notice text from PostgreSQL

橙三吉。 提交于 2020-06-26 05:23:52
问题 I'm using PL/pgSQL trying to emulate what I can do in Oracle PL/SQL with dbms_output as an equivalent to stdout. I have read that RAISE NOTICE is probably the best way to handle this. However my problem is when I retrieve the text from psycopg2 notices object I get the extra NOTICE: prefix and an extra linefeed. I know I can just remove the problem characters from the string, however it seems a little clunky, and it's annoying me that I can't work out how to retrieve the original text. Is it

Can a Postgres Commit Exist in Procedure that has an Exception Block?

余生颓废 提交于 2020-06-08 07:21:11
问题 I am having a difficult time understanding transactions in Postgres. I have a procedure that may encounter an exception. There are parts of the procedure where I might want to commit my work so-far so that it won't be rolled back if an exceptions ensues. I want to have an exception handling block at the end of the procedure where I catch the exception and insert the information from the exception into a logging table. I have boiled the problem down to a simple procedure, below, which fails on

COPY with dynamic file name

老子叫甜甜 提交于 2020-04-18 05:46:00
问题 I am trying to write a function to load csv data into a table. I want the input argument to be the path to the file. CREATE OR REPLACE FUNCTION public.loaddata(filepathname varchar) RETURNS void AS $BODY$ BEGIN COPY climatedata( climatestationid, date, prcp, prcpqflag, prcpmflag, prcpsflag, tmax, tmaxqflag, tmaxmflag, tmaxsflag, tmin, tminqflag, tminmflag, tminsflag) FROM $1 WITH csv header; END; $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION public.filltmaxa(character varying)

How to disambiguate a plpgsql variable name in a ON CONFLICT clause?

送分小仙女□ 提交于 2020-03-21 20:04:13
问题 Given this table: create table test ( name text primary key ); I need to write a plpgsql function with a variable name that collides with the primary key name, which I must use in a on conflict clause: create or replace function func( name text -- this variable name... ) returns void language plpgsql as $$ begin insert into test (name) values (name) on conflict (name) do update -- ...conflicts with this line set name = func.name; end; $$; This compiles, but then throws an ambiguous column

Why can PL/pgSQL functions have side effect, while SQL functions can't?

牧云@^-^@ 提交于 2020-03-16 07:37:10
问题 PostgreSQL document says: The entire body of a SQL function is parsed before any of it is executed. While a SQL function can contain commands that alter the system catalogs (e.g., CREATE TABLE ), the effects of such commands will not be visible during parse analysis of later commands in the function. Thus, for example, CREATE TABLE foo (...); INSERT INTO foo VALUES(...); will not work as desired if packaged up into a single SQL function , since foo won't exist yet when the INSERT command is

How to use dynamic column names in an UPDATE or SELECT statement in a function?

感情迁移 提交于 2020-02-25 03:38:06
问题 In PostgreSQL 9.1, PL/pgSQL, given a query: select fk_list.relname from ... where relname is of type name (e.g., "table_name"). How do you get the appropriate value for "relname" that can be used directly in an UPDATE statement as: Update <relname> set ... within the PL/pgSQL script? Using quote_ident(r.relname) as: Update quote_ident(r.relname) Set ... fails with: syntax error at or near "(" LINE 55: UPDATE quote_ident(r.relname) .... The complete code I am working with: CREATE FUNCTION

How to return a table by rowtype in PL/pgSQL

安稳与你 提交于 2020-02-25 01:28:06
问题 I am trying to implement a function that returns a table with the same structure as an input table in the parameter, using PL/pgSQL (PostgreSQL 9.3). Basically, I want to update a table, and return a copy of the updated table with plpgsql. I searched around SO and found several related questions (e.g. Return dynamic table with unknown columns from PL/pgSQL function and Table name as a PostgreSQL function parameter), which lead to the following minimal test example: CREATE OR REPLACE FUNCTION

Error on Dynamic csv file export using plpgsql copy to csv in a function

拈花ヽ惹草 提交于 2020-02-07 03:41:44
问题 I am trying to filter a postgresql table for rows that have a product id as a foreign key. For each product id, I need to export 1 csv each to a folder eg , prod1.csv, prod2.csv etc. I have tried to create the function below to automate this but the function is failing when I run it. Can anyone kindly assist me to fix the function or recommend a better approach? CREATE or replace FUNCTION exportdata() RETURNS SETOF record AS $$ DECLARE rec text; BEGIN FOR rec IN ( Select distinct t.products

SQL to merge rows

南楼画角 提交于 2020-01-25 12:14:13
问题 Update How I can select the rows from a table that when ordered the first element matches some row the second element matches the next row the third element the next row after the second row the forth element the next row after the third row and so on until the end of values in array? The Logic Supposing I have these rows as result of a query (table token holds id and word and table positioning holds id and position ): id | word | textblockid |sentence |position 5 | Fear | 5 | 1 | 1 8 | of |