9th century: around 805, Venice accepted Frankish suzerainty. Subsequently it was repudiated and after the siege by Pippin failed, Venice became officially Byzantine. All de jure of course, but technically Venice recognized someone else as sovereign when convenient. [I haven't found an answer to when Venice dropped the appearance of being a Byzantine outpost.]
It was in 804, when the pro-Frankish faction took (very temporarily) the power and managed to elect Obelerio degli Antoneri as doge. When Obelerio tried to enforce his authority with the support of Pepin the city rebelled and Obelerio was forced to flee the city with his family. Pepin tried to set up a siege but after six months the only result was the loss of a substantial portion of the Frankish army to sickness. In 805 Charlemagne and Nicephorus signed an agreement recognizing the respective sphere of influence (not borders which would have been too much for the thinking of the time), and Venice was recognised by both empires as being a Byzantine "subject" (as it was anyway before the short term of the unlamented Obelerio: after all being Byzantine "subjects" was no big deal as removed as Venice was from Constantinople and it brought lower import and export duties on Venetian traders in the City). Venice formally remained a Byzantine "subject" until 1084 when the formal independence of Venice was recognised by the emperor as a reward for Venetyian assistance in the war against the Normans of Apulia (and since the Chrysobulla exempting Venetian traders from any tax or duty in the ERE was granted on the same date there was no more any advantage in being Byzantine "subjects). As far as the Frankish side, it looks like they accepted with good grace Venice status, and not only rescinded the prohibition for Venetian traders to trade in the Pentapolis and on the Adriatic coast but granted new trading privileges (mostly linked to attendance at the great trade fair of Pavia).
It's true that Venice never elected a foreign Doge, but I don't think there was ever an occasion where it was faced with the choice between a white knight foreigner and conquest by a different power. Not sure which occasions you meant during which Venice's back was literally to a wall. Pippin's siege and the War of Cambrai were the times of greatest danger that I can think of. Venice took the Byzantine option after the first; there wasn't that kind of offer on the table during the second.
I agree that the war of Cambrai was one of the worst circumstances for Venice, but such widespread coalitions seldom last long enough. Pepin's siege was no big deal: he could have stayed in the marshes for 10 years rather than the 6 months he actually did and the only result would have been more deaths caused by marsh fevers and typhoid fever. Most likely the worse risk run by the Serenissima was the war of Chioggia, when the republic was certainly pushed to her limits and beyond. Another bad time was in the 14th century when Hungary was ascendant and Venice lost her Dalmatian holdings to their king. The Hungarians raided heavily into Friuli and Veneto and were allied to Verona and Padua: nothing came out of it but it was rather a nervous time. Same as when Suleiman ordered the siege of Corfu (1537) or when Crete was lost. In none of these cases Venice ever considered a foreign ruler, or even recognising the overlordship of the HRE or the Papacy.
As others have said, Portugal isn't the easiest power for Venice to get a union with. There was a Venetian king of Hungary (Pietro Orsoleo) in the eleventh century. If he had been elected doge, there could have been a personal union during his lifetime.
Technically he could not anyway: IIRC a Venetian citizen would loose all his rights if he became a ruler of a foreign country. In practice there is very little likelyhood that the Senate might give a majority to a foreign ruler (and ever if it happened there would have been an immediate and bloody revolution: Venetians were too proud of their government and traditions, and the Arsenalotti were the proudest of the lot.