unique constraint violations - Mailing list pgsql-general
From | pg noob |
---|---|
Subject | unique constraint violations |
Date | |
Msg-id | CAPNY-2WU76u8-YVJHVgcxc8Mr89GZ0DZEDC2P1NZsQHqyLtb-A@mail.gmail.com Whole thread Raw |
Responses |
Re: unique constraint violations
|
List | pgsql-general |
Generally it starts by checking if a value already exists, if not - try to insert it, which may cause a unique violation, and if a constraint violation occurs, retrying the process again.
Schema | public
Name | executehandleunique
Result data type | bigint
Argument data types | querystring character varying
Type | normal
Volatility | volatile
Owner | sysdba
Language | plpgsql
Source code | --
: -- This function executes an arbitrary query and catches any unique violation
: -- that may occur without causing the database transaction to get rolled back.
: -- Warning: Don't use this for queries that may be generated from user input
: -- as it can create a security problem.
: --
: BEGIN
: BEGIN
: RAISE NOTICE 'executing "%"', querystring;
: execute querystring;
: EXCEPTION
: WHEN unique_violation THEN
: return 2; -- status value 2 indicates unique constraint violation occurred
: END;
: RETURN 0; -- status 0 indicates success
: END;
:
Description |
Our application is a closed system, meaning that it does not accept input from untrusted sources.
Is this function safe? (Knowing that we do not handle any untrusted input.)
Are there any potential problems with this approach?
Is there a better way to avoid unique constraint errors and the transaction rollback?
Thank you.
Thank you.
pgsql-general by date: