SectorLabs/django-postgres-extra

Is psqlextra safe for multiple schemas?

SebCorbin opened this issue · 3 comments

We're using django-postgres-extra through django-localized-fields, when psqlextra.backend.introspection.PostgresIntrospection.get_constraints() is called, it searches across all schemas for indexes, and add that to the mapping of indexes.

Would could patch that method and provide A PR, but in the meantime, we are very worried about other potential problems of having multiple Django across schemas, hence the question in the title.

It seems to have been fixed in 2df1056 but the question remains...

I think fixing it this way would be better, because in PostgreSQL, the pg_indexes view within the SQL SELECT indexname, indexdef FROM pg_indexes WHERE tablename = %s is used to display index information for all tables. When you execute this query, it returns indexes for all tables that meet the condition, regardless of whether these tables are within the current schema or not.

    def get_constraints(self, cursor, table_name: str):
        """Retrieve any constraints or keys (unique, pk, fk, check, index)
        across one or more columns.

        Also retrieve the definition of expression-based indexes.
        """

        constraints = super().get_constraints(cursor, table_name)

        # standard Django implementation does not return the definition
        # for indexes, only for constraints, let's patch that up
        cursor.execute(
            "SELECT indexname, indexdef FROM pg_indexes WHERE schemaname = current_schema() AND tablename = %s",
            (table_name,),
        )
        for index, definition in cursor.fetchall():
            if constraints[index].get("definition") is None:
                constraints[index]["definition"] = definition

        return constraints

Multiple schemas are not really supported since Django also doesn't really support this. You're free to open PRs to fix issues related to using multiple schemas, but it is not an active goal for this project.