MERGE INTO rlarp.walk w USING ( SELECT $1 ship_cust, $2 bucket, $3 notes) as i ON i.ship_cust = w.ship_cust WHEN MATCHED THEN UPDATE SET bucket = i.bucket ,notes = i.notes WHEN NOT MATCHED THEN INSERT (ship_cust, bucket, notes) VALUES (i.ship_cust, i.bucket, i.notes )