The decision validates critics of the National Security Agency's policy of gathering dialing records of all phone calls made in the U.S.

WASHINGTON — A federal judge has for the first time ruled that the National Security Agency's once-secret policy of collecting the dialing records of all phone calls in the country probably violates the Constitution, a defeat for the government that could alter the political debate over the controversial program and set up an eventual review by the Supreme Court.

Monday's ruling will not immediately stop the NSA's massive data collection program because U.S. District Judge Richard J. Leon immediately stayed it to give the government time to appeal. But the decision nonetheless validated several of the arguments made by critics of the NSA and will probably add force to their push to limit the agency.

The decision marked the first public ruling by a judge who had reviewed the agency's activities since the revelations made last summer by former NSA contractor Edward Snowden. Since then, congressional critics of the NSA's activities have pushed for legislation to rein in the agency but have so far been fought to a standstill by its defenders.

The NSA's collection program gathers so-called metadata — such as the numbers that any particular telephone connects with and the length of calls — for virtually every phone call made in the U.S. The NSA keeps the data for five years.

Agency officials emphasize that, unlike with a wiretap, they do not record the contents of any calls. Moreover, they say, they only search a small portion of the database in any year, looking for numbers linked to those used by suspected foreign terrorists.

Even that limited searching, however, can involve millions of phone numbers, Leon noted, adding that what he called a dragnet almost certainly violates the 4th Amendment's ban on unreasonable searches.

"I cannot imagine a more 'indiscriminate' and 'arbitrary invasion' than this systematic and high-tech collection and retention of personal data on virtually every single citizen for purposes of querying and analyzing it without prior judicial approval," wrote the judge, who was appointed to the federal district court by President George W. Bush.

A Justice Department spokesman said the administration was reviewing the ruling. "We've seen the opinion and are studying it," said Andrew Ames. "We believe the program is constitutional as previous judges have found," referring to the judges of the Foreign Intelligence Surveillance Court, who review the NSA's activities in secret.

The administration can appeal the decision to the U.S. Court of Appeals for the District of Columbia Circuit. Just last week, two Obama appointees were confirmed to that court. From there, the losing side could ask the Supreme Court for a review.

If the ruling is upheld, Leon would have to hold further proceedings before making a final ruling for the plaintiffs, who were led by Larry Klayman, a conservative activist who testified he believes the government is "messing with me."

President Obama has previously directed aides to review the NSA's programs and determine whether changes were needed, but the White House has so far opposed most suggestions for large-scale changes in what the NSA and other intelligence agencies do.

Some defenders of the NSA were caught short by the ruling. "To me this is a big surprise," said Sen. Dianne Feinstein (D-Calif.), chairwoman of the Senate Intelligence Committee.

"This ruling decimates the NSA's defense of its telephone metadata program," said Faiza Patel, a lawyer for the Brennan Center in New York. "It confirms that who we call and when is our business, not the government's."

Among those applauding the ruling was Snowden.

"I acted on my belief that the NSA's mass surveillance programs would not withstand a constitutional challenge, and that the American public deserved a chance to see these issues determined by open courts," he said in a statement released by Glenn Greenwald, the journalist who worked with him on his first disclosures. "Today, a secret program authorized by a secret court was, when exposed to the light of day, found to violate Americans' rights. It is the first of many."

Senate Judiciary Committee Chairman Patrick J. Leahy (D-Vt.), author of one of the bills to restrict the NSA's activities, welcomed the ruling as well. "Americans deserve an open and transparent debate about the constitutionality, efficacy and appropriateness of the government's dragnet collection program," he said.

After the Sept. 11, 2001, attacks, Congress authorized the government to obtain records that were "relevant to an authorized investigation."