c77_mvc/grok_perform_matv_action-readme.md

3.3 KiB

Function: grok_perform_matv_action

Overview

This function performs maintenance actions on a materialized view based on its current health status, applying the appropriate remediation strategy.

Schema

config.grok_perform_matv_action

Parameters

  • full_matview_name (text): Full name of the materialized view (schema.name)
  • schema_name (text): Schema containing the materialized view
  • matview_name (text): Name of the materialized view
  • action (text): Action to perform: 'refresh', 'repair', or 'reindex'
  • mismatched_records (bigint): Number of records that don't match between materialized view and source
  • total_matview_records (bigint): Total number of records in the materialized view
  • time_diff (interval): Time since last refresh
  • mismatch_threshold (numeric): Threshold percentage that determines when a refresh is needed
  • time_threshold (interval): Time threshold that determines when a refresh is needed
  • encoding_issues (bigint): Number of records with encoding issues

Return Value

Returns a JSONB object indicating the action result:

{
  "action_performed": true,
  "action_result": "Refreshed successfully (concurrently)"
}

Or in case no action was taken or an error occurred:

{
  "action_performed": false,
  "action_result": "Action skipped: threshold not met or invalid action"
}

Description

This function implements a conditional maintenance system for materialized views based on their current health. It supports three types of actions:

  1. Refresh: Updates the materialized view with current data from the source view

    • Uses concurrent refresh if a unique index exists
    • Falls back to non-concurrent refresh if no unique index is found
    • Only performed if mismatch ratio exceeds the threshold or time since last refresh exceeds the time threshold
  2. Repair: Rebuilds indexes and constraints to address encoding issues

    • Drops all existing indexes (except primary keys)
    • Drops primary key and unique constraints
    • Recreates standard indexes on content_hash and synthetic_key
    • Analyzes the table to update statistics
    • Only performed if encoding issues are detected
  3. Reindex: Rebuilds all indexes without dropping them

    • Can be used for routine maintenance
    • Always performed when requested (no threshold check)

The function intelligently applies the most appropriate technique based on the materialized view's structure and current state.

Index Management

For materialized views with unique indexes, the function uses PostgreSQL's REFRESH MATERIALIZED VIEW CONCURRENTLY command, which allows queries to continue running against the materialized view during the refresh. For views without unique indexes, it falls back to the standard non-concurrent refresh.

Error Handling

If an error occurs during action execution, the function returns information about the failure without raising an exception, allowing the calling process to continue.

Dependencies

This function doesn't directly call other functions but is likely called by config.grok_manage_matv_health.

Usage Example

SELECT config.grok_perform_matv_action(
  'analytics.matc_daily_sales',
  'analytics',
  'matc_daily_sales',
  'refresh',
  155,
  12345,
  '25:30:00'::interval,
  1.0,
  '24:00:00'::interval,
  0
);