As it is expensive to parse commands again each time the parse infos are cached for each connection.
An existing parse info can be used for a command if:
A parse info is in shared use by several SQL statements, and must be therefore guarded against concurrent access.
A parse info is create by preparing a statement. Here, it is checked whether there is a parse info available, and if not the statement is parsed. Then, the parse info is inserted into the cache - hereby setting its flags appropriately.
During this process the parse info is locked by the creator.
When a matching parse info is found on prepare, the statement simply attaches to it. This means that the reference count of the parse info is increased. Attaching to a parse info also propagates the parse info also to the top of the LRU list.
When a statement is destroyed, it detaches from the parse info. If the parse info is cached, the reference count is simply decremented by one.
If it is not cached, it is checked whether this was the last referencee, and if yes, the resources are freed.
On the insertion of a new ParseInfo into the cache, the cache size is checked. If the cache is full, the ParseInfo at the end of the LRU list is displaced, hereby setting its 'cached' flag to false. If its reference count is 0, it is deleted, otherwise that task is done by the last statement that detaches from the ParseInfo.