Skip to content

Commit 44bd2b6

Browse files
committed
refactor: Update SQL scripts to use public schema for note_comments and related tables
- Modified multiple SQL scripts to reference note_comments, note_comments_text, and related tables with the public schema for consistency and to prevent potential schema resolution issues. - Ensured all JOIN operations are aligned with the updated table references, enhancing clarity and maintainability across the SQL codebase.
1 parent 303b6b5 commit 44bd2b6

11 files changed

+89
-89
lines changed

sql/dwh/Staging_32_createStagingObjects.sql

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
5353
-- This prevents race conditions where new notes are inserted while processing
5454
SELECT COALESCE(MAX(note_id), 0)
5555
INTO max_note_id_snapshot
56-
FROM notes;
56+
FROM public.notes;
5757
-- RAISE NOTICE 'Day % started.', max_processed_timestamp;
5858

5959
--RAISE NOTICE 'Flag 1: %', CLOCK_TIMESTAMP();
@@ -75,17 +75,17 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
7575
n.created_at created_at, o.id_user created_id_user, n.id_country id_country,
7676
c.sequence_action seq, c.event action_comment, c.id_user action_id_user,
7777
c.created_at action_at, t.body
78-
FROM note_comments c
79-
JOIN notes n
78+
FROM public.note_comments c
79+
JOIN public.notes n
8080
ON (c.note_id = n.note_id)
81-
JOIN note_comments o
81+
JOIN public.note_comments o
8282
ON (n.note_id = o.note_id
8383
-- Direct enum comparison (no CAST needed): foreign table is defined as note_event_enum
8484
-- ETL_60_setupFDW.sql ensures the foreign table uses note_event_enum type, not TEXT
8585
-- This allows PostgreSQL to use indexes and avoids full table scans
8686
AND o.event = ''opened''
8787
AND o.note_id <= ' || max_note_id_snapshot || ')
88-
LEFT JOIN note_comments_text t
88+
LEFT JOIN public.note_comments_text t
8989
ON (c.note_id = t.note_id AND c.sequence_action = t.sequence_action)
9090
9191
WHERE c.created_at >= ''' || max_processed_timestamp
@@ -101,17 +101,17 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
101101
n.created_at created_at, o.id_user created_id_user, n.id_country id_country,
102102
c.sequence_action seq, c.event action_comment, c.id_user action_id_user,
103103
c.created_at action_at, t.body
104-
FROM note_comments c
105-
JOIN notes n
104+
FROM public.note_comments c
105+
JOIN public.notes n
106106
ON (c.note_id = n.note_id)
107-
JOIN note_comments o
107+
JOIN public.note_comments o
108108
ON (n.note_id = o.note_id
109109
-- Direct enum comparison (no CAST needed): foreign table is defined as note_event_enum
110110
-- ETL_60_setupFDW.sql ensures the foreign table uses note_event_enum type, not TEXT
111111
-- This allows PostgreSQL to use indexes and avoids full table scans
112112
AND o.event = ''opened''
113113
AND o.note_id <= ' || max_note_id_snapshot || ')
114-
LEFT JOIN note_comments_text t
114+
LEFT JOIN public.note_comments_text t
115115
ON (c.note_id = t.note_id AND c.sequence_action = t.sequence_action)
116116
117117
WHERE c.created_at > ''' || max_processed_timestamp
@@ -144,7 +144,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
144144
INSERT INTO dwh.dimension_countries
145145
(country_id, country_name, country_name_es, country_name_en)
146146
SELECT /* Notes-staging */ c.country_id, c.country_name, c.country_name_es, c.country_name_en
147-
FROM countries c
147+
FROM public.countries c
148148
WHERE c.country_id = rec_note_action.id_country
149149
AND c.country_id NOT IN (SELECT country_id FROM dwh.dimension_countries)
150150
ON CONFLICT (country_id) DO NOTHING
@@ -286,7 +286,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date (
286286
-- be executed in READ ONLY mode for better concurrency, but this procedure also
287287
-- performs writes, so READ ONLY cannot be applied to the entire transaction.
288288
SELECT n.latitude, n.longitude INTO m_latitude, m_longitude
289-
FROM notes n WHERE n.note_id = rec_note_action.id_note;
289+
FROM public.notes n WHERE n.note_id = rec_note_action.id_note;
290290
m_timezone_id := dwh.get_timezone_id_by_lonlat(m_longitude, m_latitude);
291291
m_local_action_id_date := dwh.get_local_date_id(rec_note_action.action_at, m_timezone_id);
292292
m_local_action_id_hour_of_week := dwh.get_local_hour_of_week_id(rec_note_action.action_at, m_timezone_id);
@@ -428,7 +428,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
428428
-- This prevents race conditions where new notes are inserted while processing
429429
SELECT COALESCE(MAX(note_id), 0)
430430
INTO max_note_id_snapshot
431-
FROM notes;
431+
FROM public.notes;
432432

433433
-- Use this snapshot for all queries to ensure referential integrity
434434
-- All note_comments processed will have their corresponding notes
@@ -466,14 +466,14 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
466466
-- Filter by max_note_id_snapshot to ensure consistency
467467
SELECT /* Notes-staging */ MAX(DATE(created_at))
468468
INTO max_note_action_date
469-
FROM note_comments
469+
FROM public.note_comments
470470
WHERE note_id <= max_note_id_snapshot;
471471

472472
-- Start from the first date with comments
473473
-- Filter by max_note_id_snapshot to ensure consistency
474474
SELECT /* Notes-staging */ MIN(DATE(created_at))
475475
INTO max_processed_date
476-
FROM note_comments
476+
FROM public.note_comments
477477
WHERE note_id <= max_note_id_snapshot;
478478

479479
-- Process all dates from first date until the latest date (skip empty days)
@@ -495,7 +495,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
495495
-- OPTIMIZATION: Use timestamp range instead of DATE() to allow index usage on FDW
496496
SELECT /* Notes-staging */ COUNT(1)
497497
INTO qty_notes_on_date
498-
FROM note_comments
498+
FROM public.note_comments
499499
WHERE created_at >= max_processed_date::TIMESTAMP
500500
AND created_at < (max_processed_date + INTERVAL '1 day')::TIMESTAMP
501501
AND created_at > max_note_on_dwh_timestamp
@@ -512,8 +512,8 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
512512
-- OPTIMIZATION: Use timestamp range instead of DATE() to allow index usage on FDW
513513
SELECT /* Notes-staging */ MIN(DATE(created_at))
514514
INTO max_processed_date
515-
FROM note_comments
516-
WHERE created_at >= (max_processed_date + INTERVAL '1 day')::TIMESTAMP
515+
FROM public.note_comments
516+
WHERE created_at >= (max_processed_date + INTERVAL '1 day')::TIMESTAMP
517517
AND note_id <= max_note_id_snapshot;
518518

519519
-- If no more dates with comments, exit loop
@@ -556,7 +556,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
556556
-- Filter by max_note_id_snapshot to ensure consistency
557557
SELECT /* Notes-staging */ MAX(DATE(created_at))
558558
INTO max_note_action_date
559-
FROM note_comments
559+
FROM public.note_comments
560560
WHERE note_id <= max_note_id_snapshot;
561561
--RAISE NOTICE 'Max date with comments in base tables: %', max_note_action_date;
562562
--RAISE NOTICE '1Flag 4: %', CLOCK_TIMESTAMP();
@@ -605,13 +605,13 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
605605
-- processed.
606606
-- Filter by max_note_id_snapshot to ensure consistency
607607
-- OPTIMIZATION: Use timestamp range instead of DATE() to allow index usage on FDW
608-
SELECT /* Notes-staging */ COUNT(1)
609-
INTO qty_notes_on_date
610-
FROM note_comments
611-
WHERE created_at >= max_processed_date::TIMESTAMP
612-
AND created_at < (max_processed_date + INTERVAL '1 day')::TIMESTAMP
613-
AND created_at > max_note_on_dwh_timestamp
614-
AND note_id <= max_note_id_snapshot;
608+
SELECT /* Notes-staging */ COUNT(1)
609+
INTO qty_notes_on_date
610+
FROM public.note_comments
611+
WHERE created_at >= max_processed_date::TIMESTAMP
612+
AND created_at < (max_processed_date + INTERVAL '1 day')::TIMESTAMP
613+
AND created_at > max_note_on_dwh_timestamp
614+
AND note_id <= max_note_id_snapshot;
615615
--RAISE NOTICE 'count notes to process on date %: %.', max_processed_date,
616616
--qty_notes_on_date;
617617
--RAISE NOTICE '1Flag 7: %', CLOCK_TIMESTAMP();
@@ -625,8 +625,8 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
625625
-- OPTIMIZATION: Use timestamp range instead of DATE() to allow index usage on FDW
626626
SELECT /* Notes-staging */ MIN(DATE(created_at))
627627
INTO max_processed_date
628-
FROM note_comments
629-
WHERE created_at >= (max_processed_date + INTERVAL '1 day')::TIMESTAMP
628+
FROM public.note_comments
629+
WHERE created_at >= (max_processed_date + INTERVAL '1 day')::TIMESTAMP
630630
AND note_id <= max_note_id_snapshot;
631631

632632
-- If no more dates with comments, exit loop
@@ -671,7 +671,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_dwh (
671671
-- OPTIMIZATION: Use timestamp range instead of DATE() to allow index usage on FDW
672672
SELECT /* Notes-staging */ COUNT(1)
673673
INTO qty_notes_on_date
674-
FROM note_comments
674+
FROM public.note_comments
675675
WHERE created_at >= max_processed_date::TIMESTAMP
676676
AND created_at < (max_processed_date + INTERVAL '1 day')::TIMESTAMP
677677
AND created_at > max_note_on_dwh_timestamp

sql/dwh/Staging_33_initialFactsBaseObjects.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ BEGIN
135135
END
136136
$$;
137137

138-
CREATE INDEX IF NOT EXISTS comments_function_year ON note_comments (EXTRACT(YEAR FROM created_at), created_at);
138+
CREATE INDEX IF NOT EXISTS comments_function_year ON public.note_comments (EXTRACT(YEAR FROM created_at), created_at);
139139
COMMENT ON INDEX comments_function_year IS
140140
'Index to improve access when processing ETL per years';
141141

sql/dwh/Staging_33_initialFactsBaseObjects_Simple.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ END
142142
$$;
143143

144144
-- Create index for performance
145-
CREATE INDEX IF NOT EXISTS comments_function_year ON note_comments (EXTRACT(YEAR FROM created_at), created_at);
145+
CREATE INDEX IF NOT EXISTS comments_function_year ON public.note_comments (EXTRACT(YEAR FROM created_at), created_at);
146146
COMMENT ON INDEX comments_function_year IS
147147
'Index to improve access when processing ETL per years';
148148

sql/dwh/Staging_34_initialFactsLoadCreate.sql

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date_${YEAR} (
7777
n.created_at created_at, o.id_user created_id_user, n.id_country id_country,
7878
c.sequence_action seq, c.event action_comment, c.id_user action_id_user,
7979
c.created_at action_at, t.body
80-
FROM note_comments c
81-
JOIN notes n
80+
FROM public.note_comments c
81+
JOIN public.notes n
8282
ON (c.note_id = n.note_id)
83-
JOIN note_comments o
83+
JOIN public.note_comments o
8484
ON (n.note_id = o.note_id
8585
AND o.event = ''opened'')
86-
LEFT JOIN note_comments_text t
86+
LEFT JOIN public.note_comments_text t
8787
ON (c.note_id = t.note_id AND c.sequence_action = t.sequence_action)
8888
8989
WHERE c.created_at >= ''' || max_processed_timestamp
@@ -98,13 +98,13 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date_${YEAR} (
9898
n.created_at created_at, o.id_user created_id_user, n.id_country id_country,
9999
c.sequence_action seq, c.event action_comment, c.id_user action_id_user,
100100
c.created_at action_at, t.body
101-
FROM note_comments c
102-
JOIN notes n
101+
FROM public.note_comments c
102+
JOIN public.notes n
103103
ON (c.note_id = n.note_id)
104-
JOIN note_comments o
104+
JOIN public.note_comments o
105105
ON (n.note_id = o.note_id
106106
AND o.event = ''opened'')
107-
LEFT JOIN note_comments_text t
107+
LEFT JOIN public.note_comments_text t
108108
ON (c.note_id = t.note_id AND c.sequence_action = t.sequence_action)
109109
110110
WHERE c.created_at > ''' || max_processed_timestamp
@@ -133,7 +133,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date_${YEAR} (
133133
INSERT INTO dwh.dimension_countries
134134
(country_id, country_name, country_name_es, country_name_en)
135135
SELECT /* Notes-staging */ c.country_id, c.country_name, c.country_name_es, c.country_name_en
136-
FROM countries c
136+
FROM public.countries c
137137
WHERE c.country_id = rec_note_action.id_country
138138
AND c.country_id NOT IN (SELECT country_id FROM dwh.dimension_countries)
139139
ON CONFLICT (country_id) DO NOTHING
@@ -202,7 +202,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date_${YEAR} (
202202
IF (rec_note_action.action_comment = 'opened') THEN
203203
SELECT /* Notes-staging */ body
204204
INTO m_text_comment
205-
FROM note_comments_text
205+
FROM public.note_comments_text
206206
WHERE note_id = rec_note_action.id_note
207207
AND sequence_action = rec_note_action.seq; -- Sequence should be 1.
208208
--RAISE NOTICE 'Flag 8: %', CLOCK_TIMESTAMP();
@@ -255,7 +255,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_at_date_${YEAR} (
255255
-- Defaults for local/tz/season
256256
-- Compute tz/season using note coordinates
257257
SELECT n.latitude, n.longitude INTO m_latitude, m_longitude
258-
FROM notes n WHERE n.note_id = rec_note_action.id_note;
258+
FROM public.notes n WHERE n.note_id = rec_note_action.id_note;
259259
m_timezone_id := dwh.get_timezone_id_by_lonlat(m_longitude, m_latitude);
260260
m_local_action_id_date := dwh.get_local_date_id(rec_note_action.action_at, m_timezone_id);
261261
m_local_action_id_hour_of_week := dwh.get_local_hour_of_week_id(rec_note_action.action_at, m_timezone_id);
@@ -369,7 +369,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_staging_${YEAR} (
369369
--RAISE NOTICE '0 facts, processing all year ${YEAR}. It could take several hours.';
370370
SELECT /* Notes-staging */ MIN(created_at)
371371
INTO min_timestamp
372-
FROM note_comments
372+
FROM public.note_comments
373373
WHERE EXTRACT(YEAR FROM created_at) = ${YEAR};
374374
CALL staging.process_notes_at_date_${YEAR}(min_timestamp, qty_dwh_notes,
375375
TRUE);
@@ -380,7 +380,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_staging_${YEAR} (
380380
-- Gets the date of the most recent note action from base tables.
381381
SELECT /* Notes-staging */ MAX(DATE(created_at))
382382
INTO max_note_action_date
383-
FROM note_comments
383+
FROM public.note_comments
384384
WHERE EXTRACT(YEAR FROM created_at) = ${YEAR};
385385
--RAISE NOTICE 'recursive case %.', max_note_action_date;
386386
--RAISE NOTICE '1Flag 3: %', CLOCK_TIMESTAMP();
@@ -420,7 +420,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_staging_${YEAR} (
420420
-- processed.
421421
SELECT /* Notes-staging */ COUNT(1)
422422
INTO qty_notes_on_date
423-
FROM note_comments
423+
FROM public.note_comments
424424
WHERE DATE(created_at) = max_processed_date
425425
AND created_at > max_note_on_dwh_timestamp
426426
AND EXTRACT(YEAR FROM created_at) = ${YEAR};
@@ -434,7 +434,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_staging_${YEAR} (
434434
-- Find next date that actually has comments (skip empty days) for this year
435435
SELECT /* Notes-staging */ MIN(DATE(created_at))
436436
INTO max_processed_date
437-
FROM note_comments
437+
FROM public.note_comments
438438
WHERE DATE(created_at) > max_processed_date
439439
AND EXTRACT(YEAR FROM created_at) = ${YEAR};
440440

@@ -452,7 +452,7 @@ CREATE OR REPLACE PROCEDURE staging.process_notes_actions_into_staging_${YEAR} (
452452
-- being processed.
453453
SELECT /* Notes-staging */ COUNT(1)
454454
INTO qty_notes_on_date
455-
FROM note_comments
455+
FROM public.note_comments
456456
WHERE DATE(created_at) = max_processed_date
457457
AND created_at > max_note_on_dwh_timestamp
458458
AND EXTRACT(YEAR FROM created_at) = ${YEAR};

sql/dwh/Staging_34_initialFactsLoadCreate_Parallel.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ BEGIN
5353
n.created_at created_at, o.id_user created_id_user, n.id_country id_country,
5454
c.sequence_action seq, c.event action_comment, c.id_user action_id_user,
5555
c.created_at action_at, t.body
56-
FROM note_comments c
57-
JOIN notes n
56+
FROM public.note_comments c
57+
JOIN public.notes n
5858
ON (c.note_id = n.note_id)
59-
JOIN note_comments o
59+
JOIN public.note_comments o
6060
ON (n.note_id = o.note_id
6161
AND o.event = 'opened')
62-
LEFT JOIN note_comments_text t
62+
LEFT JOIN public.note_comments_text t
6363
ON (c.note_id = t.note_id AND c.sequence_action = t.sequence_action)
6464
WHERE EXTRACT(YEAR FROM c.created_at) = ${YEAR}
6565
ORDER BY c.note_id, c.sequence_action;
@@ -78,7 +78,7 @@ BEGIN
7878
INSERT INTO dwh.dimension_countries
7979
(country_id, country_name, country_name_es, country_name_en)
8080
SELECT /* Notes-staging */ c.country_id, c.country_name, c.country_name_es, c.country_name_en
81-
FROM countries c
81+
FROM public.countries c
8282
WHERE c.country_id = rec_note_action.id_country
8383
AND c.country_id NOT IN (SELECT country_id FROM dwh.dimension_countries)
8484
ON CONFLICT (country_id) DO NOTHING
@@ -173,7 +173,7 @@ BEGIN
173173

174174
-- Get timezone and local time info
175175
SELECT n.latitude, n.longitude INTO m_latitude, m_longitude
176-
FROM notes n WHERE n.note_id = rec_note_action.id_note;
176+
FROM public.notes n WHERE n.note_id = rec_note_action.id_note;
177177
m_timezone_id := dwh.get_timezone_id_by_lonlat(m_longitude, m_latitude);
178178
m_local_action_id_date := dwh.get_local_date_id(rec_note_action.action_at, m_timezone_id);
179179
m_local_action_id_hour_of_week := dwh.get_local_hour_of_week_id(rec_note_action.action_at, m_timezone_id);

sql/dwh/Staging_35_initialFactsLoadExecute_Simple.sql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ BEGIN
5050
n.created_at created_at, o.id_user created_id_user, n.id_country id_country,
5151
c.sequence_action seq, c.event action_comment, c.id_user action_id_user,
5252
c.created_at action_at, t.body
53-
FROM note_comments c
54-
JOIN notes n
53+
FROM public.note_comments c
54+
JOIN public.notes n
5555
ON (c.note_id = n.note_id)
56-
JOIN note_comments o
56+
JOIN public.note_comments o
5757
ON (n.note_id = o.note_id
5858
AND o.event = 'opened')
59-
LEFT JOIN note_comments_text t
59+
LEFT JOIN public.note_comments_text t
6060
ON (c.note_id = t.note_id AND c.sequence_action = t.sequence_action)
6161
ORDER BY c.note_id, c.sequence_action;
6262

@@ -74,7 +74,7 @@ BEGIN
7474
INSERT INTO dwh.dimension_countries
7575
(country_id, country_name, country_name_es, country_name_en)
7676
SELECT /* Notes-staging */ c.country_id, c.country_name, c.country_name_es, c.country_name_en
77-
FROM countries c
77+
FROM public.countries c
7878
WHERE c.country_id = rec_note_action.id_country
7979
AND c.country_id NOT IN (SELECT country_id FROM dwh.dimension_countries)
8080
ON CONFLICT (country_id) DO NOTHING
@@ -193,7 +193,7 @@ BEGIN
193193

194194
-- Get timezone and local time info
195195
SELECT n.latitude, n.longitude INTO m_latitude, m_longitude
196-
FROM notes n WHERE n.note_id = rec_note_action.id_note;
196+
FROM public.notes n WHERE n.note_id = rec_note_action.id_note;
197197
m_timezone_id := dwh.get_timezone_id_by_lonlat(m_longitude, m_latitude);
198198
m_local_action_id_date := dwh.get_local_date_id(rec_note_action.action_at, m_timezone_id);
199199
m_local_action_id_hour_of_week := dwh.get_local_hour_of_week_id(rec_note_action.action_at, m_timezone_id);

sql/dwh/Staging_61_loadNotes.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ SELECT /* Notes-staging */ COUNT(1) AS facts, 0 AS comments
1111
FROM dwh.facts
1212
UNION
1313
SELECT /* Notes-staging */ 0 AS facts, count(1) AS comments
14-
FROM note_comments;
14+
FROM public.note_comments;
1515
COMMIT;
1616

1717
SELECT /* Notes-staging */ clock_timestamp() AS Processing,
@@ -29,5 +29,5 @@ SELECT /* Notes-staging */ COUNT(1) AS facts, 0 AS comments
2929
FROM dwh.facts
3030
UNION
3131
SELECT /* Notes-staging */ 0 AS facts, count(1) AS comments
32-
FROM note_comments;
32+
FROM public.note_comments;
3333
COMMIT;

0 commit comments

Comments
 (0)