Internal Server Error on using some tags (ERROR: duplicate key value violates unique constraint "index_tags_on_name")

This seems to be an odd problem as it is only effecting some tags - when trying to create a Topic with the tag the user is shown an Internal Server Message. Sometimes, when it gets past that stage, we are shown the same message via the review queue system. When going to the pages of these affected tags, it says Oops! That page doesn’t exist or is private.

Here are some logs:

ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_tags_on_name"
DETAIL:  Key (name)=(ecommerce) already exists.
)
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.0/lib/patches/db/pg.rb:72:in `async_exec_params'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.0/lib/patches/db/pg.rb:72:in `async_exec_params'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.0/lib/patches/db/pg.rb:72:in `exec_params'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:668:in `block (2 levels) in exec_no_cache'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:667:in `block in exec_no_cache'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:707:in `block (2 levels) in log'
/usr/local/lib/ruby/2.6.0/monitor.rb:235:in `mon_synchronize'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:706:in `block in log'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:697:in `log'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:666:in `exec_no_cache'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:647:in `execute_and_clear'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:96:in `exec_query'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:135:in `exec_insert'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:130:in `exec_insert'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:162:in `insert'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/query_cache.rb:22:in `insert'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/persistence.rb:375:in `_insert_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/persistence.rb:933:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/counter_cache.rb:163:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/locking/optimistic.rb:70:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/attribute_methods/dirty.rb:211:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/callbacks.rb:332:in `block in _create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:135:in `run_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:827:in `_run_create_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/callbacks.rb:332:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/timestamp.rb:111:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/persistence.rb:906:in `create_or_update'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/callbacks.rb:328:in `block in create_or_update'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:135:in `run_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:827:in `_run_save_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/callbacks.rb:328:in `create_or_update'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/timestamp.rb:129:in `create_or_update'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/persistence.rb:470:in `save'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/validations.rb:47:in `save'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:315:in `block in save'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:375:in `block in with_transaction_returning_status'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:275:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:212:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:366:in `with_transaction_returning_status'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:315:in `save'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/suppressor.rb:44:in `save'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/persistence.rb:38:in `create'
/var/www/discourse/lib/discourse_tagging.rb:62:in `block in tag_topic_by_names'
/var/www/discourse/lib/discourse_tagging.rb:60:in `each'
/var/www/discourse/lib/discourse_tagging.rb:60:in `tag_topic_by_names'
/var/www/discourse/lib/topic_creator.rb:158:in `setup_tags'
/var/www/discourse/lib/topic_creator.rb:37:in `create'
/var/www/discourse/lib/post_creator.rb:421:in `create_topic'
/var/www/discourse/lib/post_creator.rb:168:in `block in create'
/var/www/discourse/lib/post_creator.rb:345:in `block in transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:275:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:212:in `transaction'
/var/www/discourse/lib/post_creator.rb:344:in `transaction'
/var/www/discourse/lib/post_creator.rb:166:in `create'
/var/www/discourse/app/models/reviewable_queued_post.rb:80:in `perform_approve_post'
/var/www/discourse/app/models/reviewable.rb:319:in `public_send'
/var/www/discourse/app/models/reviewable.rb:319:in `block in perform'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:277:in `block in transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
/usr/local/lib/ruby/2.6.0/monitor.rb:235:in `mon_synchronize'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:277:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:212:in `transaction'
/var/www/discourse/app/models/reviewable.rb:317:in `perform'
/var/www/discourse/app/controllers/reviewables_controller.rb:184:in `perform'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/base.rb:196:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/rendering.rb:30:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/callbacks.rb:42:in `block in process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:135:in `run_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/callbacks.rb:41:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/rescue.rb:22:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/notifications.rb:180:in `block in instrument'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/notifications.rb:180:in `instrument'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/railties/controller_runtime.rb:27:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/base.rb:136:in `process'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionview-6.0.0/lib/action_view/rendering.rb:39:in `process'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.0/lib/mini_profiler/profiling_methods.rb:104:in `block in profile_method'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal.rb:191:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal.rb:252:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/route_set.rb:51:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/route_set.rb:33:in `serve'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/router.rb:49:in `block in serve'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/router.rb:32:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/router.rb:32:in `serve'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/route_set.rb:837:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-protection-2.0.7/lib/rack/protection/frame_options.rb:31:in `call'
/var/www/discourse/lib/middleware/omniauth_bypass_middleware.rb:68:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/tempfile_reaper.rb:15:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/conditional_get.rb:38:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/head.rb:12:in `call'
/var/www/discourse/lib/content_security_policy/middleware.rb:12:in `call'
/var/www/discourse/lib/middleware/anonymous_cache.rb:274:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:232:in `context'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:226:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/cookies.rb:648:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:101:in `run_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/logster-2.3.3/lib/logster/middleware/reporter.rb:43:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/rack/logger.rb:38:in `call_app'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/rack/logger.rb:28:in `call'
/var/www/discourse/config/initializers/100-quiet_logger.rb:18:in `call'
/var/www/discourse/config/initializers/100-silence_logger.rb:31:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/request_id.rb:27:in `call'
/var/www/discourse/lib/middleware/enforce_hostname.rb:17:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/executor.rb:14:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/host_authorization.rb:77:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.0/lib/mini_profiler/profiler.rb:296:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/message_bus-2.2.2/lib/message_bus/rack/middleware.rb:57:in `call'
/var/www/discourse/lib/middleware/request_tracker.rb:176:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/engine.rb:526:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/railtie.rb:190:in `public_send'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/railtie.rb:190:in `method_missing'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/urlmap.rb:68:in `block in call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/urlmap.rb:53:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/urlmap.rb:53:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:605:in `process_client'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:700:in `worker_loop'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:548:in `spawn_missing_workers'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:144:in `start'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/bin/unicorn:128:in `<top (required)>'
/var/www/discourse/vendor/bundle/ruby/2.6.0/bin/unicorn:23:in `load'
/var/www/discourse/vendor/bundle/ruby/2.6.0/bin/unicorn:23:in `<main>'

Failed to handle exception in exception app middleware : PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_tags_on_name"
DETAIL:  Key (name)=(ecommerce) already exists.
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.0/lib/patches/db/pg.rb:72:in `async_exec_params'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.0/lib/patches/db/pg.rb:72:in `exec_params'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:668:in `block (2 levels) in exec_no_cache'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:667:in `block in exec_no_cache'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:707:in `block (2 levels) in log'
/usr/local/lib/ruby/2.6.0/monitor.rb:235:in `mon_synchronize'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:706:in `block in log'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:697:in `log'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:666:in `exec_no_cache'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:647:in `execute_and_clear'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:96:in `exec_query'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:135:in `exec_insert'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:130:in `exec_insert'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:162:in `insert'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/query_cache.rb:22:in `insert'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/persistence.rb:375:in `_insert_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/persistence.rb:933:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/counter_cache.rb:163:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/locking/optimistic.rb:70:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/attribute_methods/dirty.rb:211:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/callbacks.rb:332:in `block in _create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:135:in `run_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:827:in `_run_create_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/callbacks.rb:332:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/timestamp.rb:111:in `_create_record'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/persistence.rb:906:in `create_or_update'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/callbacks.rb:328:in `block in create_or_update'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:135:in `run_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:827:in `_run_save_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/callbacks.rb:328:in `create_or_update'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/timestamp.rb:129:in `create_or_update'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/persistence.rb:470:in `save'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/validations.rb:47:in `save'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:315:in `block in save'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:375:in `block in with_transaction_returning_status'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:275:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:212:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:366:in `with_transaction_returning_status'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:315:in `save'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/suppressor.rb:44:in `save'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/persistence.rb:38:in `create'
/var/www/discourse/lib/discourse_tagging.rb:62:in `block in tag_topic_by_names'
/var/www/discourse/lib/discourse_tagging.rb:60:in `each'
/var/www/discourse/lib/discourse_tagging.rb:60:in `tag_topic_by_names'
/var/www/discourse/lib/topic_creator.rb:158:in `setup_tags'
/var/www/discourse/lib/topic_creator.rb:37:in `create'
/var/www/discourse/lib/post_creator.rb:421:in `create_topic'
/var/www/discourse/lib/post_creator.rb:168:in `block in create'
/var/www/discourse/lib/post_creator.rb:345:in `block in transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:275:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:212:in `transaction'
/var/www/discourse/lib/post_creator.rb:344:in `transaction'
/var/www/discourse/lib/post_creator.rb:166:in `create'
/var/www/discourse/app/models/reviewable_queued_post.rb:80:in `perform_approve_post'
/var/www/discourse/app/models/reviewable.rb:319:in `public_send'
/var/www/discourse/app/models/reviewable.rb:319:in `block in perform'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:277:in `block in transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:280:in `block in within_new_transaction'
/usr/local/lib/ruby/2.6.0/monitor.rb:235:in `mon_synchronize'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:278:in `within_new_transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:277:in `transaction'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/transactions.rb:212:in `transaction'
/var/www/discourse/app/models/reviewable.rb:317:in `perform'
/var/www/discourse/app/controllers/reviewables_controller.rb:184:in `perform'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/base.rb:196:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/rendering.rb:30:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/callbacks.rb:42:in `block in process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:135:in `run_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/callbacks.rb:41:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/rescue.rb:22:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/notifications.rb:180:in `block in instrument'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/notifications/instrumenter.rb:24:in `instrument'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/notifications.rb:180:in `instrument'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.0/lib/active_record/railties/controller_runtime.rb:27:in `process_action'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/abstract_controller/base.rb:136:in `process'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionview-6.0.0/lib/action_view/rendering.rb:39:in `process'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.0/lib/mini_profiler/profiling_methods.rb:104:in `block in profile_method'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal.rb:191:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_controller/metal.rb:252:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/route_set.rb:51:in `dispatch'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/route_set.rb:33:in `serve'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/router.rb:49:in `block in serve'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/router.rb:32:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/journey/router.rb:32:in `serve'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/routing/route_set.rb:837:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-protection-2.0.7/lib/rack/protection/frame_options.rb:31:in `call'
/var/www/discourse/lib/middleware/omniauth_bypass_middleware.rb:68:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/tempfile_reaper.rb:15:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/conditional_get.rb:38:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/head.rb:12:in `call'
/var/www/discourse/lib/content_security_policy/middleware.rb:12:in `call'
/var/www/discourse/lib/middleware/anonymous_cache.rb:274:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:232:in `context'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/session/abstract/id.rb:226:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/cookies.rb:648:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/callbacks.rb:101:in `run_callbacks'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/logster-2.3.3/lib/logster/middleware/reporter.rb:43:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/rack/logger.rb:38:in `call_app'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/rack/logger.rb:28:in `call'
/var/www/discourse/config/initializers/100-quiet_logger.rb:18:in `call'
/var/www/discourse/config/initializers/100-silence_logger.rb:31:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/request_id.rb:27:in `call'
/var/www/discourse/lib/middleware/enforce_hostname.rb:17:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/method_override.rb:22:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/executor.rb:14:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/sendfile.rb:111:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/actionpack-6.0.0/lib/action_dispatch/middleware/host_authorization.rb:77:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.0/lib/mini_profiler/profiler.rb:296:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/message_bus-2.2.2/lib/message_bus/rack/middleware.rb:57:in `call'
/var/www/discourse/lib/middleware/request_tracker.rb:176:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/engine.rb:526:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/railtie.rb:190:in `public_send'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/railties-6.0.0/lib/rails/railtie.rb:190:in `method_missing'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/urlmap.rb:68:in `block in call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/urlmap.rb:53:in `each'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-2.0.7/lib/rack/urlmap.rb:53:in `call'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:605:in `process_client'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:700:in `worker_loop'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:548:in `spawn_missing_workers'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/lib/unicorn/http_server.rb:144:in `start'
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/unicorn-5.5.1/bin/unicorn:128:in `<top (required)>'
/var/www/discourse/vendor/bundle/ruby/2.6.0/bin/unicorn:23:in `load'
/var/www/discourse/vendor/bundle/ruby/2.6.0/bin/unicorn:23:in `<main>'

Any idea why it’s happening? It seems to be trying to create a tag that already exists?

Update:

On one of the tags where we were getting the same problem, we ended up simply asking the user to repost without the tag, then upon editing it I was able to add the tag (and also visit the tag’s page without issue). Previously the user was actually able to post the topic but it had gone into the review queue (as we review all first posts/topics) and we were getting the internal server error when trying to authorise the topic.

However, I’m not able to add the other tag that it happened to (ecommerce) as it is still reporting an internal server error.

Getting the same thing on another tag:

ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_tags_on_name"
DETAIL:  Key (name)=(file-uploading) already exists.
)
/var/www/discourse/vendor/bundle/ruby/2.6.0/gems/rack-mini-profiler-1.1.2/lib/patches/db/pg.rb:72:in `async_exec_params'

Is this a Discourse or AR/Rails bug? Seems it is trying to create the tag again even though it exists?

Can you look in data explorer in the tags table, maybe there is some sort of casing issue of Ecommerce vs ecommerce ?

1 Like

Hi Sam, I’ve looked in the table and there are:

ecommerce
e-commerce

With the second one (file-uploading) there’s:

file-downloading
file-uploading

They both have hyphens so I wonder if that’s causing it?

It settings we have force lowercase tags checked - could that be conflicting somehow too?

If you need me to check anything else please let me know :blush:

Not 100% sure, hold a day or so, @neil is very familiar with the tags system he may have some ideas here.

1 Like

Will do, thanks Sam :smiley:

I’ve been playing around with mixed case tag names and tags with hyphens in them, but can’t reproduce this error. What exact version of discourse are you using? Is this happening in a category with tag restrictions? What other tag settings do you use?

2 Likes

Hi Neil, I’m using 2.4.0.beta6 (8ca5aad1e2)

I’ve managed to fix the issue by manually renaming them in rails console - interestingly, the e.commerce tag was returning nil even though it existed! I located it by ID and then renamed-named it and it is now working as well.

I have a feeling this started happening when case sensitivity was introduced (we’ve had the problem a while but I usually wait for a few new releases as you generally fix things pretty quickly).

If you need me to investigate anything else let me know, otherwise I will go through all the tags (or error logs) and identify other broken tags and fix them accordingly. In case it’s any help, here’s the answers to your questions:

No - but all categories require at least one tag

1 Like

I don’t get what the problem was. You renamed “e.commerce” from what to what? I tested enabling/disabling the force lowercase tags settings, using the tags in mixed case, and it didn’t attempt to create duplicate tags.

3 Likes

I renamed tag-name to tag-name2 then back to tag-name

I used find by name to locate them:

Tag.find_by name: "tag-name"

However that did not work for e-commerce I had to find it by ID, which seems odd - not sure why that would happen.

The only other thing I can think of, is at some point I have ‘mass’ tagged threads by carrying out a search and then clicking on a number of Topics and then ‘append tag’ from the options. Not sure if that would make a difference tho : /

Sorry @neil I’ve encountered another issue which may be related.

Two tags with the same name, however my manual fix is not working this time.

[1] pry(main)> Tag.find_by(name: "liveview")
=> #<Tag:0x000055646b510750
 id: 1843,
 name: "liveview",
 topic_count: 25,
 created_at: Tue, 01 Oct 2019 07:42:44 UTC +00:00,
 updated_at: Tue, 01 Oct 2019 07:42:44 UTC +00:00,
 pm_topic_count: 0>
[2] pry(main)> t=Tag.find_by(id: 1036)
=> #<Tag:0x000055646b653978
 id: 1036,
 name: "liveview",
 topic_count: 150,
 created_at: Sun, 09 Sep 2018 16:19:15 UTC +00:00,
 updated_at: Sun, 09 Sep 2018 16:19:15 UTC +00:00,
 pm_topic_count: 0>
[3] pry(main)> t2=Tag.find_by(id: 1843)
=> #<Tag:0x000055646b6af7a0
 id: 1843,
 name: "liveview",
 topic_count: 25,
 created_at: Tue, 01 Oct 2019 07:42:44 UTC +00:00,
 updated_at: Tue, 01 Oct 2019 07:42:44 UTC +00:00,
 pm_topic_count: 0>
[4] pry(main)> t2.name = "liveview2"
=> "liveview2"
[5] pry(main)> t2.save
=> true
[6] pry(main)> t.name = "liveview1"
=> "liveview1"
[7] pry(main)> t.save
=> true
[8] pry(main)> t.name = "liveview"
=> "liveview"
[9] pry(main)> t.save
=> true
[10] pry(main)> t.name = "liveview1"
=> "liveview1"
[11] pry(main)> 

liveview2 works, but the lone liveview doesn’t, however it works when named liveview1 but when it’s name is changed back to liveview it just goes to Oops! That page doesn’t exist or is private.. Index related?

(nothing in /logs either)

There are two indexes to protect against this case. Is your database missing the indexes?

$ bin/rails db

discourse_development=# \d tags
                                          Table "public.tags"
     Column     |            Type             | Collation | Nullable |             Default              
----------------+-----------------------------+-----------+----------+----------------------------------
 id             | integer                     |           | not null | nextval('tags_id_seq'::regclass)
 name           | character varying           |           | not null | 
 topic_count    | integer                     |           | not null | 0
 created_at     | timestamp without time zone |           | not null | 
 updated_at     | timestamp without time zone |           | not null | 
 pm_topic_count | integer                     |           | not null | 0
Indexes:
    "tags_pkey" PRIMARY KEY, btree (id)
    "index_tags_on_lower_name" UNIQUE, btree (lower(name::text))
    "index_tags_on_name" UNIQUE, btree (name)

A migration dated 2018 Sept 28 should have fixed tags with duplicate names and then created one of the indexes. Did you import your site from other forum software? If so, it might have created duplicate rows and then failed to create the indexes.

1 Like
     Column     |            Type             | Collation | Nullable |             Default              
----------------+-----------------------------+-----------+----------+----------------------------------
 id             | integer                     |           | not null | nextval('tags_id_seq'::regclass)
 name           | character varying           |           | not null | 
 topic_count    | integer                     |           | not null | 0
 created_at     | timestamp without time zone |           |          | 
 updated_at     | timestamp without time zone |           |          | 
 pm_topic_count | integer                     |           | not null | 0
Indexes:
    "tags_pkey" PRIMARY KEY, btree (id)
    "index_tags_on_lower_name" UNIQUE, btree (lower(name::text))
    "index_tags_on_name" UNIQUE, btree (name)

No this has always been a Discourse forum - the only thing I’ve done is move to another server and so that meant a fresh install then imported via backup. Think this was done around August last year - could that have caused it?

Should I do something like REINDEX TABLE tags; to rebuild the indexes Neil?

Edit: just noticed created_at and updated_at does not have not null under nullable - will it matter?

Edit 2: Have just noticed there are two of these tags too:

1862	live-view
1486	live-view

Yes, I think reindexing the table is needed. But first you’ll need to rename the tags that have duplicate names, otherwise the reindex will fail. And hope that only the tags table has this problem…

I don’t see how a restore would create this problem. The indexes are there, but clearly aren’t working.

2 Likes

Ok done :smiley:

If anyone else needs the query to find duplicate tags it’s:

SELECT name, COUNT(*)
FROM tags
GROUP BY name
HAVING COUNT(*) > 1

Don’t scare me now Neil! :scream:

I did have a quick look at the categories table and that seems fine (there are a few duplicated entires based on name, but they are sub-categories).

Thanks for your help @sam and @neil :blush:

(The reindex didn’t have any effect on this issue tho Neil)

We have this index:

https://github.com/discourse/discourse/blob/deec2cf578ce1c898bcde88117610d8bc945f174/app/models/tag.rb#L166-L166

Are you 100% sure that exists on the table cause what you are describing is a completely broken Postgres which is infinitely unlikely, this would send complete shoke waves through the community.

Much more likely somehow at some point that index got dropped due to a human …

At a minimum I recommend you create unique index on tags (name) but what else can you have missing?

3 Likes

Hi Sam,

I have:

discourse=> \d tags
                                          Table "public.tags"
     Column     |            Type             | Collation | Nullable |             Default              
----------------+-----------------------------+-----------+----------+----------------------------------
 id             | integer                     |           | not null | nextval('tags_id_seq'::regclass)
 name           | character varying           |           | not null | 
 topic_count    | integer                     |           | not null | 0
 created_at     | timestamp without time zone |           |          | 
 updated_at     | timestamp without time zone |           |          | 
 pm_topic_count | integer                     |           | not null | 0
Indexes:
    "tags_pkey" PRIMARY KEY, btree (id)
    "index_tags_on_lower_name" UNIQUE, btree (lower(name::text))
    "index_tags_on_name" UNIQUE, btree (name)

And to recreate the indexes I…

  • renamed duplicate tags (so tag_name_1 and tag_name_2 etc)
  • I checked there were no more duplicate tags with the query in the above post
  • I went to rails db and then ran:
discourse=> REINDEX TABLE tags;
REINDEX

Do I need to do anything else other than that Sam?

Maybe try inserting a dupe tag and confirm your index is happy and not allowing dupe data any more?

1 Like

Looks good Sam:

[1] pry(main)> t=Tag.new
=> #<Tag:0x000055fe76499830 id: nil, name: nil, topic_count: 0, created_at: nil, updated_at: nil, pm_topic_count: 0>
[2] pry(main)> t
=> #<Tag:0x000055fe76499830 id: nil, name: nil, topic_count: 0, created_at: nil, updated_at: nil, pm_topic_count: 0>
[3] pry(main)> t.name ="test-five"
=> "test-five"
[4] pry(main)> t.save
=> true

[5] pry(main)> Tag.last
=> #<Tag:0x000055fe7671ce90
 id: 1995,
 name: "test-five",
 topic_count: 0,
 created_at: Fri, 08 Nov 2019 02:44:36 UTC +00:00,
 updated_at: Fri, 08 Nov 2019 02:44:36 UTC +00:00,
 pm_topic_count: 0

[6] pry(main)> t=Tag.new
=> #<Tag:0x000055fe76763a70 id: nil, name: nil, topic_count: 0, created_at: nil, updated_at: nil, pm_topic_count: 0>
[7] pry(main)> t.name ="test-five"
=> "test-five"
[8] pry(main)> t.save
=> false
[9] pry(main)> 

Also tried via the forum in a Topic:

If you think I should try anything else please let me know :smiley:

1 Like

Also just to confirm, I don’t think I have done anything else with the DB via the console so I’m not sure why this happened. I -think- it might have started after case sensitivity was introduced but can’t be sure. If you need me to check anything else to help ascertain the root of the issue just let me know Sam.