As a major component in the development of better flow models for the "cold production" process in heavy oils, formation of a foamy fluid phase during flow of a liquid containing dissolved gas toward a wellbore is considered. The appearance of gas bubbles in the flowing fluid is described by the theory of nucleation. Unlike previous approaches, the foamy state is interpreted as a metastable state which starts developing at pressures below the bubble-point of a gas-fluid phase diagram. The amount of gaseous phase is determined kinetically, that is, by the rate of nucleation. Mass balance analysis for the compound foamy fluid gives the spatio-temporal evolution of the foamy zone which starts growing around the wellbore. Fluid pressure in the foamy zone exhibits a steeper gradient, compared to the non-foamy flow. The conditions of sand production instability via tensile failure at the foamy front are also determined, and this constitutes a condition for liquefaction and loss of effective stress in the granular medium.