Snowflake-Labs / django-snowflake

MIT License
59 stars 15 forks source link

Use last_query_id when retrieving last_insert_id #56

Closed stanorama closed 1 year ago

stanorama commented 1 year ago

I note in this project: https://github.com/benryan2010/django-snowflake-backend/blob/master/django-snowflake-backend/operations.py

They use the last_query_id to ensure that the max query relates only to the last query in the current session. I guess this only works if the database session is specific to the current web session and is not shared.

        try:
            with self.connection.cursor() as cursor:
                cursor.execute('SET qid = last_query_id()')
        # TODO: map exception here
        except Database.errors.ProgrammingError as e:
            # default error message
            print(e)
            # customer error message
            print('Error {0} ({1}): {2} ({3})'.format(e.errno, e.sqlstate, e.msg, e.sfqid))
            return False

        set_statement_id_sql = 'select max("{0}") from "{1}" AT(statement=>$qid)'.format(pk_name, table_name)

Would this help alleviate the race condition?

timgraham commented 1 year ago

First, I'm not a Snowflake expert. I did see that approach, but as far as I could tell, it could also be problematic. My sense is that Snowflake should be used as a data warehouse, not in a web application context where concurrency is a factor. That being the case, adding more overhead each time a new object is created (perhaps even giving users the idea that this use case is appropriate) didn't seem worthwhile.

@sfc-gh-mkeller, any opinions?

timgraham commented 1 year ago

Absent an argument that this is worth the cost, closing as wontfix.