Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.
Red Hat Satellite engineering is moving the tracking of its product development work on Satellite to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "Satellite project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs will be migrated starting at the end of May. If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "Satellite project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/SAT-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.

Bug 2223567

Summary: Postgresql logs contain PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "katello_available_module_streams_name_stream_context"
Product: Red Hat Satellite Reporter: Jan Jansky <jjansky>
Component: Hosts - ContentAssignee: Jeremy Lenz <jlenz>
Status: CLOSED ERRATA QA Contact: visawant
Severity: medium Docs Contact:
Priority: high    
Version: 6.13.2CC: ahumbe, chrobert, iballou, jbhatia, jlenz, jsenkyri, kurathod, osousa, rlavi, satellite6-bugs, visawant, zhunting
Target Milestone: 6.15.0Keywords: PrioBumpGSS, Reopened, Triaged
Target Release: Unused   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
: 2266147 (view as bug list) Environment:
Last Closed: 2024-04-23 17:11:43 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description Jan Jansky 2023-07-18 09:42:23 UTC
Description of problem:
This seems to be regression of BZ 2139545.

During registration violation of unique constraint is hit as below

PG::UniqueViolation: ERROR: duplicate key value violates unique constraint "katello_available_module_streams_name_stream_context"

It seems like previous bugzilla just removed the log from /var/log/foreman/production, but postgresql still reflects same issue as below:

2023-07-18 13:31:36 CEST ERROR:  duplicate key value violates unique constraint "katello_available_module_streams_name_stream_context"
2023-07-18 13:31:36 CEST DETAIL:  Key (name, stream, context)=(python36, 3.6, 683b2e54) already exists.
2023-07-18 13:31:36 CEST STATEMENT:  INSERT INTO "katello_available_module_streams" ("name", "stream", "context") VALUES ($1, $2, $3) RETURNING "id"
2023-07-18 13:31:36 CEST ERROR:  duplicate key value violates unique constraint "katello_available_module_streams_name_stream_context"
2023-07-18 13:31:36 CEST DETAIL:  Key (name, stream, context)=(ruby, 2.5, ad008a3a) already exists.
2023-07-18 13:31:36 CEST STATEMENT:  INSERT INTO "katello_available_module_streams" ("name", "stream", "context") VALUES ($1, $2, $3) RETURNING "id"

Version-Release number of selected component (if applicable):
Satellite 6.13.2

How reproducible:
Always

Steps to Reproduce:
1. re-register RHEL8 host

Actual results:
/var/log/foreman/production.log
2023-07-18T13:31:36 [I|app|9ba09d93] Processing by Katello::Api::Rhsm::CandlepinDynflowProxyController#upload_profiles as JSON
2023-07-18T13:31:36 [I|app|9ba09d93]   Parameters: {"id"=>"1b137c1f-62d0-440b-acb7-9a55c342dd45"}
2023-07-18T13:31:36 [I|app|9ba09d93] Completed 200 OK in 167ms (Views: 6.1ms | ActiveRecord: 56.7ms | Allocations: 52327)

/var/lib/pgsql/data/log/postgresql-Tue.log
2023-07-18 13:31:36 CEST ERROR:  duplicate key value violates unique constraint "katello_available_module_streams_name_stream_context"
2023-07-18 13:31:36 CEST DETAIL:  Key (name, stream, context)=(python36, 3.6, 683b2e54) already exists.
2023-07-18 13:31:36 CEST STATEMENT:  INSERT INTO "katello_available_module_streams" ("name", "stream", "context") VALUES ($1, $2, $3) RETURNING "id"
2023-07-18 13:31:36 CEST ERROR:  duplicate key value violates unique constraint "katello_available_module_streams_name_stream_context"
2023-07-18 13:31:36 CEST DETAIL:  Key (name, stream, context)=(ruby, 2.5, ad008a3a) already exists.
2023-07-18 13:31:36 CEST STATEMENT:  INSERT INTO "katello_available_module_streams" ("name", "stream", "context") VALUES ($1, $2, $3) RETURNING "id"

Expected results:
No index violation.

Additional info:
Reported by customer but also reproduced locally, i will share reproducer details.

Comment 6 Jeremy Lenz 2023-08-25 13:57:09 UTC
I see what's happening now.

In the previous fix we switched to using Active Record `create_or_find_by` to avoid the race condition in concurrent registrations.

According to [1], the way that create_or_find_by works is

"If a row already exists with one or several of these unique constraints, the exception such an insertion would normally raise is caught, and the existing record with those attributes is found using #find_by!."

Therefore, the errors in Postgres are expected, and should not cause any harm.

[1] https://apidock.com/rails/v6.0.0/ActiveRecord/Relation/create_or_find_by

Comment 8 Jeremy Lenz 2023-09-18 13:24:04 UTC
Closing as NOTABUG; see https://bugzilla.redhat.com/show_bug.cgi?id=2223567#c6

Comment 20 Jeremy Lenz 2024-02-01 22:03:14 UTC
Created redmine issue https://projects.theforeman.org/issues/37137 from this bug

Comment 21 visawant 2024-03-13 11:40:30 UTC
Verified
Tested on 6.15.0 snap 10.1


Steps performed:
1. re-register RHEL8 host


#tail -f /var/log/foreman/production.log

2024-03-13T07:29:24 [I|app|ec960f3c] Started PUT "/rhsm/consumers/ee644f3d-945a-4370-8d5b-78bf0578a801/profiles" for 10.0.168.31 at 2024-03-13 07:29:24 -0400
2024-03-13T07:29:24 [I|app|ec960f3c] Processing by Katello::Api::Rhsm::CandlepinDynflowProxyController#upload_profiles as JSON
2024-03-13T07:29:24 [I|app|ec960f3c]   Parameters: {"id"=>"ee644f3d-945a-4370-8d5b-78bf0578a801"}
2024-03-13T07:29:24 [I|app|ec960f3c] Completed 200 OK in 146ms (Views: 1.5ms | ActiveRecord: 51.2ms | Allocations: 57452)
2024-03-13T07:29:24 [I|app|18be0722] Started GET "/rhsm/consumers/ee644f3d-945a-4370-8d5b-78bf0578a801/compliance" for 10.0.168.31 at 2024-03-13 07:29:24 -0400

# grep -ri "duplicate key value violates unique constraint \"katello_available_module_streams_name_stream_context\"" /var/lib/pgsql/data/log/
# tail -f /var/lib/pgsql/data/log/postgresql-Wed.log 
^C
#

Observation: Don't see any log inside /var/lib/pgsql/data/log/ related to index violation.

Comment 24 errata-xmlrpc 2024-04-23 17:11:43 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (Important: Satellite 6.15.0 release), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHSA-2024:2010