Questions tagged [plpgsql]
PL/pgSQL is the default procedural language for PostgreSQL. Questions about PL/pgSQL should probably be tagged [PostgreSQL] as well.
4,289
questions
0
votes
1
answer
19
views
Function to DELETE rows taking the parameters in sql query
I want create a function which removes rows and returns true if there are no errors:
CREATE OR REPLACE FUNCTION s_grnplm_tt.sp_del_stts_cd(num text, package_name text, stts_cd text)
returns ...
0
votes
2
answers
50
views
"RETURNS TABLE (col record)" in a PL/pgSQL function
I have a function in PL/pgSQL that accepts a record type parameter, and returns 1 record type as well:
DROP FUNCTION IF EXISTS public.f_obtem_record_auth(record);
CREATE OR REPLACE FUNCTION public....
1
vote
2
answers
38
views
Committing after every k insertions in PostgreSQL
In PostgreSQL (I use 14 and 16) I am trying to create a function that inserts a record into a table that has two columns: an identifier (integer) and a string "Some string". I want to write ...
0
votes
0
answers
23
views
Retrieve results in PostgreSQL stored procedure allowing query parallelism
I want to execute an SQL query and process its results inside a stored procedure without preventing query parallelism.
Since I don't want to prevent query parallelism, cursors can't be used, and I ...
0
votes
1
answer
29
views
How do I insert data from a select and a variable?
I have a variable, say insert_reply_to_uid;
I want to insert into a table values from a SELECT statement as well as the value of the variable.
A sample code, which is giving run time errors is:
INSERT ...
0
votes
1
answer
33
views
Create a temporary table in Redshift using a JSON string format parameter
I have a redshift stored proc signature that look like
CREATE OR REPLACE PROCEDURE get_filtered_results
(
p_id smallint,
p_filters varchar(65535),
rs_out REFCURSOR
)
LANGUAGE plpgsql
The ...
1
vote
1
answer
48
views
Can PostgreSQL save a query result in a variable?
I am trying to change the name of a database to include todays date and a comment in PostgreSQL using a query.
So far I have managed to change the name to include the date
\c postgres
SELECT
...
-1
votes
0
answers
13
views
Fail to run multiple tests at once on Pycharm
I used to be able to run multi unit tests at once by clicking on the run button on the left side of my test class or right click in my test file and select “Run pytest in test_….”. But recently I got ...
0
votes
1
answer
34
views
Dynamic sql in plpgsql procedure with unknown number of bound variables
I am rewriting an Oracle database application to PostgreSQL. Extensive structures are created dynamically there. Now I am rewriting the procedure where dynamic sql is created and maintained using ...
-2
votes
1
answer
74
views
FOR loop inside json_build_array() function does not work
I am currently working on a function in Postgres. I have the function working, but I need to add a loop inside the json_build_array() function to add multiple of what is inside the array. This is what ...
0
votes
1
answer
35
views
PostgreSQL transaction that has anormally no effect
I'm trying to create a PostgreSQL program to simulate transactions between accounts in a bank.
In order to test my system, I tried with a PostgreSQL transaction that simulates a banking transaction. ...
1
vote
3
answers
53
views
Make dynamic INSERT inside function more readable
I have a user defined Postgres function that does dynamic INSERT. Code is unreadable and it's quite hard to modify it due to string escapes. Is there a way to make it look better?
Here is my code, ...
0
votes
1
answer
46
views
PL/pgSQL vs Python
What is faster, a Python script or a PL/pgSQL procedure?
For instance, if I want to insert for each USER_NAME and USER_PERM from USERS into SYN_USER, I could use this procedure (minimal example):
...
...
-1
votes
2
answers
60
views
How to use $$ as string value inside pl/pgsql
Im building parameterised sql using pgp and wrapping the generated sql inside DO $$ END $$. Problem is when I try saving password (varchar) some of the password hashes contain $$ which causes issues ...
0
votes
1
answer
64
views
I do not understand why PostgreSQL indicates that a procedure does not exist
I have a strange problem with PostgreSQL. It indicates that a procedure does not exist while I think that I created it. I really do not understand. I tried to remove arguments only to see if I would ...
0
votes
1
answer
39
views
Is there a workaround to importing foreign schema's functions in postgres
My use case is that I would like to use a function from one database server into a schema of the DB i am operating in. From it looks like, foreign data wrapper only supports importing tables and when ...
2
votes
1
answer
41
views
Syntax error at or near ""20231002"" in procedure with dynamic table name
I want to create procedure which can create a table with dynamic table name. It takes a date, formats and appends it to the table name.
What I did:
CREATE OR REPLACE PROCEDURE l2....
0
votes
1
answer
40
views
'column reference "id" is ambiguous' error on function call that uses RETURNING
I have this table with two autogenerated columns:
CREATE TABLE driver_orders
(
id SERIAL PRIMARY KEY,
car_id INTEGER NOT NULL,
location_from GEOGRAPHY -- generated
GENERATED ...
0
votes
1
answer
52
views
Query returned more than one row from INSERT ... RETURNING in function
I'm trying to write a function that inserts multiple records from a SELECT statement and then returns the inserted ids to the caller, but I keep getting the error
ERROR: query returned more than one ...
0
votes
1
answer
79
views
Loop with many DELETEs not finishing even though the statements complete OK
I have a function that deletes rows from many tables, as much as 130+ tables, in a loop. The problem isn't the DELETE statements themselves, they seem to complete fine in a timely fashion, but for ...
0
votes
2
answers
53
views
Spring test @Sql throws "Unterminated dollar quote started ... Expected terminating $$"
I am testing my Spring Boot API with a test class (FoodE2eTest) and I have a separate SQL script file (setup-test-schema.sql) that I want to run before the tests start to set up schema, tables, and ...
0
votes
1
answer
32
views
Postgres cursor is not taking multiple parameters
I have created a procedure, and inside the procedure I have created a parameterized cursor to which I am passing parameters. The Cursor query has two parameters: one parameter of the procedure and the ...
0
votes
1
answer
61
views
A procedure supposedly does not exists
I'm trying to make a PostgreSQL system that simulate transfers between 5 banks. But I have a problem that I really do not manage to understand. PostgreSQL seems to not understand the types of my data (...
0
votes
1
answer
62
views
using a values returned from a function in another query inside a function
I am trying the construct a Postgres function which can use the result of another select in where clause. The following works fine
create or replace function get_film_count(a varchar[], v varchar[])
...
0
votes
1
answer
43
views
Output result set in a PL/pgSQL script
I have a PL/pgSQL script (not a function or procedure), which does a bunch of work. Is it possible to display the output of a SELECT statement?
DO $$
BEGIN
SELECT * FROM my_table LIMIT 10;
END $$;
...
0
votes
1
answer
80
views
Create function to return results from multiple queries
I am struggling to write a function to return data from more than 1 table in Postgres
CREATE TABLE IF NOT EXISTS dbo.device (
deviceid uuid NOT NULL,
serialnumber character varying(255),
...
0
votes
1
answer
36
views
Postgresql: dblink_exec parametrized in stored procedure error
I have a "style problem". I'm writing a Postgresql function and I have this query in the code:
INSERT INTO JUNK VALUES (1, 2, 3)
I want to get this query
INSERT INTO JUNK VALUES ('1', '2', '...
0
votes
1
answer
30
views
Does postgres-nodejs support pl/pgsql block code?
I am trying to declare a custom UUID variable to be used on multiple queries during a transaction.
I have tried using a javascript variable but that defeated the purpose of trying to declare the ...
0
votes
0
answers
59
views
How to correct the error of invalid transaction stop that occurs in the transaction?
I have to make a PostgreSQL code to simulate a money transaction between a client of a bank 1 and a client of a bank 2. But I have an invalid transaction's termination error that I do not understand.
...
0
votes
1
answer
21
views
How to correct the syntax error that occurs when I try to do a rollback?
I'm trying to make a rollback but I have a syntax error. I want to make a transaction script to handle money transfers. Can you help me, please ?
DROP TABLE clients, accounts, transactions;
CREATE ...
0
votes
2
answers
46
views
How to correct my PostgreSQL in order to correct the syntax error and well use the CREATE TRIGGER statement?
I need to create a PostgreSQL PL/pgSQL code capable of simulating a simple transfer of money from one account to another.
So here is my code:
DROP TABLE clients, accounts, transactions;
CREATE TABLE ...
0
votes
2
answers
61
views
How to enter "desc" parameter in postgresql function?
I need to enter value for direction of sort in a function, but how to do it?
Tried like that, but this doesn't work of course.
create or replace function invest_ideas
(order_by_direction varchar ...
-1
votes
3
answers
45
views
Passing a date to a function gives conversion to integer error
I am trying to pass a date to a function in postgres. Here is my code:
CREATE OR REPLACE FUNCTION public.path_optimization(input_date date)
RETURNS TABLE(area_id_ uuid, result_uuids text[])
LANGUAGE ...
0
votes
1
answer
75
views
Passing date to a function in postgres
I am using postgres, and trying to define a function which accepts date as an input parameter.
Here is my code:
CREATE OR REPLACE FUNCTION public.path_optimization(input_date date)
RETURNS TABLE(...
0
votes
0
answers
27
views
Greenplum database,postgresql
How to find the list of temporary tables which have been utilizing in the functions in greenplum database
I am trying to find the list of functions only by using the below query
select pn.nspname as ...
1
vote
2
answers
103
views
How to list tables of specific database and sort them by size with PostgreSQL?
I have tried to select a list of databases and select all tables from each database, ordering the first ten tables by their size - including their indexes- through PL/pgSQL, but I have had a problem ...
0
votes
0
answers
43
views
how to treat special characters with postgresql
I have a function using postgresql (plpgsql) that sends emails using the sendgrid api, the content of this email is a .csv file, containing product names, etc.
the email is sent successfully, the ...
0
votes
0
answers
39
views
Looping over a MST implementation using a PL/pgSQL function
I am looping over a MST implementation using a PL/pgSQL function. Here is the function I am using:
CREATE OR REPLACE FUNCTION process_pma_ranked()
RETURNS VOID AS $$
DECLARE
r RECORD;
BEGIN
...
0
votes
2
answers
32
views
Syntax error in plpgsql function with if statement and variable
I'm new to SQL and don't think I understand how to write if else statements. The following code block produces a syntax error at the if statement.
create or replace function weight_from_days_ago (...
0
votes
0
answers
21
views
PLPGSQL Stored Procedure exits LOOP on exception
Trying to implement a LOOP inside PLPGSQL Stored Procedure which should handle an exception scenario and move to the next cycle of the LOOP, but the Stored Procedure terminates on occurrence of ...
0
votes
1
answer
38
views
In Redshift/PLPGSQL stored procedure : how to store the result of show table into a variable to reuse it?
I'm trying to automate the generation of creation table script.
How to deal with this
CREATE OR REPLACE PROCEDURE talend_retail.generate_sql_create_table_by_name(
schema_name VARCHAR(100),
...
0
votes
3
answers
53
views
SQL Error [42804]: ERROR: structure of query does not match function result type
CREATE OR REPLACE FUNCTION display_columns_data(table_name varchar, columns_to_display varchar[])
RETURNS TABLE (result_row json) AS
$$
BEGIN
RETURN QUERY EXECUTE
'SELECT ' || array_to_string(...
2
votes
2
answers
77
views
How do I Iterate through individual fields of OLD records in Postgres plpgsql trigger?
Is there a way to iterate through individual fields of OLD records in a Postgres plpgsql trigger?
I want to write a trigger function that logs all the changes that were made to the particular fields ...
0
votes
1
answer
44
views
Concatenate table name postgresql function
I want to use a function that assign the value of a query to a variable. However the name of the name of the table is derived from the output of previous lines in my function.
ref_A int
ref_B int
......
0
votes
1
answer
41
views
pgsql function cannot execute
I tried to execute this function:
CREATE OR REPLACE FUNCTION public.increases_chrono(
chrono_seq_name text,
chrono_id_name text)
RETURNS TABLE(chrono_id bigint)
LANGUAGE 'plpgsql'
...
0
votes
0
answers
80
views
Wrong order of items in pg_restore
I have an issue that takes place during restore of postgresql database. During this process creation of extension aws_s3 fails because it depends on plpgsql extension which it seems to be created ...
0
votes
0
answers
60
views
How to iterate over results of query in postgresql
i am new to postgresql trying to itterate the select query. first i want use select query after getting the value want to iterate the result set and process this this record set and insert one by one ...
0
votes
0
answers
37
views
call plpython3u function in a plpgsql function
I have a simple function in plpython3u that makes a request to a server, like this:
CREATE OR REPLACE FUNCTION public.f_request_http(
metodo TEXT
, url ...
0
votes
2
answers
80
views
Convert dynamic T-SQL to dynamic PostgreSQL PL/SQL
I wrote the SQL Server stored procedure usp_getrowcounts, shown below, that dynamically gets a row count from each table, as tables come and go, and need the equivalent dynamically generated and ...
0
votes
1
answer
81
views
Is there a way to create a dynamic number of columns in a plpgsql function return value?
I have the following SQL code:
SELECT
dip
|| ' '
|| (
SELECT lib1 || ' ' || lib2
FROM bds
WHERE dip_s = dip
) AS label,
COUNT(dip) AS freq
FROM ...