Avatar of Theresa Reid
Theresa Reid
Flag for United States of America asked on

Create stored procedure or function which updates a table.

I'm trying to do something super simple...or seems it should be. I have a script that truncates and updates a table...it creates unique combinations across three other tables for a dimension build. I was hoping to create a function/procedure that I could call from an application in a single line, rather than trying to store the sql in my application.

We are using postgresql and sqlworkbench/j. My code to create the table includes a view creation, distinct, union, and in insert into using select.

I was trying:

    CREATE OR REPLACE FUNCTION update_table()
    RETURNS void AS $$
    BEGIN
      CREATE OR REPLACE VIEW ...

     ...;
    END;
    $$ LANGUAGE plpgsql
    IMMUTABLE;

I get the error that "Create Function is not supported".

I'm admittedly a sql rookie and everything I have tried has failed, giving me one error after another. Is there a simpler way to do this? Nothing changes in my create statement...no parameters passed in, nada. It's simply that the underlying transactional data is changing and we're only keeping valid combos, so we trunc and reload the table.

Thanks in advance!
DatabasesPostgreSQLSQL

Avatar of undefined
Last Comment
NerdsOfTech

8/22/2022 - Mon
NerdsOfTech

ASKER CERTIFIED SOLUTION
NerdsOfTech

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Theresa Reid

ASKER
I got to this documentation as well, the issue is the below limitation...which eliminates my ability to run any of the scripts I am trying to put into procedures. Does that mean I'm up that one creek without a paddle?

The SELECT clause can't include any of the following types of clauses:

FROM
INTO
WHERE
GROUP BY
ORDER BY
LIMIT
SOLUTION
NerdsOfTech

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
NerdsOfTech

What does your proposed function do? Maybe there is a predefined function that exists in Redshift... Or maybe a VIEW is in order?
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Theresa Reid

ASKER
The function creates a distinct set of elements for use as a dimension data source. The problem is, when we had it created as a view, the subsequent access of that view by the application was VERY slow. It's essentially a union of distinct members across three very large transactional tables.

Using a combo of an interim view and an INSERT INTO( SELECT...) we were able to get the update speed to be around 20s and a resulting static table that could be access by our application more quickly.

Generally we've found that trying to use views as data sources has been super slow.
NerdsOfTech

Are you able to create your UDF via:
another PostgreSQL tool; or
console/PostgreSQL (change the delimiter to slash, add UDF, change delimiter back to a semi-colon)?

Also, please provide the full function here as we might be able to optimize the XP (execution plan).
earth man2

Views are not necessarily slow, you need to create the right indexes and make sure that they are used.  Another way to go is to use materialized views.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Theresa Reid

ASKER
Thanks guys. I'll try a couple of these options and get back to you. Unfortunately, because of time constraints, I may just put the SQL in the app itself (what I was trying to avoid in the first place) and come back to optimization later. Esp with that views comments. I thought RedShift didn't utilize indexes?
earth man2

I've only just realised that you are probably using amazon redshift.  This is a very different beast from postgresql, so plpgsql and indexes are not part of the mix.  Just goes to show the disadvantages of using this product.
SOLUTION
NerdsOfTech

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
NerdsOfTech

UDF can be added via the database itself or via a tool that supports UDF
Your help has saved me hundreds of hours of internet surfing.
fblack61