-
Notifications
You must be signed in to change notification settings - Fork 6
/
pg_fact_loader--1.5--1.6.sql
371 lines (348 loc) · 16.8 KB
/
pg_fact_loader--1.5--1.6.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* pg_fact_loader--1.5--1.6.sql */
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION pg_fact_loader" to load this file. \quit
DROP VIEW fact_loader.queue_deps_all_with_retrieval;
DROP VIEW fact_loader.queue_deps_all;
DROP VIEW fact_loader.prioritized_jobs;
-- Must ensure we have the fully schema-qualified regprod before converting to text
SET search_path TO '';
ALTER TABLE fact_loader.debug_process_queue ALTER COLUMN proid TYPE TEXT;
ALTER TABLE fact_loader.debug_process_queue ADD CONSTRAINT check_proid CHECK (COALESCE(proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.fact_table_dep_queue_table_deps ALTER COLUMN delete_merge_proid TYPE TEXT;
ALTER TABLE fact_loader.fact_table_dep_queue_table_deps ADD CONSTRAINT check_delete_merge_proid CHECK (COALESCE(delete_merge_proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.fact_table_dep_queue_table_deps ALTER COLUMN insert_merge_proid TYPE TEXT;
ALTER TABLE fact_loader.fact_table_dep_queue_table_deps ADD CONSTRAINT check_insert_merge_proid CHECK (COALESCE(insert_merge_proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.fact_table_dep_queue_table_deps ALTER COLUMN update_merge_proid TYPE TEXT;
ALTER TABLE fact_loader.fact_table_dep_queue_table_deps ADD CONSTRAINT check_update_merge_proid CHECK (COALESCE(update_merge_proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.fact_table_deps ALTER COLUMN default_delete_merge_proid TYPE TEXT;
ALTER TABLE fact_loader.fact_table_deps ADD CONSTRAINT check_default_delete_merge_proid CHECK (COALESCE(default_delete_merge_proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.fact_table_deps ALTER COLUMN default_insert_merge_proid TYPE TEXT;
ALTER TABLE fact_loader.fact_table_deps ADD CONSTRAINT check_default_insert_merge_proid CHECK (COALESCE(default_insert_merge_proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.fact_table_deps ALTER COLUMN default_update_merge_proid TYPE TEXT;
ALTER TABLE fact_loader.fact_table_deps ADD CONSTRAINT check_default_update_merge_proid CHECK (COALESCE(default_update_merge_proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.fact_tables ALTER COLUMN daily_scheduled_proid TYPE TEXT;
ALTER TABLE fact_loader.fact_tables ADD CONSTRAINT check_daily_scheduled_proid CHECK (COALESCE(daily_scheduled_proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.fact_tables ALTER COLUMN fact_table_agg_proid TYPE TEXT;
ALTER TABLE fact_loader.fact_tables ADD CONSTRAINT check_fact_table_agg_proid CHECK (COALESCE(fact_table_agg_proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.queue_table_deps ALTER COLUMN delete_merge_proid TYPE TEXT;
ALTER TABLE fact_loader.queue_table_deps ADD CONSTRAINT check_delete_merge_proid CHECK (COALESCE(delete_merge_proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.queue_table_deps ALTER COLUMN insert_merge_proid TYPE TEXT;
ALTER TABLE fact_loader.queue_table_deps ADD CONSTRAINT check_insert_merge_proid CHECK (COALESCE(insert_merge_proid::REGPROC, 'boolin') IS NOT NULL);
ALTER TABLE fact_loader.queue_table_deps ALTER COLUMN update_merge_proid TYPE TEXT;
ALTER TABLE fact_loader.queue_table_deps ADD CONSTRAINT check_update_merge_proid CHECK (COALESCE(update_merge_proid::REGPROC, 'boolin') IS NOT NULL);
RESET search_path;
CREATE OR REPLACE VIEW fact_loader.prioritized_jobs AS
WITH jobs_with_daily_variables AS (
SELECT
ft.*,
/***
Keep all this logic of daily jobs as variables to ease visualization of logic in the next cte below!!
*/
(--If this is the first run of a scheduled job, it is eligible
ft.last_refresh_attempted_at IS NULL
OR (
--If it was last attempted successfully prior to this scheduled time only - meaning yesterday, it is eligible
(
ft.last_refresh_succeeded AND
ft.last_refresh_attempted_at::DATE <
-- Timezone taken from daily_scheduled_tz if base job, otherwise look up the timezone of the base job if this is dependent
(now() AT TIME ZONE COALESCE(
ft.daily_scheduled_tz,
base.daily_scheduled_tz
)
)::DATE
)
OR
--If a job has failed and been re-enabled, it is eligible again even though it has been attempted at or after the scheduled time
NOT ft.last_refresh_succeeded
)
) AS daily_not_attempted_today,
(now() AT TIME ZONE ft.daily_scheduled_tz)::TIME
BETWEEN daily_scheduled_time AND '23:59:59.999999'::TIME AS daily_scheduled_time_passed,
base.use_daily_schedule
AND base.last_refresh_succeeded
AND base.last_refresh_attempted_at :: DATE = (now() AT TIME ZONE base.daily_scheduled_tz) :: DATE
AS daily_base_job_finished,
ft.depends_on_base_daily_job_id = ft.depends_on_parent_daily_job_id AS daily_has_only_one_parent,
-- This should only be used in combination with daily_has_only_one_parent
parent.use_daily_schedule
AND parent.last_refresh_succeeded
AND parent.last_refresh_attempted_at :: DATE = (now() AT TIME ZONE COALESCE(parent.daily_scheduled_tz, base.daily_scheduled_tz)) :: DATE
AS parent_job_finished
FROM fact_loader.fact_tables ft
LEFT JOIN LATERAL
(SELECT ftb.use_daily_schedule,
ftb.last_refresh_succeeded,
ftb.last_refresh_attempted_at,
ftb.daily_scheduled_tz
FROM fact_loader.fact_tables ftb
WHERE ftb.fact_table_id = ft.depends_on_base_daily_job_id) base ON TRUE
LEFT JOIN LATERAL
(SELECT ftp.use_daily_schedule,
ftp.last_refresh_succeeded,
ftp.last_refresh_attempted_at,
ftp.daily_scheduled_tz
FROM fact_loader.fact_tables ftp
WHERE ftp.fact_table_id = ft.depends_on_parent_daily_job_id) parent ON TRUE
WHERE enabled
)
, jobs_with_daily_schedule_eligibility AS (
SELECT
*,
--Only run this job according to the same day of the daily_scheduled_time
--according to configured timezone
(use_daily_schedule AND daily_not_attempted_today
AND
(
daily_scheduled_time_passed
OR
(daily_base_job_finished AND (daily_has_only_one_parent OR parent_job_finished))
)
) AS daily_schedule_eligible
FROM jobs_with_daily_variables)
SELECT *
FROM jobs_with_daily_schedule_eligibility
WHERE NOT use_daily_schedule OR daily_schedule_eligible
ORDER BY
CASE WHEN force_worker_priority THEN 0 ELSE 1 END,
--If a job has a daily schedule, once the time has come for the next refresh,
--prioritize it first
CASE
WHEN daily_schedule_eligible
THEN (now() AT TIME ZONE daily_scheduled_tz)::TIME
ELSE NULL
END NULLS LAST,
--This may be improved in the future but is a good start
last_refresh_attempted_at NULLS FIRST,
priority
;
CREATE OR REPLACE VIEW fact_loader.queue_deps_all AS
WITH RECURSIVE fact_table_dep_cutoffs AS
(SELECT
1 AS level
, qtd.queue_table_dep_id
, ftdqc.fact_table_dep_id
, ftdqc.fact_table_dep_queue_table_dep_id
--This dep_maximum_cutoff_time is being taken from the queue_table_deps, because we cannot go past when the
--fact table has been updated
, qtd.last_cutoff_id AS dep_maximum_cutoff_id
, qtd.last_cutoff_source_time AS dep_maximum_cutoff_time
, ftd.parent_id AS parent_fact_table_id
, ftd.child_id AS child_fact_table_id
, ftd.child_id AS base_fact_table_id
, queue_table_id
, relevant_change_columns
, ftdqc.last_cutoff_id
, ftdqc.last_cutoff_source_time
, ftdqc.insert_merge_proid
, ftdqc.update_merge_proid
, ftdqc.delete_merge_proid
FROM fact_loader.queue_table_deps qtd
INNER JOIN fact_loader.fact_table_dep_queue_table_deps ftdqc ON ftdqc.queue_table_dep_id = qtd.queue_table_dep_id
INNER JOIN fact_loader.fact_table_deps ftd ON ftd.fact_table_dep_id = ftdqc.fact_table_dep_id
UNION ALL
/****
In this recursive part, we walk UP the chain to the base level in order to get the
last_cutoff_id and last_cutoff_source_time of parent_ids because children must never surpass those.
The ONLY difference between this recursive part and the non-recursive part are the dep_maximum_cutoffs.
That means we can get our resultant data below by simply selecting distinct ON the right fields and order
by dep_maximum_cutoffs to get the most conservative cutoff window, that is, the minimum cutoff amongst
the queue tables and any PARENT fact table cutoffs.
That means if, for example,
- IF a queue table has been cutoff up until 11:00:00
- AND IF a level 1 fact table dependent on that queue table was last cutoff at 10:55:00
- THEN a level 2 fact table dependent on level 1 fact table must not go past 10:55:00 when it is processed.
*/
SELECT
ftdc.level + 1 AS level
, ftdc.queue_table_dep_id
, ftdc.fact_table_dep_id
, ftdc.fact_table_dep_queue_table_dep_id
--This dep_maximum_cutoff_time is being taken from the queue_table_deps, because we cannot go past when the
--fact table has been updated
, ftdqc.last_cutoff_id AS dep_maximum_cutoff_id
, ftdqc.last_cutoff_source_time AS dep_maximum_cutoff_time
, ftd.parent_id AS parent_fact_table_id
, ftd.child_id AS child_fact_table_id
, ftdc.base_fact_table_id
, ftdc.queue_table_id
, ftdc.relevant_change_columns
, ftdc.last_cutoff_id
, ftdc.last_cutoff_source_time
, ftdc.insert_merge_proid
, ftdc.update_merge_proid
, ftdc.delete_merge_proid
FROM fact_loader.queue_table_deps qtd
INNER JOIN fact_loader.fact_table_dep_queue_table_deps ftdqc ON ftdqc.queue_table_dep_id = qtd.queue_table_dep_id
INNER JOIN fact_loader.fact_table_deps ftd ON ftd.fact_table_dep_id = ftdqc.fact_table_dep_id
INNER JOIN fact_table_dep_cutoffs ftdc ON ftdc.parent_fact_table_id = ftd.child_id
)
, adjusted_fact_table_deps AS (
/****
The reason we look at distinct queue_table_dep_id and not simply queue_table_id
is because two parent fact tables could have differing logic for retrieving changes
for the same base queue_tables.
*/
SELECT DISTINCT ON(base_fact_table_id, queue_table_dep_id)
*
FROM fact_table_dep_cutoffs
ORDER BY base_fact_table_id, queue_table_dep_id, dep_maximum_cutoff_time
)
, queue_table_info AS (
SELECT * FROM fact_loader.queue_table_delay_info()
)
/****
For fact tables that depend on other fact tables, we join the child fact table to the queue_table_deps of the parent
fact table, and just reuse this exactly, with these distinctions:
- From the fact_table_dep table, we do use the proids, and the last_cutoff_id
- We use the parent last_cutoff_source_time as the maximum_cutoff, because we can only update those records already updated on the parent
- We pass the information of which table for which to update metadata in the end
*/
, queue_table_deps_with_nested AS (
/****
This part of the union is for the base level of queue_table_deps - for fact tables with no other dependent fact tables
*/
SELECT
queue_table_dep_id
, NULL :: INT AS fact_table_dep_id
, NULL :: INT AS fact_table_dep_queue_table_dep_id
, NULL :: BIGINT AS dep_maximum_cutoff_id
, NULL :: TIMESTAMPTZ AS dep_maximum_cutoff_time
, fact_table_id
, queue_table_id
, relevant_change_columns
, last_cutoff_id
, last_cutoff_source_time
, insert_merge_proid
, update_merge_proid
, delete_merge_proid
FROM fact_loader.queue_table_deps
UNION ALL
/****
This part of the union is for fact tables with other dependent fact tables
*/
SELECT
queue_table_dep_id
, fact_table_dep_id
, fact_table_dep_queue_table_dep_id
, aftd.dep_maximum_cutoff_id
, aftd.dep_maximum_cutoff_time
, base_fact_table_id AS fact_table_id
, queue_table_id
, relevant_change_columns
, aftd.last_cutoff_id
, aftd.last_cutoff_source_time
, aftd.insert_merge_proid
, aftd.update_merge_proid
, aftd.delete_merge_proid
FROM adjusted_fact_table_deps aftd
)
SELECT
ft.fact_table_id,
ft.fact_table_relid,
ft.fact_table_agg_proid,
qt.queue_table_id,
qt.queue_table_relid,
qt.queue_of_base_table_relid,
qtd.relevant_change_columns,
qtd.last_cutoff_id,
qtd.last_cutoff_source_time,
rt.if_name AS provider_name,
rt.replication_set_name,
qtd.dep_maximum_cutoff_id, --Not used yet - TODO - think about if it needs to be used to filter as cutoff MAX in addition to the time filter
LEAST(
MIN(qtd.dep_maximum_cutoff_time)
OVER (
PARTITION BY qtd.fact_table_id ),
MIN(rt.source_time)
OVER (
PARTITION BY qtd.fact_table_id )
) AS maximum_cutoff_time,
aqt.queue_table_id_field,
'primary_key'::name AS queue_table_key,
'operation'::name AS queue_table_op,
'change'::name AS queue_table_change,
'changed_at'::name AS queue_table_timestamp,
qt.queue_table_tz,
aqbt.queue_of_base_table_key,
aqbt.queue_of_base_table_key_type,
queue_table_dep_id,
fact_table_dep_id,
fact_table_dep_queue_table_dep_id,
insert_merge_proid,
update_merge_proid,
delete_merge_proid,
qt.purge
FROM queue_table_deps_with_nested qtd
INNER JOIN fact_loader.fact_tables ft ON ft.fact_table_id = qtd.fact_table_id
INNER JOIN fact_loader.queue_tables qt ON qt.queue_table_id = qtd.queue_table_id
INNER JOIN queue_table_info rt ON rt.queue_of_base_table_relid = qt.queue_of_base_table_relid
INNER JOIN LATERAL
(SELECT a.attname AS queue_of_base_table_key, format_type(atttypid, atttypmod) AS queue_of_base_table_key_type
FROM (SELECT
i.indrelid
, unnest(indkey) AS ik
, row_number()
OVER () AS rn
FROM pg_index i
WHERE i.indrelid = qt.queue_of_base_table_relid AND i.indisprimary) pk
INNER JOIN pg_attribute a
ON a.attrelid = pk.indrelid AND a.attnum = pk.ik) aqbt ON TRUE
INNER JOIN LATERAL
(SELECT a.attname AS queue_table_id_field
FROM (SELECT
i.indrelid
, unnest(indkey) AS ik
, row_number()
OVER () AS rn
FROM pg_index i
WHERE i.indrelid = qt.queue_table_relid AND i.indisprimary) pk
INNER JOIN pg_attribute a
ON a.attrelid = pk.indrelid AND a.attnum = pk.ik) aqt ON TRUE
ORDER BY ft.fact_table_relid;
CREATE OR REPLACE VIEW fact_loader.queue_deps_all_with_retrieval AS
SELECT
qtd.*,
krs.filter_scope,
krs.level,
krs.return_columns, --we need not get the type separately. It must match queue_of_base_table_key_type
krs.is_fact_key,
krs.join_to_relation,
qtk.queue_table_relid AS join_to_relation_queue,
krs.join_to_column,
ctypes.join_column_type,
krs.return_columns_from_join,
ctypes.return_columns_from_join_type,
krs.join_return_is_fact_key,
/***
We include this in this view def to be easily shared by all events (I, U, D) in sql_builder,
as those may be different in terms of passing source_change_date.
*/
format(', %s::DATE AS source_change_date',
CASE
WHEN krs.pass_queue_table_change_date_at_tz IS NOT NULL
/***
For casting queue_table_timestamp to a date, we first ensure we have it as timestamptz (objective UTC time).
Then, we cast it to the timezone of interest on which the date should be based.
For example, 02:00:00 UTC time on 2018-05-02 is actually 2018-05-01 in America/Chicago time.
Thus, any date-based fact table must decide in what time zone to consider the date.
*/
THEN format('(%s %s AT TIME ZONE %s)',
'q.'||quote_ident(qtd.queue_table_timestamp),
CASE WHEN qtd.queue_table_tz IS NULL THEN '' ELSE 'AT TIME ZONE '||quote_literal(qtd.queue_table_tz) END,
quote_literal(krs.pass_queue_table_change_date_at_tz))
ELSE 'NULL'
END) AS source_change_date_select
FROM fact_loader.queue_deps_all qtd
INNER JOIN fact_loader.key_retrieval_sequences krs ON qtd.queue_table_dep_id = krs.queue_table_dep_id
LEFT JOIN fact_loader.queue_tables qtk ON qtk.queue_of_base_table_relid = krs.join_to_relation
LEFT JOIN LATERAL
(SELECT MAX(CASE WHEN attname = krs.join_to_column THEN format_type(atttypid, atttypmod) ELSE NULL END) AS join_column_type,
MAX(CASE WHEN attname = krs.return_columns_from_join[1] THEN format_type(atttypid, atttypmod) ELSE NULL END) AS return_columns_from_join_type
FROM pg_attribute a
WHERE a.attrelid IN(krs.join_to_relation)
/****
We stubbornly assume that if there are multiple columns in return_columns_from_join, they all have the same type.
Undue complexity would ensue if we did away with that rule.
*/
AND a.attname IN(krs.join_to_column,krs.return_columns_from_join[1])) ctypes ON TRUE;