1

I am creating a function that will create a new table and insert informations about that table into other tables.
To create that table I am using the

CREATE TABLE IF NOT EXISTS

statement. Sadly it does not update the FOUND special variable in PostgreSQL nor can i find any other variable that would be updated.

Is there any way in PL/PGSQL to know whether that statement created a table or not?

The target of it is to not to double the informations in the other tables.

4
  • 1
    It's probably more reliable to make the insert that follows the CREATE TABLE not insert anything if the rows are already there (e.g. using on conflict do nothing) Commented Mar 5, 2018 at 14:56
  • sadly there is no conflict. Also i can not make any check to see if the rows are there. The CREATE TABLE IF NOT EXISTS will fail to create the table simply because the name is already taken. The informations inserted in other tables may vary. Commented Mar 5, 2018 at 15:09
  • 1
    But that "information inserted in other tables" stores information about tables created, right? So it needs to contain at least the table name that you tried to create, so it should be possible to come up with INSERT statements that do not insert anything if information for that table is already stored in that "other table". But without actually seeing the whole picture this is impossible to answer. Commented Mar 5, 2018 at 15:13
  • Nah! You are right, I am dumb! Of course i am saving the name of the table! The whole picture is a little to big to show (about 100 lines of code) so i tried to describe the problem as good as i could. I see i did an enough good job :) Thank you! Commented Mar 5, 2018 at 15:16

2 Answers 2

2

You may use CREATE TABLE AS in combination with ON_ERROR_ROLLBACK:

BEGIN;

-- Do inital stuff

\set ON_ERROR_ROLLBACK on
CREATE TABLE my_table AS 
    SELECT id, name FROM (VALUES (1, 'Bob'), (2, 'Mary')) v(id, name);
\set ON_ERROR_ROLLBACK off

-- Do remaining stuff

END;

To put it bluntly, with \set ON_ERROR_ROLLBACK on postgres will create a savepoint before each statement and automatically rollback to this savepoint or releasing it depending on the success of that statement.

The code above will execute initial and remaining stuff even if the table creation fails.

Sign up to request clarification or add additional context in comments.

3 Comments

Let's say the insertion fails. Will the function then do the rollback and stop working? Or will it rather finish it's code that comes after the Rollback?
@Shuumi: It will finish with the code after the rollback.
That is great! Thank you!
0

No, there are not any information if this command created table or not. The found variable is updated after query execution - not after DDL command. There is guaranteed so after this command, the table will be or this command fails to an exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.