allow comma separated values if timestamp not included
[sgn.git] / bin / refresh_matviews.pl
blobb4ebb06bdb3c6cbdedffb87dbee9d3ec6121b753
1 #!/usr/bin/perl
3 =head1 NAME
5 refresh_matviews.pl - run PL/pgSQL functions to do a basic or concurrent refresh of all database materialized views
7 =head1 DESCRIPTION
9 refresh_matviews.pl -H [database handle] -D [database name] -c [to run concurrent refresh] -m [materialized view select]
11 Options:
13 -H the database host
14 -D the database name
15 -c flag; if present, run concurrent refresh
16 -m materialized view select. can be either 'fullview' or 'stockprop'
18 All materialized views that are included in the refresh function will be refreshed
19 If -c is used, the refresh will be done concurrently, a process that takes longer than a standard refresh but that is completed without locking the views.
21 =head1 AUTHOR
23 Bryan Ellerbrock <bje24@cornell.edu>
25 =cut
27 use strict;
28 use warnings;
29 use Getopt::Std;
30 use DBI;
31 #use CXGN::DB::InsertDBH;
33 our ($opt_H, $opt_D, $opt_U, $opt_P, $opt_m, $opt_c, $refresh, $status);
34 getopts('H:D:U:P:m:c');
36 print STDERR "Connecting to database...\n";
37 my $dsn = 'dbi:Pg:database='.$opt_D.";host=".$opt_H.";port=5432";
38 my $dbh = DBI->connect($dsn, $opt_U, $opt_P);
40 eval {
41 print STDERR "Refreshing materialized views . . ." . localtime() . "\n";
43 if ($opt_m eq 'fullview'){
44 my $q = "UPDATE public.matviews SET currently_refreshing=?";
45 my $state = 'TRUE';
46 my $h = $dbh->prepare($q);
47 $h->execute($state);
49 if ($opt_c) {
50 $refresh = 'SELECT refresh_materialized_views_concurrently()';
51 } else {
52 $refresh = 'SELECT refresh_materialized_views()';
55 $h = $dbh->prepare($refresh);
56 $status = $h->execute();
58 if ($opt_c) {
59 $refresh = 'SELECT refresh_materialized_stockprop_concurrently()';
60 } else {
61 $refresh = 'SELECT refresh_materialized_stockprop()';
64 $h = $dbh->prepare($refresh);
65 $status = $h->execute();
67 $q = "UPDATE public.matviews SET currently_refreshing=?";
68 $state = 'FALSE';
69 $h = $dbh->prepare($q);
70 $h->execute($state);
73 if ($opt_m eq 'stockprop'){
74 if ($opt_c) {
75 $refresh = 'SELECT refresh_materialized_stockprop_concurrently()';
76 } else {
77 $refresh = 'SELECT refresh_materialized_stockprop()';
80 my $h = $dbh->prepare($refresh);
81 $status = $h->execute();
84 print STDERR "Materialized views refreshed! Status: $status" . localtime() . "\n";
87 if ($@) {
88 $dbh->rollback();
89 print STDERR $@;
90 } else {
91 print STDERR "Done, exiting refresh_matviews.pl \n";