     current_time := timeofday()::timestamp;
 
     RAISE NOTICE 'time - %', current_time;
-    FOR media_rec in  SELECT media_id, coalesce( sw_data_start_date, default_start_day ) as start_date FROM media where not (coalesce ( sw_data_start_date, default_start_day ) is null ) ORDER BY media_id LOOP
+
+    FOR media_rec IN (
+          SELECT media_id,
+                 COALESCE( sw_data_start_date, default_start_day ) AS start_date
+          FROM media
+          WHERE NOT (COALESCE( sw_data_start_date, default_start_day ) IS NULL )
+          ORDER BY media_id
+      ) LOOP
         current_time := timeofday()::timestamp;
+
         RAISE NOTICE 'media_id is %, start_date - % time - %', media_rec.media_id, media_rec.start_date, current_time;
-        DELETE from story_sentences where media_id = media_rec.media_id and date_trunc( 'day', publish_date ) < date_trunc( 'day', media_rec.start_date ); 
+
+        DELETE FROM story_sentences
+        WHERE media_id = media_rec.media_id
+          AND date_trunc( 'day', publish_date ) < date_trunc( 'day', media_rec.start_date );
+
     END LOOP;
 
   RAISE NOTICE 'time - %', current_time;  -- Prints 30
-  FOR media_rec in  SELECT media_id, coalesce( sw_data_end_date, default_end_day ) as end_date FROM media where not (coalesce ( sw_data_end_date, default_end_day ) is null ) ORDER BY media_id LOOP
+  FOR media_rec IN (
+          SELECT media_id,
+                 COALESCE( sw_data_end_date, default_end_day ) AS end_date
+          FROM media
+          WHERE NOT (COALESCE( sw_data_end_date, default_end_day ) IS NULL )
+          ORDER BY media_id
+      ) LOOP
         current_time := timeofday()::timestamp;
+
         RAISE NOTICE 'media_id is %, end_date - % time - %', media_rec.media_id, media_rec.end_date, current_time;
-        DELETE from story_sentences where media_id = media_rec.media_id and date_trunc( 'day', publish_date ) > date_trunc( 'day', media_rec.end_date ); 
+
+        DELETE from story_sentences
+        WHERE media_id = media_rec.media_id
+          AND date_trunc( 'day', publish_date ) > date_trunc( 'day', media_rec.end_date );
+
     END LOOP;
 END;
 $$

);

create index media_stats_medium on media_stats( media_id );
    
create type feed_feed_type AS ENUM ( 'syndicated', 'web_page' );
    

CREATE VIEW downloads_in_past_day as select * from downloads where download_time > now() - interval '1 day';
CREATE VIEW downloads_with_error_in_past_day as select * from downloads_in_past_day where state = 'error';

CREATE VIEW daily_stats AS
    SELECT *
    FROM (
            SELECT COUNT(*) AS daily_downloads
            FROM downloads_in_past_day
         ) AS dd,
         (
            SELECT COUNT(*) AS daily_stories
            FROM stories_collected_in_past_day
         ) AS ds,
         (
            SELECT COUNT(*) AS downloads_to_be_extracted
            FROM downloads_to_be_extracted
         ) AS dex,
         (
            SELECT COUNT(*) AS download_errors
            FROM downloads_with_error_in_past_day
         ) AS er;

CREATE TABLE feedless_stories (
        stories_id integer,
