Skip to content

Support for computed columns and indexes for MSSQL backend JSON-based results column #9501

Open

Description

@dxdc

Checklist

  • I have checked the issues list
    for similar or identical enhancement to an existing feature.
  • I have checked the pull requests list
    for existing proposed enhancements.
  • I have checked the commit log
    to find out if the same enhancement was already implemented in the
    main branch.
  • I have included all related issues and possible duplicate issues in this issue
    (If there are none, check this box anyway).

Related Issues and Possible Duplicates

Related Issues

Possible Duplicates

  • None

Brief Summary

I’m currently using Celery with MSSQL as the result backend, and I’ve modified the results column to store JSON instead of binary (see #8634). I’d like to add computed columns to help index and query the JSON data more efficiently, but I’m unsure if this is officially supported within the Celery framework or if it could break anything.

e.g.:

from sqlalchemy import JSON
models.Task.__table__.c.result.type = JSON()

Could you advise on whether:

  • Adding computed columns is a supported or recommended approach?
  • This change might require a feature request or additional Celery configuration?

Design

Architectural Considerations

None

Proposed Behavior

Proposed UI/UX

Diagrams

N/A

Alternatives

None

Activity

  1. auvipy commented on Feb 24, 2025

    @auvipy
    Member

    contributions are welcome

  2. dxdc commented on Feb 24, 2025

    @dxdc
    Author

    thx @auvipy ... I was thinking probably the database models need to be converted into something more extensible, while still keeping the needed fields in place.

    do you have any suggestions how to approach this?

    I'm also not sure if the schema is checked each time and aligned, in case of upgrades or changes for example.

    https://github.com/celery/celery/blob/main/celery/backends/database/models.py

  3. dxdc commented on Feb 24, 2025

    @dxdc
    Author

    for example, wondering about something like this, but not sure if this is a proper way to override the backend. Maybe there is a better way to do it.

    # celery_custom.py
    
    from celery import Celery
    from celery.backends.database import DatabaseBackend
    from celery.backends.database.models import Task
    from sqlalchemy import Column, String, Integer
    
    # 1. Define a mixin that supplies additional fields.
    class AdditionalFieldsMixin:
        # Add any extra fields you require.
        extra_field = Column(String(255), nullable=True)
        another_field = Column(Integer, default=0, nullable=False)
    
    # 2. Create a custom Task model that includes the additional fields.
    #    We use multiple inheritance to combine our mixin with Celery's Task model.
    class CustomTask(AdditionalFieldsMixin, Task):
        # We keep the same table name to act as a drop-in replacement.
        __tablename__ = Task.__tablename__
    
    # 3. Create a custom database backend that uses the custom Task model.
    class CustomDatabaseBackend(DatabaseBackend):
        # Override the TaskModel to point to our custom model.
        TaskModel = CustomTask
    
    # 4. Setup a Celery application and configure it to use a database backend.
    #    Note: For this example we are using an SQLite database.
    app = Celery('my_app', broker='pyamqp://guest@localhost//', backend='db+sqlite:///results.sqlite')
    
    # Override the default backend with our custom backend.
    app.backend = CustomDatabaseBackend(app=app)
    
    @app.task
    def add(x, y):
        """Simple task that adds two numbers."""
        return x + y
    
    if __name__ == '__main__':
        # Submit a task and print its result.
        result = add.delay(4, 6)
        print("Task result:", result.get(timeout=10))
  4. auvipy commented on Feb 27, 2025

    @auvipy
    Member

    Mmm... May be a 3 rd party extension is well suited for MsSQL specific features for now

  5. dxdc commented on Feb 27, 2025

    @dxdc
    Author

    Is there a recommended place in the code to be able to plugin and develop an interface like that? I could see general utility for it.

    I don't know how well it will persist with version upgrades otherwise.

  6. auvipy commented on Mar 9, 2025

    @auvipy
    Member

    In that case, i suggest to open a draft PR for review and further discussion in celery 🙂 it will be the easiest I guess

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions