A judge has dismissed a lawsuit against the FBI over a faulty background check that allowed Dylann Roof to buy the gun he used to kill nine people in a racist attack at a South Carolina church.

But U.S. District Judge Richard Gergel also blasted the federal government for what he called its "abysmally poor policy choices" in how it runs the national database for firearm background checks.

"The record reveals that the FBI's background check system is disturbingly superficial, excessively micromanaged by rigid standard operating procedures, and obstructed by policies that deny the overworked and overburdened examiners access to the most comprehensive law enforcement federal database," Gergel wrote in the ruling Monday.

The FBI has acknowledged that a 2015 drug charge should have prevented Roof from buying the gun, according to court documents. However, clerical errors by local law enforcement impeded the background check and federal procedures limited other commonsense ways the examiner could have completed the research, Gergel wrote.

Because the examiner followed agency procedures, the judge had to dismiss the lawsuit by massacre survivors and grieving families. He said the plaintiffs failed to prove their cases fit into narrow exceptions to laws that generally shield federal agencies and government employees carrying out official duties.

He wrote that "glaring weaknesses" in the background check system "are the function of distinct policy choices made by the FBI, not violations of specific legal standards."

Roof was sentenced to death in the June 2015 shooting that killed nine black worshippers at Charleston's Emanuel AME Church. Roof had told a friend he intended to kill people at the historic black church to start a race war.

A lead lawyer for the plaintiffs who sued the FBI didn't respond to a message Wednesday.

Weeks before the church shooting, Roof was arrested on February 28, 2015 by Columbia police on the drug possession charge.

Lexington County Sheriff Jay Koon has told The Associated Press that a jail clerk entered incorrect information for that arrest. That's among the reasons the charge wasn't properly considered during Roof's April 2015 gun purchase.

The errors included wrongly listing the sheriff's office as the arresting agency in the drug case, according to court documents. An examiner with the National Instant Criminal Background Check System found some information on the arrest but needed more to deny the sale, so she sent a fax to the sheriff's office. The sheriff's office responded it didn't have the report, directing her to the Columbia police, the judge wrote.

But according to operating procedures, the examiner couldn't simply search the internet for police department contact information, the ruling says. She had to refer instead to a federal listing of law enforcement agencies. However, the information she needed wasn't there: "Columbia was erroneously omitted from the list," the judge wrote.

After trying the separate West Columbia Police Department, and being told it was the wrong agency, the examiner did nothing more. Gergel wrote that procedures didn't require the examiner to make further efforts. He also noted government policies prevented examiners from using a separate federal database that had the information needed to deny Roof's gun purchase.

Thus, after a three-day delay expired, Roof returned to a West Columbia store to pick up the handgun he later used in the massacre, court documents say.

FBI officials declined to comment Wednesday about the judge's criticism of the background check system, spokeswoman Carol Cratty said.

Defense lawyers previously argued the agency was stymied by legal limits on background checks and the local clerical errors. The system handles tens of thousands of checks daily.

Government lawyers wrote in 2016 that while examiners can impose three-day delays, "the FBI has no authority to prevent a sale if, after those three business days have elapsed, it has not yet found definitive information" to deny the purchase.

___

Drew reported from Raleigh, North Carolina. Associated Press writer Jeffrey Collins in Columbia, South Carolina, contributed to this report.

___

Follow Drew at https://twitter.com/JonathanLDrew