Gloucestershire 463 (Bracey 152, Higgins 103) and 137 for 6 (Howell 44) drew with Glamorgan 250 (Hemphrey 60, Wagg 50) and 481 for 8 dec (Selman 140, Labuschagne 137, Hemphrey 58)
Glamorgan fought back so well after they were asked to follow on 213 runs adrift at the start of the third day, that it was Gloucestershire who had to defend and avoid defeat at the end of an absorbing championship game at Spytty Park in Newport.
Glamorgan batted so well in their second innings, they had the luxury of declaring and setting the visitors a target, and with the visitors at one stage faltering at 41 for 4 in the 13th over they would have had visions of an improbable victory.
Glamorgan began the final day on 359 for 1, with Nicholas Selman and Marnus Labuschagne going on to achieve career-best scores. Selman, though, only added two more runs to his overnight score before edging David Payne to wicketkeeper Gareth Roderick for 150.
Labuschagne went to score 137 before padding up to a ball from Matt Taylor that nipped back off the seam. There was more assistance for the seamers on the final day as the ball moved off the pitch while the cloud cover also assisted swing.
Only Graham Wagg offered resistance towards the end of the innings and when Glamorgan declared on 481 for 8, Wagg was undefeated on 25. The declaration required Gloucestershire to score at fractionally over five runs an over, but batting was not as straightforward as it had been in the first innings.
Timm van der Gugten struck with his first ball and the seventh of the innings when Chris Dent was caught low at slip, and with Marchant de Lange bowling at a brisk pace at the other end, Gloucestershire lost their second wicket when Miles Hammond edged to the substitute wicketkeeper Tom Cullen.
James Bracey, who scored 152 in the first innings, began confidently by striking four boundaries, but then succumbed to a rash shot when he slashed at a wide delivery from Wagg and was caught at second slip.
Two runs later, Kieran Bull's off-spin accounted for George Hankins, who was bowled by a ball that turned from the rough and reduce the visitors to 41 for 4. Glamorgan were then thwarted by Gareth Roderick and Benny Howell, who shared 62 for the fifth wicket but - more importantly - were together for 22 overs.
The partnership was broken by Bull, who had Howell well caught at slip by Lloyd diving forward, and Ryan Higgins almost went next ball when Labuschagne just failed to hold on at bat-pad.
Glamorgan needed to take five more wickets in the final 16 overs and, with the ball turning for Bull and Labuschagne, the spinners assumed control with Labuschagne dismissing Roderick, who had faced 86 balls for 19.
Higgins and Graeme van Burren held out for the final 8.5 overs, but Glamorgan would have been heartened by their battling performance before their next championship game, which starts at Derby on Sunday.