Skip to content

PostgreSQL unnecessary drop and create view #5855

@patschwork

Description

@patschwork

Engine: PostgreSQL

Changing simple conditions (e.g. a WHERE statement) leads to drop and create a model view. For example permissions are gone (in this case) or the object_id is changes. (for Microsoft SQL Server this would also happen, but I didn't tried it out, yet). (I'm aware of on_virtual_update, where it is possible to handle permissions. it is just an example).

Example:

Initial model

MODEL (
  name analysis.airports_per_timezone_gt_10,
  grain timezone,
);

SELECT 
    * 
FROM analysis.airports_per_timezone apt
WHERE cnt_per_timezone>0
;

This generates:

  • sqlmesh__analysis.analysis__airports_per_timezone_gt_10__1111111111
  • analysis.analysis__airports_per_timezone_gt_10 which uses sqlmesh__analysis.analysis__airports_per_timezone_gt_10__1111111111

model change

MODEL (
  name analysis.airports_per_timezone_gt_10,
  grain timezone,
);

SELECT 
    * 
FROM analysis.airports_per_timezone apt
WHERE cnt_per_timezone>10  -- changed
;

This generates:

  • sqlmesh__analysis.analysis__airports_per_timezone_gt_10__222222222
  • DROP and CREATE analysis.analysis__airports_per_timezone_gt_10 which uses sqlmesh__analysis.analysis__airports_per_timezone_gt_10__222222222

If no columns are changed (altered positions, renamings, deletions), then PostgreSQL can replace the view. Just replacing the source view only needs CREATE AND REPLACE and not DROP + CREATE.

Can this be avoided somehow? (without creating a "Custom materialization")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions