I have committed the ref counting for pristine texts (r1058523) and have had a bit more insight into when to perform the deletion.
Deleting unreferenced texts on closing a "wcroot" is too late - too large a granularity - for the way the WC code is currently structured because this doesn't happen until a (long-lived) pool is cleaned up, as Bert pointed out. Deleting unreferenced texts after running the Work Queue is too soon - too fine a granularity - for the way "commit" is currently structured. A simple test of this approach showed that by the time the post-commit processing tries to install a reference to a pristine text whose checksum was noted earlier, in some cases that pristine row has already been purged. At the moment I think the practical solution is to insert calls to the pristine cleanup at the end of each main libsvn_wc public API that may have freed up one or more pristines. Wherever we do it, if it's at all frequent, the search for unreferenced pristines needs to be efficient. At present I use "SELECT ... FROM PRISTINE WHERE refcount = 0". I believe that can most easily be made efficient by adding an index on the 'refcount' column, so that's what I intend to do. - Julian