I have a php script to insert new parts into a postgres database. I want to check if the part exists before it is created. How can I do this. I am using the usual INSERT INTO clause.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You
SELECTfirst, to know if you mustINSERTorUPDATE, like:Re Milen’s comment: great point! With the default PostgreSQL transaction isolation level (
READ COMMITTED) it’s possible that another process inserts (and commits) “your” part after yourSELECT, but before yourINSERT.For the kind of apps we build, we normally just catch the DB exception, and do something about it (inform the user, retry). Or you can go with
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE. See Transaction Isolation.