Gautam Gambhir's maiden century for Essex helped set Gloucestershire an improbable 432 runs for victory on day three of their LV= County Championship fixture at Bristol.
India opening batsman Gambhir (106) shared a 201-run partnership with Jaik Mickelburgh (86) before Essex eventually declared in their second innings on 285-4.
Gloucestershire closed on 19-0 without loss when bad light set in, with much resting on captain Michael Klinger (two not out) and Chris Dent (16no) heading into the final day.
An unbeaten maiden first-class 50 by Craig Miles, part of an unlikely 78-run stand for the last wicket with Graeme McCarter, had earlier ensured Klinger's side avoided following on.
Miles, 19, is probably only batting at number 11 due to his tender years and he became only the sixth Gloucestershire player to make a half-century in that position with some handsome drives and sensible back-foot maneuvering.
The hosts were eventually all out when the similarly impressive McCarter (20) was run out by Reece Topley's direct hit from deep backward square, meaning the deficit stood at 146.
That had stretched to 178 by the start of the second session, heightening Gloucestershire's need for quick wickets to avoid being cut adrift in the contest.
Instead Mickleburgh and Gambhir struck the right balance between attack and defence, the latter the first to reach 50 off 72 balls with the aid of seven boundaries.
Gambhir averages 44.18 from 51 Test matches and had too much class for a young attack, who are showing signs of weariness as September looms.
Mickleburgh required 116 deliveries to achieve the same feat, and six overs later Gambhir signalled the visitors' intent when he launched Benny Howell for six into the building site.
A virtually identical shot by Mickleburgh yielded the same result before Gambhir reached a 141-ball hundred with a single off David Payne, and by tea Essex had amassed a huge advantage of 337 at 191 without loss.
Gambhir departed eight balls after the resumption, hooking Miles to Hamish Marshall on the square-leg boundary, having faced 149 balls and hit 12 fours and a six.
Mickleburgh holed out to the same fielder off Tom Smith in the next over and spinner Smith dismissed his namesake Greg for 40 via Alex Gidman's tumbling catch at backward point before Owais Shah (eight) was pinned leg before by Miles.
Ryan ten Doeschate made 23 not out at a run a ball before the declaration came and Gloucestershire's reply started steadily.