Firearms at a gun store in Gainesville, Va. (Matt McClain/The Washington Post)

The FBI’s background-check system is missing millions of records of criminal convictions, mental illness diagnoses and other flags that would keep guns out of potentially dangerous hands, a gap that contributed to the shooting deaths of 26 people in a Texas church this week.

Experts who study the data say government agencies responsible for maintaining such records have long failed to forward them into federal databases used for gun background checks — systemic breakdowns that have lingered for decades as officials decided they were too costly and time-consuming to fix.

As the shooting at a Texas church on Sunday showed, what the FBI doesn’t know can get people killed. In that case, the gunman had been convicted at a court-martial of charges stemming from a domestic violence case. Officials say the Air Force never notified the FBI of his conviction, so when he purchased weapons at a retail store, he cleared the background check.

The FBI said it doesn’t know the scope of the problem, but the National Rifle Association says about 7 million records are absent from the system, based on a 2013 report by the nonprofit National Consortium for Justice Information and Statistics. That report determined that “at least 25% of felony convictions . . . are not available” to the National Instant Criminal Background Check System maintained by the FBI.

Experts who study the data say that estimate can be misleading, because felons often have multiple convictions, so if one is missed, others may still alert authorities to individuals who cannot legally buy a gun.

The government funded a four-year effort beginning in 2008 to try to estimate how many records existed of people who should be barred under federal law from buying a gun but aren’t flagged in the FBI system. That effort was abandoned in 2012 because of the cost.

The National Rifle Association has complained that the federal database is inadequate. The powerful gun rights lobbying group opposes calls for more restrictions on gun buying, arguing that the government should focus instead on making its current background-check system fully functional.

“The shortcomings of the system have been identified, there just seems to be a lack of will to address them,’’ said Louis Dekmar, president of the International Association of Chiefs of Police.

In light of the Texas shooting, Air Force officials have launched an internal review and faulted the staff at an air base for not sending the necessary information to the FBI, but federal officials who work in the database effort say the problem of military nonreporting of domestic violence cases extends far beyond a single base or service branch.

A large number of people convicted of misdemeanor domestic violence — who also are prohibited from buying guns — are absent from the FBI database as well, particularly in states that don’t require fingerprints for such convictions, according to people involved in the work.

According to FBI records, at the end of last year the Pentagon had exactly one active record for a misdemeanor domestic violence conviction in one of the FBI’s main gun background-check databases, though there are two other large databases in which such records could be gathered and for which data was not available. By contrast, the database held nearly 11,000 dishonorable discharge records.

Asked about the gaps in the data, an FBI spokesman said the information law enforcement agencies provide to the FBI “varies greatly across the nation.’’ A potential domestic violence arrest, the spokesman said, “can be one of the most difficult federal firearms prohibitions to establish,” because state or local records often do not indicate whether a particular assault meets the federal definition for domestic violence.

By the end of last year, there were 153,109 domestic violence conviction records in the main FBI database for gun purchase background checks. Officials said that was a marked improvement from years past. In 2008, for example, there were only about 46,000 such records in the system. But FBI officials said they can’t tell how many domestic violence convictions they are still missing. An FBI spokesman declined to say which states are not providing important data into the system.

“In many states, there are literally hundreds of courts and law enforcement agencies that maintain original-source records,” the 2013 report from the National Consortium for Justice Information and Statistics concluded. “There is no practical way to obtain estimates about these records from so many agencies, or to even ask them to take on the burden of counting records.’’

Others who work with gun background-check data said the gaps are clear and have been known for years. Besides felonies and misdemeanor domestic violence convictions, the FBI also misses an untold number of drug addicts, because there is no good mechanism for probation or parole services agencies to share that type of data with the bureau.

That lack of visibility also proved deadly when white supremacist Dylann Roof purchased a gun and killed nine worshipers at a historic black church in Charleston, S.C., in 2015. The FBI later concluded he shouldn’t have been able to buy the weapon because he had recently confessed to drug possession. That confession was not entered into the background-check database, and Roof was able to buy a gun two months before the attack.

“This case rips all of our hearts out,” FBI Director James B. Comey said at the time. “The thought that an error on our part is connected to this guy’s purchase of a gun that he used to slaughter these good people is very painful to us.” He ordered a 30-day review to examine procedures that led to the failure.

Awareness of the problems with the database go back even further. President Barack Obama signed an executive order in 2016 aimed in part at improving the quality of the federal gun background-check database. It directed the FBI to overhaul the background-check system so the checks are more instantaneous and to hire 230 more staff to help with the vetting.

At the Pentagon, the problem goes back decades. In 1997, an internal review found the military was “not consistently submitting criminal history data to the FBI criminal history files.”

The Pentagon’s review found a mostly unused notification system meant to alert federal law enforcement about crimes committed by troops that would lead to prohibition of the purchase of firearms, like the domestic violence convictions Kelley received.

The Army and Navy failed in more than 80 percent of cases to forward FBI fingerprint card files to the bureau. The Air Force failed to send them in 38 percent of cases. The probe concluded that the services were plagued by unclear directives from the Defense Department on how and when to submit the forms.

A review in 2015 found an improvement of reporting rates, with 30 percent of the Navy, Air Force and Marine Corps failing to submit fingerprint cards and final case outcome reports to the FBI. The Army was not evaluated because of limited data, the report said.

“If the problem is that we didn’t put something out, we’ll correct that,” Defense Secretary Jim Mattis said Tuesday of Kelley’s case.

Geoffrey Corn, a former Army lawyer and professor at the South Texas College of Law in Houston, said the long history of administrative dysfunction played a role in Kelley slipping through the net designed to prevent violent offenders from purchasing firearms.

“There is no unity of effort. No one knows who is supposed to do what,” Corn said.

In the era of mass shootings, there has been a growing awareness of the connection between perpetrators of such crimes and domestic violence. According to a study by the gun-control advocacy group Everytown for Gun Safety, 54 percent of recent mass shootings were related to domestic or family violence.

As a result, lawmakers on both sides of the aisle have lined up behind laws aimed at keeping firearms out of the hands of domestic abusers.

According to Everytown, 25 states and the District of Columbia have passed laws since 2013 aimed at strengthening the federal prohibition — for example, extending the prohibition to include boyfriends, and adding enforcement mechanisms so local law enforcement can confiscate abusers’ weapons. Eight states passed such laws in 2017 alone.

Yet, gun-control advocates note that the federal domestic violence law is limited in that it applies to spouses and other relatives but not dating partners. Advocates against domestic violence say at least half of all domestic abuse crimes now are committed by boyfriends.

Moreover, many convicted domestic abusers who buy guns do so not through a gun store, as Kelley did, but online or from gun shows, advocates say. In the case of such private sales, sellers are not obligated to run buyers’ names through the federal database.

Still, advocates say the database is a valuable tool that erects barriers for violent people seeking weapons. During a 16-year period that ended in December 2014, more than 1 million prohibited people — including more than 100,000 convicted of a misdemeanor crime of domestic violence and nearly 50,000 with domestic-violence-related restraining orders — were denied guns after background checks, according to a report last year by the Justice Department’s inspector general.

“We know when these records are in the system that it’s effective,” said Monica McLaughlin, director of public policy for the National Network to End Domestic Violence. “It bars batterers from obtaining weapons. It’s a really effective tool that exists in federal law that we have created, but compliance is inconsistent at best and incredibly lackluster.”