The government disclosed today that Queen Elizabeth's personal police bodyguard resigned Saturday after admitting he had been involved in a lengthy affair with a male prostitute. The security breach is at least as serious as that posed by the intruder who made his way into the queen's bedroom 10 days ago.

The policeman, Commander Michael Trestrail, 50, was a "classic potential blackmail victim," one official said tonight. After undergoing scrutiny of his professional and personal qualifications, Trestrail, 50, became the queen's bodyguard in 1973 and in 1979 was put in charge of all policemen who guard members of the royal family when they are outside their palace homes.

He was a familiar, dapper figure at the queen's side in most of her public appearances and maintained an apartment at the palace in order to be available when she went on any excursion.

Trestrail told his superiors, sources said, that he had used the palace apartment to meet his lover.

The prostitute apparently took his story last week to a British newspaper. The paper alerted police who confronted Trestrail.

The resignation is not directly connected to the case of Michael Fagan, the man who eluded security in Buckingham Palace on July 9 and spent 10 minutes with the queen in her bedroom before help arrived. Trestrail was not responsible for the queen's safety when she was in the palace.

But coming so soon after that episode, the Trestrail resignation is certain to lead to a major overhaul of all aspects of the queen's security. In announcing the latest revelation to Parliament, Home Secretary William Whitelaw said the consequences of both the Trestrail and Fagan cases would be covered in a full government report Wednesday based on extensive investigations.

Meanwhile, two senior police officials who oversee royal protection were transferred tonight.

In yet another security problem for the government, Downing Street announced this afternoon that Prime Minister Margaret Thatcher would make a statement in Parliament Tuesday responding to widespread rumors that a major new spy scandal is about to break. Last week a former employe of Britain's General Communication Headquarters at Cheltenham, base for the country's extensive international electronic eavesdropping network, had been charged under the Official Secrets Act with transmitting information to an enemy. Details of the charge have not been released.

Official sources have encouraged interest in the case by telling reporters that "its seriousness should not be underestimated." The communications headquarters works closely with U.S. intelligence. The suspect, Geoffrey Prime, is a Russian speaker who worked at the center from 1968 until 1978.

Fagan, the 30-year-old unemployed man who shinnied up a drainpipe and went to the queen's bedroom, appeared in court today and was held without bond on a charge of stealing wine in a previous visit to the palace June 7 and other unrelated domestic offenses. Prosecutors said Fagan would not be charged in the matter of the queen's bedroom, because under British law there is no evidence that his tresspass had criminal intent.

At several points in the magistrate's hearing, Fagan erupted angrily. At the first mention of the queen by his lawyer, Maurice Nadeem, he shouted, "I told you not to mention anything about the queen's bedroom . . . I don't want her brought into it. I would rather plead guilty than have her name mentioned in court."

The prosecutors said Fagan's break-ins at the palace were part of a series of "irrational" acts he had committed lately, including at least two attempted suicides and an assault on his stepson.

The Trestrail case may prove even more damaging to confidence in police security than the Fagan episode.

A police officer with 30 years' experience, Trestrail, a bachelor, joined the palace staff 16 years ago and was promoted to bodyguard for Prince Philip before being assigned to the queen.

Nothing is known about the male prostitute or how long his affair with Trestrail went on. An unidentified man telephoned newspaper offices last week, including The Washington Post London bureau, and said he was about to implicate the head of security at Buckingham Palace in a homosexual relationship. He offered no details.

Apparently, the British tabloid, The Sun, made further contact with the caller who told his story.

Scotland Yard announced today that Trestrail had resigned for "personal reasons." A short time later, Whitelaw, who oversees the police, gave the reason in Parliament.

Whitelaw acknowledged that Trestrail had been "positively vetted" meaning that he had been submitted to security checks. It was evident from Whitelaw's cryptic statement that those checks had failed to turn up any problem.

In addition to the transfer of senior officers today, Trestrail was replaced by Christopher Hagen, a 37-year-old officer who had been serving as Prince Philip's bodyguard.

Homosexuality between consenting adults is not illegal in Britain. But the fact that Trestrail never revealed his sexual preference made him a blackmail target, officials said, and his use of the palace apartment for meetings with a male prostitute was also considered intolerable.