PGSQL file backend updates on files with multiple references
The Problem
When you store a file that is an exact copy of a file already in the database (uses md5sum to compare) instead of storing the file chunks multiple times, it just creates multiple references to the file contents. The problem here is, when we update the contents of a file we just blindly perform a PDO UPDATE query to write the contents, which means that the contents of all references is updated which might not be the desired result.
The Solution
When updating file contents we just need to look and see if there are multiple references to the content. If there is only one references, then we can just do the update as normal. If there are multiple, we will need to check the MD5 sum and if it already exists, update the references to point to that (rather than insert new data). If the MD5 sum doesn't exist, then we can just do the INSERT of the new data and update the references.