Melatonin is a hormone that the brain releases when it gets dark.
Many people buy melatonin as a sleep aid because they think it is natural and safe.
In some countries you need a doctor’s prescription, but in others you can buy it over the counter.
Doctors from the State University of New York looked at a large world-wide database called TriNetX.
They examined the records of about 130,000 adults who had chronic insomnia.
Around 65,000 of these people took melatonin for at least one year.
People who used melatonin for one year or more had a 4.6 % chance of developing heart failure within five years.
Those who did not take the hormone had a 2.7 % chance.
This means the risk was about 90 % higher for long-term users.
Users were also 3.5 times more likely to be hospitalised for heart failure and almost twice as likely to die from any cause in five years.
The researchers say the study does not prove that melatonin directly causes heart problems.
Some people may have bought melatonin as a health supplement without a prescription, so their use was not recorded.
The team calls for more follow-up studies to check the safety of long-term melatonin use.
Doctors may start to talk more carefully with patients about any sleep aid they take.