sqlpostgresqlcasesavepoints

Usage ROLLBACK TO SAVEPOINT with condition


Is it possible to ROLLBACK TO SAVEPOINT with CASE? My query is

BEGIN;
SAVEPOINT my_savepoint;
INSERT INTO DPoint (uuid) VALUES ('5547f4b7-00b3-4aac-8ceb-c9ca163a0214')
ON CONFLICT (uuid) DO NOTHING;
WITH
 ins1 AS (INSERT INTO Point (latitude, longitude, srid)
VALUES (37.251667, 14.917222, 4326) RETURNING id),
 ins2 as (INSERT INTO SPoint (idPt, uuiddpt)
     VALUES ((SELECT id FROM ins1), '5547f4b7-00b3-4aac-8ceb-c9ca163a0214') RETURNING id),
 ins3 as (INSERT INTO Distance (idSpt, uuiddpt)
     VALUES ((SELECT id FROM ins2), '5547f4b7-00b3-4aac-8ceb-c9ca163a0214'))
INSERT INTO DPointTS (uuid, type, name, idPoint)
VALUES ('5547f4b7-00b3-4aac-8ceb-c9ca163a0214', NULL, NULL, (SELECT id FROM ins1));

SELECT CASE WHEN
(SELECT uuid FROM DPoint
WHERE uuid = '5547f4b7-00b3-4aac-8ceb-c9ca163a0214' )
is not NULL THEN ROLLBACK TO SAVEPOINT my_savepoint END;
COMMIT;

My idea is:

When trying to insert once again DPoint.uuid = '5547f4b7-00b3-4aac-8ceb-c9ca163a0214', it is no need to insert Point, SPoint, Distance, DPointTS. So I would like to ROLLBACK these insertions to my_savepoint in transaction. Maybe any idea in what way I have to rewrite my code?

EDIT:

SELECT uuid IS NULL AS is_not_uuid FROM DPoint WHERE uuid = '5547f4b7-00b3-4aac-8ceb-c9ca163a0214';
\gset
\if :is_not_uuid
    \echo 'insert row to DPoint'
    INSERT INTO DPoint (uuid) VALUES ('5547f4b7-00b3-4aac-8ceb-c9ca163a0214');
    ... 
    my INSERT query

\endif

I update my strategy without SAVEPOINTs - if SELECT query returns TRUE I evaluate all insertions. What way I am execute the query, only in command line? When trying in console.sql in DataGRIP it throws an error - it honestly execute all the rows and fails in INSERT INTO DPoint (uuid)... in case the point is already exists. I would like to execute the statements in one way


Solution

  • No, you cannot do it like that.

    You will have to write client code and use conditional processing.

    For example with psql:

    -- set the variable "want_rollback" to TRUE or FALSE
    SELECT uuid IS NOT NULL AS want_rollback
    FROM dpoint
    WHERE uuid = '5547f4b7-00b3-4aac-8ceb-c9ca163a0214' \gset
    \if :want_rollback
    ROLLBACK TO SAVEPOINT my_savepoint;
    \endif
    

    See the documentation for details about \if:

    \if expression
    \elif expression
    \else
    \endif

    This group of commands implements nestable conditional blocks. A conditional block must begin with an \if and end with an \endif. In between there may be any number of \elif clauses, which may optionally be followed by a single \else clause. Ordinary queries and other types of backslash commands may (and usually do) appear between the commands forming a conditional block.

    The \if and \elif commands read their argument(s) and evaluate them as a boolean expression. If the expression yields true then processing continues normally; otherwise, lines are skipped until a matching \elif, \else, or \endif is reached. Once an \if or \elif test has succeeded, the arguments of later \elif commands in the same block are not evaluated but are treated as false. Lines following an \else are processed only if no earlier matching \if or \elif succeeded.

    The expression argument of an \if or \elif command is subject to variable interpolation and backquote expansion, just like any other backslash command argument. After that it is evaluated like the value of an on/off option variable. So a valid value is any unambiguous case-insensitive match for one of: true, false, 1, 0, on, off, yes, no. For example, t, T, and tR will all be considered to be true.

    Expressions that do not properly evaluate to true or false will generate a warning and be treated as false.

    Lines being skipped are parsed normally to identify queries and backslash commands, but queries are not sent to the server, and backslash commands other than conditionals (\if, \elif, \else, \endif) are ignored. Conditional commands are checked only for valid nesting. Variable references in skipped lines are not expanded, and backquote expansion is not performed either.

    All the backslash commands of a given conditional block must appear in the same source file. If EOF is reached on the main input file or an \include-ed file before all local \if-blocks have been closed, then psql will raise an error.

    The same page will also explain \gset.