Repository: pulp/pulpcore

Issue:
Use a less random UUID format
**Version**
All

**Describe the bug**
UUIDv4 is a completely 100% random 128-bit integer, which isn't efficient as far as indexes are concerned.  It would be better to use something such as UUIDv7 (draft) where the bits are arranged specifically for database index friendliness, lexicographic ordering and other nice properties.

While UUIDv7 is still in draft and hasn't been accepted yet,  UUID is just a 128-bit integer, the actual source of it doesn't matter so much.  Postgresql doesn't differentiate.  We can in all likelihood use a replacement UUID generator with little effort or risk.

This is something that may have greater benefits at larger scale but be unnoticeable at small scale. 


Discussion:

Resolving patch:
diff --git a/pulpcore/app/migrations/0105_abstract_uuid_gen.py b/pulpcore/app/migrations/0105_abstract_uuid_gen.py
new file mode 100644
--- /dev/null
+++ b/pulpcore/app/migrations/0105_abstract_uuid_gen.py
@@ -0,0 +1,214 @@
+# Generated by Django 3.2.15 on 2022-09-06 14:33
+
+from django.db import migrations, models
+import pulpcore.app.models.base
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('core', '0104_delete_label'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='accesspolicy',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='alternatecontentsource',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='alternatecontentsourcepath',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='artifact',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='basedistribution',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='content',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='contentappstatus',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='contentartifact',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='contentguard',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='createdresource',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='distribution',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='export',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='exportedresource',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='exporter',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='groupprogressreport',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='grouprole',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='import',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='importer',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='progressreport',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='publication',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='publishedartifact',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='pulpimporterrepository',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='pulptemporaryfile',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='remote',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='remoteartifact',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='repository',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='repositorycontent',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='repositoryversion',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='role',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='signingservice',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='systemid',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='task',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='taskgroup',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='taskschedule',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='upload',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='uploadchunk',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='userrole',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='worker',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='domain',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+        migrations.AlterField(
+            model_name='upstreampulp',
+            name='pulp_id',
+            field=models.UUIDField(default=pulpcore.app.models.base.pulp_uuid, editable=False, primary_key=True, serialize=False),
+        ),
+    ]
diff --git a/pulpcore/app/models/analytics.py b/pulpcore/app/models/analytics.py
--- a/pulpcore/app/models/analytics.py
+++ b/pulpcore/app/models/analytics.py
@@ -1,12 +1,12 @@
-import uuid
-
 from django.db import models
 
 from django_lifecycle import hook, LifecycleModel
 
+from pulpcore.app.models import pulp_uuid
+
 
 class SystemID(LifecycleModel):
-    pulp_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
+    pulp_id = models.UUIDField(primary_key=True, default=pulp_uuid, editable=False)
 
     @hook("before_save")
     def ensure_singleton(self):
diff --git a/pulpcore/app/models/base.py b/pulpcore/app/models/base.py
--- a/pulpcore/app/models/base.py
+++ b/pulpcore/app/models/base.py
@@ -1,5 +1,4 @@
 from gettext import gettext as _
-import uuid
 
 from django.contrib.contenttypes.fields import GenericRelation
 from django.db import models
@@ -7,15 +6,15 @@
 from django.db.models.base import ModelBase
 from django_lifecycle import LifecycleModel
 from functools import lru_cache
+from uuid6 import uuid7
 
 
 def pulp_uuid():
-    """
-    Abstract wrapper for UUID generator.
+    """Abstract wrapper for UUID generator.
 
     Allows the implementation to be swapped without triggering migrations.
     """
-    return uuid.uuid4()
+    return uuid7()
 
 
 class BaseModel(LifecycleModel):
@@ -40,7 +39,7 @@ class BaseModel(LifecycleModel):
 
     """
 
-    pulp_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
+    pulp_id = models.UUIDField(primary_key=True, default=pulp_uuid, editable=False)
     pulp_created = models.DateTimeField(auto_now_add=True)
     pulp_last_updated = models.DateTimeField(auto_now=True, null=True)
     user_roles = GenericRelation("core.UserRole")


Test patch: