django-postgres-extra icon indicating copy to clipboard operation
django-postgres-extra copied to clipboard

Add support for model tables on different Postgres schemas

Open brmejia opened this issue 1 year ago • 1 comments

Problem

Unable to use the command pgpartition if the model table is using a schema other than public

The model definition is something like this

class MonthTimeseriesLong(PostgresPartitionedModel):
    """
    ... fields
    """
    class PartitioningMeta:
        method = PostgresPartitioningMethod.RANGE
        key = ["timestamp"]

    class Meta:
        ...
        db_table = 'timeseries"."tpa_ts_timeseries_long_month'
        ...

Migrations are working good and the tables are created in the right schema whern migrate is executed. However, pgpartition command fails after proposed partitions are accepted:

image

No table is found because the query made inside the method get_partitioned_tables of PostgresIntrospection returns only the table name and the following strings are being compared: 'timeseries"."tpa_ts_timeseries_long_month' == 'tpa_ts_timeseries_long_month'

Solution

Split by "." the table name defined on the model and trim remaining quotes in order to perform the comparison.

Test results

tox

image

benchmarks

image

brmejia avatar Aug 03 '22 16:08 brmejia

Django doesn't have proper support for database schemas, see: https://code.djangoproject.com/ticket/6148

I am happy to merge limited support for this, but this implementation leaves a lot to be desired:

  • Breaks on table names that need escaping/quoting.
  • Breaks when the table name or schema name is properly quoted ('"schema"."table").
  • Breaks when two tables in different schemas have the same name.
  • No tests to prevent this from breaking in the future.

You'd have to use something like PostgreSQL parse_ident to do this properly. There are also other introspection methods that don't support this properly. I don't think just this patch is sufficient to fully make this work.

Photonios avatar Aug 04 '22 19:08 Photonios