Shanghai - Roger Federer admitted that he came "very close" to defeat as he was given a huge scare before seeing off Daniil Medvedev 6-4, 4-6, 6-4 to open his Shanghai Masters defence on Wednesday.
The 37-year-old Swiss top seed faces Spain's unseeded Roberto Bautista Agut on Thursday in the last 16 after what was an unconvincing start against the Russian.
The 20-time Grand Slam champion initially wasted little time stamping his mark on the tournament, breaking the 22nd-ranked Medvedev in the first game.
But Medvedev, 22, fresh from winning the Japan Open, broke back and enjoyed even more luck on Federer's serve in the second set to force a third.
In what was the first meeting between the pair, Federer broke in the ninth game of the decider to finally destroy Medvedev's resolve in just under two nail-biting hours.
"In the third set I really started to figure it out," said Federer, who was roared on by a band of fanatical fans decked out in red tops.
"How aggressive or how tactical I wanted to make the match, and by trying everything, I found a solution at the end."
Federer, who was uncharacteristically flustered at times, said he was pleased with his performance, but admitted it had been a nervy one.
"Of course I feel pressure from the tournament organisers, the fans, my team, myself, because I know that sometimes coming through a tough first round can be the key to what's to come afterwards," said the 2017 champion.
"Today was very close to maybe not getting that next step."
With the exception of injured world number one Rafael Nadal, Federer's chief rivals are all still in the mix.
Juan Martin del Potro and Alexander Zverev both defied illness to join Federer and Novak Djokovic in the next round.
Del Potro, the third seed, said that he is suffering from a virus and reviewing his health "hour by hour", after labouring past France's Richard Gasquet.
The Argentine lost Sunday's final of the China Open in Beijing to unseeded Nikoloz Basilashvili and afterwards revealed that he had been unwell.
Three days on and the 30-year-old continues to be under the weather, but just about did enough to see off Gasquet 7-5, 7-6 (9/7).
Between coughs at his post-match press conference, Del Potro said: "It wasn't easy to play for me today, I did what I can. I didn't run too much.
"But I'm going like hour by hour and see how I'm feeling," added Del Potro, who plays 13th seed Borna Coric of Croatia next in Shanghai.
"I got lucky that I won, but tomorrow will be a different day.
"If I have a good night today and I have a good recovery tomorrow morning, maybe I will feel a little bit better for tomorrow night and play better than today."
The 21-year-old Zverev fought off a cold - he has had a sore throat and blocked nose for a few days - and the raw power of Basilashvili, winning 7-5, 6-4.
The fourth seed will play Australia's world-ranked 33 Alex de Minaur.
Zverev said that Basilashvili, who is up to a career-high ranking of 23, "knows how to play, definitely".
"Probably the hardest-hitting player I have ever played against," said Zverev.
Results from the ATP Shanghai Masters on Wednesday (x denotes seed):
Roger Federer (SUI x1) bt Daniil Medvedev (RUS) 6-4, 4-6, 6-4
Roberto Bautista Agut (ESP) bt Mackenzie McDonald (USA) 3-6, 6-4, 6-1
Sam Querrey (USA) bt Taylor Fritz (USA) 6-3, 6-7 (4/7), 7-5
Kei Nishikori (JPN x8) bt Wu Yibing (CHN) 3-6, 6-0, 6-3
Juan Martin del Potro (ARG x3) bt Richard Gasquet (FRA) 7-5, 7-6 (9/7)
Kyle Edmund (GBR x11) bt Andreas Seppi (ITA) 6-3, 6-4
Alex De Minaur (AUS) bt Benoit Paire (FRA) 6-4, 6-3
Alexander Zverev (GER x4) bt Nikoloz Basilashvili (GEO) 7-5, 6-4
Kevin Anderson (RSA x7) bt Mikhail Kukushkin (KAZ) 6-3, 6-2
Stefanos Tsitsipas (GRE x10) bt Karen Khachanov (RUS) 6-4, 7-6 (10/8)
Marco Cecchinato (ITA x16) bt Chung Hyeon (KOR) 4-6, 7-6 (7/5), 7-6 (7/5)