What is the origin and basis for the Catholic teaching regarding Mary's vow of celibacy prior to her betrothal? In the answers to this question it is asserted that Mary had taken a vow of celibacy prior to her betrothal to Joseph.  Thus, Joseph already knew that he and Mary would never have marital relations prior to the Annunciation.

I have never heard this before, so I am wondering when this teaching originated and how close that was to the birth of Christ.  Also, were vows of celibacy inside marriage recorded as being practiced at any time in Jewish history?
Nobody will mind if I quote a 1850 year old document at length here right? If you don't read it with any context it might seem kind of strange.  The Protoevangelium of St. James is definitely apocryphal, but that doesn't make it bad and it certainly shows evidence of an age old tradition (and a good one).


  
  And her months were added to the child. And the child was two years old, and Joachim said: Let us take her up to the temple of the Lord, that we may pay the vow that we have vowed, lest perchance the Lord send to us, and our offering be not received. And Anna said: Let us wait for the third year, in order that the child may not seek for father or mother. And Joachim said: So let us wait. And the child was three years old, and Joachim said: Invite the daughters of the Hebrews that are undefiled, and let them take each a lamp, and let them stand with the lamps burning, that the child may not turn back, and her heart be captivated from the temple of the Lord. And they did so until they went up into the temple of the Lord. And the priest received her, and kissed her, and blessed her, saying: The Lord has magnified your name in all generations. In you, on the last of the days, the Lord will manifest His redemption to the sons of Israel. And he set her down upon the third step of the altar, and the Lord God sent grace upon her; and she danced with her feet, and all the house of Israel loved her.
  


So far it's Our Lady just being a cute little kid and it's her parent who vow to give her to the temple (ala Hannah who she herself definitely prefigures).


And her parents went down marvelling, and praising the Lord God, because the child had not turned back. And Mary was in the temple of the Lord as if she were a dove that dwelt there, and she received food from the hand of an angel. And when she was twelve years old there was held a council of the priests, saying: Behold, Mary has reached the age of twelve years in the temple of the Lord. What then shall we do with her, lest perchance she defile the sanctuary of the Lord? 


and here it's the priests who don't want Mary to be defiled, so they try to marry her off to an old guy.


  And they said to the high priest: You stand by the altar of the Lord; go in, and pray concerning her; and whatever the Lord shall manifest unto you, that also will we do. And the high priest went in, taking the robe with the twelve bells into the holy of holies; and he prayed concerning her. And behold an angel of the Lord stood by him, saying unto him: Zacharias, Zacharias, go out and assemble the widowers of the people, and let them bring each his rod; and to whomsoever the Lord shall show a sign, his wife shall she be. And the heralds went out through all the circuit of Judæa, and the trumpet of the Lord sounded, and all ran.


So, in the end, all the pieces fit snugly with unchanging Sacred Tradition concerning the Perpetual Virginity of Mary.  Even if the Holy Spirit didn't choose to assert Himself through the truths contained in this ancient text.  The evidence of a tradition is certainly evidence of a tradition.  

That she, of her own accord, decided to be a temple virgin or not isn't particularly important.  Whatever God did for her, He preserved her from following her passions and going off and doing something sinful so he could use her, whom he most loved of all His creatures, for something amazing.