Point your dashboards at a plain view first. Each etlmcp run into PostgreSQL deletes its own pipeline's rows for the run's date range and inserts the new rows, so a plain view, which runs its query every time it is read, always reflects the latest finished run. Move to a materialized view only when that query gets too slow. A materialized view stores its result, so it stays as it was until you refresh it, and you need to refresh it after the pipeline's daily run and after every backfill. To keep dashboards answering during the refresh, use REFRESH MATERIALIZED VIEW CONCURRENTLY, which needs a unique index on plain columns that covers every row.
What a run changes in your table
etlmcp writes one table per destination, with the columns date, network, account_id, account_name, campaign_id, campaign_name, the ad group and ad IDs and names, the metrics (impressions, clicks, cost, conversions, conversion_value, ctr, cpc, cpm, cpa) and currency, plus a _etlmcp_pipeline_id column that marks which pipeline wrote each row. date is a date column, the metrics are double precision and the rest are text.
A run does not append. It works out its date range (widened to cover every date in the fetched rows), deletes the rows of its own pipeline inside that range, inserts the fresh rows and commits, all in one transaction. Two things follow for anything you build on top:
- Days you have already loaded change. A routine sync pulls a trailing window again, for example the last 29 days for Meta and 31 for Google Ads, because platforms restate recent figures. See the trailing window. A summary built once a day and never rebuilt drifts from the table.
- Readers never see a half-loaded range. Until the commit, a query sees the rows from the previous run; after it, the new ones.
The full mechanics are on how PostgreSQL rows are replaced.
A plain view
CREATE VIEW reporting.daily_campaign AS
SELECT date,
network,
account_id,
coalesce(campaign_id, '') AS campaign_id,
max(campaign_name) AS campaign_name,
currency,
sum(impressions) AS impressions,
sum(clicks) AS clicks,
sum(cost) AS cost,
sum(conversions) AS conversions,
sum(conversion_value) AS conversion_value
FROM public.ad_stats
GROUP BY date, network, account_id, coalesce(campaign_id, ''), currency;
Replace public.ad_stats with the schema and table you set on the destination. The view rolls ad-level rows up to campaigns and keeps currency in the grouping, so you never add two currencies together. It leaves out ctr, cpc, cpm and cpa: summing a ratio gives a wrong number, so work them out from the summed columns in the dashboard, as ratios from sums explains.
A view is not stored, so it needs no refresh and costs exactly as much as the query under it. Time the dashboard's queries against it before you build anything else.
A platform extra column keeps its dotted name, so write it in double quotes, such as "extra.<field>".
A materialized view
A materialized view runs the query once and keeps the result like a table, so reads are quick and you can index it. The price is that it shows the data as of its last refresh. PostgreSQL fills it when you create it, unless you add WITH NO DATA, which leaves it unreadable until the first refresh.
CREATE MATERIALIZED VIEW reporting.daily_campaign_mv AS
SELECT date,
network,
account_id,
coalesce(campaign_id, '') AS campaign_id,
max(campaign_name) AS campaign_name,
currency,
sum(impressions) AS impressions,
sum(clicks) AS clicks,
sum(cost) AS cost,
sum(conversions) AS conversions,
sum(conversion_value) AS conversion_value
FROM public.ad_stats
GROUP BY date, network, account_id, coalesce(campaign_id, ''), currency;
CREATE UNIQUE INDEX daily_campaign_mv_key
ON reporting.daily_campaign_mv (date, network, account_id, campaign_id, currency);
Refreshing without blocking readers
A plain REFRESH MATERIALIZED VIEW uses fewer resources and is usually quicker when many rows change, but it can block the connections reading the view while it works. Adding CONCURRENTLY keeps those reads going:
REFRESH MATERIALIZED VIEW CONCURRENTLY reporting.daily_campaign_mv;
PostgreSQL accepts CONCURRENTLY only when all of these hold:
- The view has at least one unique index that names plain columns only. An index on an expression, such as
coalesce(campaign_id, ''), or a partial index with aWHEREclause does not count. That is why the example appliescoalesceinside the view's query and indexes the resulting column. - The index covers every row, and its columns form a real key. Unique indexes treat each NULL as different from every other, so NULLs in a key column slip past the check. Campaign-level rows have no ad group or ad ID, and those columns are NULL in your table, so keep nullable columns out of the key or turn them into values in the query. Current PostgreSQL also accepts
NULLS NOT DISTINCTon a unique index, which treats NULLs as equal. - The view is already populated.
CONCURRENTLYcannot be combined withWITH NO DATA, and it cannot fill a view created that way: run one plain refresh first.
Even with CONCURRENTLY, only one refresh can run on a given materialized view at a time. If two jobs might overlap, for instance a scheduled refresh and one you start after a backfill, have the second wait or skip. The role that runs the refresh needs the MAINTAIN privilege on the view; a read-only dashboard login such as the one in a read-only role for dashboards cannot refresh it.
A refresh does not keep the row order of an ORDER BY in the view's query, so sort in the dashboard's query instead.
When to refresh
- After the daily run. A pipeline runs once a day after its hour in its own time zone, 6:00 UTC unless you changed it. See when a pipeline runs. The run starts after that hour and takes some time to finish, so schedule your refresh with a margin after it, or better, check that the run ended with the status
successthrough list runs or get run before you refresh. - After a backfill. A backfill replaces older dates in the table. The plain view shows them at once; the materialized view shows them only after its next refresh.
- After a run you start by hand, from the dashboard, the API or MCP, for the same reason.
If several pipelines write to the same table at different hours, refresh after the last of them, or refresh after each one and accept a few extra refreshes.