Bug #38568
closedupgrade fails with unique constraint "index_katello_installed_packages_on_nvrea"
Description
I'm still experiencing bug 38504 when upgrading from katello 4.15.1 to 4.16.2:
PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "index_katello_installed_packages_on_nvrea" DETAIL: Key (nvrea)=(openblas-serial64-0.3.15-4.el8.x86_64) already exists.
trying out some of the code with "foreman-rake console" gives me this:
irb(main):014:0> duplicated_nvreas = ::Katello::InstalledPackage.group(:nvrea).having('count(*) > 1').pluck(:nvrea)
=>
["openblas-threads64-0.3.15-4.el8.x86_64",
...
irb(main):015:0> duplicated_nvreas
=>
["openblas-threads64-0.3.15-4.el8.x86_64",
"openblas-openmp64-0.3.15-4.el8.x86_64",
"openblas-serial64_-0.3.15-4.el8.x86_64",
"openblas-serial64-0.3.15-4.el8.x86_64",
"openblas-openmp64_-0.3.15-4.el8.x86_64",
"openblas-threads64_-0.3.15-4.el8.x86_64"]
When I iterate through that list, none appear to have duplicates to delete. And so I can't complete the upgrade with foreman-installer. Let me know if you need more debugging info.
Updated by Quinn James 7 months ago
Concern stems from a user message on this PR (which should have addressed the bug): https://github.com/Katello/katello/pull/11421
Updated by Ian Ballou 7 months ago
Hi Matt,
In the foreman console, what do you see if you run ::Katello::InstalledPackage.where(nvrea: "openblas-serial64-0.3.15-4.el8.x86_64")?
If there are duplicates still, you can paste the following into the foreman console and run `fix_installed_package_dupes`:
def fix_installed_package_dupes
# Remove duplicate installed packages and host installed packages.
duplicated_nvreas = ::Katello::InstalledPackage.group(:nvrea).having('count(*) > 1').pluck(:nvrea)
duplicated_nvreas.each do |nvrea|
packages_relation = ::Katello::InstalledPackage.where(nvrea: nvrea).order(:id)
original_package = packages_relation.first
duplicate_package_ids = packages_relation.offset(1).pluck(:id)
hosts_with_original_package = ::Katello::HostInstalledPackage.where(installed_package_id: original_package.id).pluck(:host_id)
if hosts_with_original_package.any?
::Katello::HostInstalledPackage.where(host_id: hosts_with_original_package, installed_package_id: duplicate_package_ids).delete_all
end
::Katello::HostInstalledPackage.where(installed_package_id: duplicate_package_ids).update_all(installed_package_id: original_package.id)
::Katello::InstalledPackage.where(id: duplicate_package_ids).delete_all
end
end
Then re-run the InstalledPackage query to check the duplicates again.
This manual verification of the duplicate cleanup removes any complications with the migration not running with the fix for whatever reason.
Updated by Ian Ballou 7 months ago
- Status changed from New to Need more information
Updated by Matt D 7 months ago
- Status changed from Need more information to Feedback
Ian Ballou wrote in #note-2:
Hi Matt,
In the foreman console, what do you see if you run ::Katello::InstalledPackage.where(nvrea: "openblas-serial64-0.3.15-4.el8.x86_64")?
If there are duplicates still, you can paste the following into the foreman console and run `fix_installed_package_dupes`:
[...]
Then re-run the InstalledPackage query to check the duplicates again.
This manual verification of the duplicate cleanup removes any complications with the migration not running with the fix for whatever reason.
I get this:
irb(main):021:0> ::Katello::InstalledPackage.group(:nvrea).having('count(*) > 1').pluck(:nvrea)
=>
["openblas-threads64-0.3.15-4.el8.x86_64",
"openblas-openmp64-0.3.15-4.el8.x86_64",
"openblas-serial64_-0.3.15-4.el8.x86_64",
"openblas-serial64-0.3.15-4.el8.x86_64",
"openblas-openmp64_-0.3.15-4.el8.x86_64",
"openblas-threads64_-0.3.15-4.el8.x86_64"]
irb(main):022:0> ::Katello::InstalledPackage.where(nvrea: nvrea).order(:id)
=>
[#<Katello::InstalledPackage:0x00007f87d53f0c28
id: 18594,
name: "openblas-serial64",
nvra: "openblas-serial64-0.3.15-4.el8.x86_64",
nvrea: "openblas-serial64-0.3.15-4.el8.x86_64",
epoch: "0",
version: "0.3.15",
release: "4.el8",
arch: "x86_64",
evr: "(0,\"{\"\"(0,)\"\",\"\"(3,)\"\",\"\"(15,)\"\"}\",\"{\"\"(4,)\"\",\"\"(0,el)\"\",\"\"(8,)\"\"}\")",
vendor: "CentOS">]
irb(main):023:0> fix_installed_package_dupes
=>
["openblas-threads64-0.3.15-4.el8.x86_64",
"openblas-openmp64-0.3.15-4.el8.x86_64",
"openblas-serial64_-0.3.15-4.el8.x86_64",
"openblas-serial64-0.3.15-4.el8.x86_64",
"openblas-openmp64_-0.3.15-4.el8.x86_64",
"openblas-threads64_-0.3.15-4.el8.x86_64"]
Updated by Ian Ballou 7 months ago
Matt,
So,
::Katello::InstalledPackage.group(:nvrea).having('count(*) > 1').pluck(:nvrea)
returned
["openblas-threads64-0.3.15-4.el8.x86_64",
"openblas-openmp64-0.3.15-4.el8.x86_64",
"openblas-serial64_-0.3.15-4.el8.x86_64",
"openblas-serial64-0.3.15-4.el8.x86_64",
"openblas-openmp64_-0.3.15-4.el8.x86_64",
"openblas-threads64_-0.3.15-4.el8.x86_64"]
but
::Katello::InstalledPackage.where(nvrea: nvrea).order(:id)
returned only
[#<Katello::InstalledPackage:0x00007f87d53f0c28
id: 18594,
...
nvrea: "openblas-serial64-0.3.15-4.el8.x86_64",
...]
Something seems off here because that means there actually are not any duplicates with the nvrea "openblas-serial64-0.3.15-4.el8.x86_64".
Regardless, it looks like you did run `fix_installed_package_dupes`, which should have deleted all of the duplicates.
Now that the DB should be fixed, can you run
::Katello::InstalledPackage.where(nvrea: "openblas-serial64-0.3.15-4.el8.x86_64")
to verify that only a single record is returned?
If only a single record is returned, try reindexing the foreman DB to solidify the broken indexes:
sudo -u postgres reindexdb foreman
The same can be run for the pulpcore and candlepin DBs.
Updated by Matt D 7 months ago
After running reindexdb, it turns out my foreman db was riddled with constraint violations (duplicate facts, module streams, etc). I worked through those since there are posts in the community forum about that. But ultimately I had to brute force it and run commands like this to purge those problem packages.
::Katello::InstalledPackage.where(nvrea: "openblas-threads64_-0.3.15-4.el8.x86_64").first.delete
Now I can upgrade and all is well.
Updated by Quinn James 7 months ago
- Status changed from Feedback to Closed
- Triaged changed from No to Yes
Matt, thanks for getting back to us about this. Glad to hear you were able to work through the upgrade issues. We'll close out this issue and if you need further assistance, please reach out to us on the community support forum or matrix :)
We'd like to verify you successfully ran reindexdb for foreman, pulpcore, and candlepin or else your next upgrade will likely have the same issues. Best of luck!