I found that the db user had the correct permissions and I am not a fan of changing core code regardless of if it is moved to local.
The error was experienced following a command line reindex after an upgrade from 1.4 to 1.6.
php indexer.php -reindex catalog_url
An error occurred while saving the URL rewrite
The was paired with the following Exception in the log:-
exception 'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'category/360-1-6' for key 'UNQ_CORE_URL_REWRITE_ID_PATH_IS_SYSTEM_STORE_ID'' in httpdocs/lib/Zend/Db/Statement/Pdo.php:228
I ran the following query via mysql to locate the problem record (note how the query relates to the Exception), it transpired this related to a category that had been renamed...
QUERY: select * from core_url_rewrite where id_path = "category/360";
I note that this is a category url rewrite and that the product_id record is 0 as opposed to NULL, I suspect this is the issue and if you are having this problem it would be interesting to see if this is the same for you, run the following:
QUERY: select * from core_url_rewrite where product_id=0;
When I ran the above query it returned one record the problem record.
The solution I found was to delete the problem record...
Now catalog_url reindex works with out an issue.
Running the below query returns a new record with product_id of NULL and represents the new URI path. The old URI results in a 404 as opposed to redirecting as it should do.
QUERY: select * from core_url_rewrite where id_path = "category/360";
I believe this problem is a single data record issue, probably following an upgrade. I do not agree that changing the URL.php or truncating core_url_rewrite is correct.
I have since rebuilt the index for catalog_url twice and a third as part of a complete reindex. All returned: Catalog URL Rewrites index was rebuilt successfully
I have documented my findings your circumstances maybe different but this goes a little way to explaining how you might locate the issue and fix it.