Testing makes no sense, just include the "test" in the where clause:
INSERT INTO silly_table(the_text)
'literal_text'
WHERE NOT EXISTS (
SELECT *
FROM silly_table
WHERE the_text = 'literal_text'
);
Now, you'll make the test only when it is needed: at the end of the statement the row will exist. There is no such thing as try.
For those that don't understand testing makes no sense: testing would make sense if the situation after the test would not be allowed to change after the test. That would need a test&lock scenario. Or, even worse: a test inside a transaction.
UPDATE: version that works (basically the same):
DROP TABLE exitsnot CASCADE;
CREATE TABLE exitsnot
( id SERIAL NOT NULL PRIMARY KEY
, val INTEGER -- REFERENCES something
, str varchar -- REFERENCES something
);
INSERT INTO exitsnot (val)
SELECT 42
WHERE NOT EXISTS (
SELECT * FROM exitsnot
WHERE val = 42
);
INSERT INTO exitsnot (str)
SELECT 'silly text'
WHERE NOT EXISTS (
SELECT * FROM exitsnot
WHERE str = 'silly text'
);
SELECT version();
Output:
DROP TABLE
NOTICE: CREATE TABLE will create implicit sequence "exitsnot_id_seq" for serial column "exitsnot.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "exitsnot_pkey" for table "exitsnot"
CREATE TABLE
INSERT 0 1
INSERT 0 1
version
----------------------------------------------------------------------------------------------
PostgreSQL 9.1.2 on i686-pc-linux-gnu, compiled by gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3, 32-bit
(1 row)
WHERE (NOT) EXISTS(...)term to the query.