循环脚本无法正常工作

时间:2015-07-02 21:38:58

标签: postgresql postgresql-9.4

在我的数据库中有以下脚本:

1)

library("shiny")
library("dplyr")

n <- 1000
taxi <- data.frame(day = sample(1:7, n, TRUE),
                   hour = sample(0:23,n, TRUE),
                   source= sample(1:4, n, TRUE),
                   service= sample(1:5, n, TRUE),
                   relevancy= sample(1:4, n, TRUE),
                   tollfree= sample(c(0,1), n, TRUE),
                   distance= sample(0:15, n, TRUE),
                   similarity= sample(seq(0,1,0.01), n, TRUE),
                   simi.names= sample(c('[0,0.25)','[0.25,0.5)','[0.5,0.75)','[0.75,1]'), n, TRUE),
                   dist.names= sample(c('[0,1)','[1,2)','[2,3)','[3,4)','[4,15]'), n, TRUE),
                   week= sample(1:7, n, TRUE),
                   rel= sample(c(1,4), n, TRUE),
                   conversion = sample(0:1, n, TRUE))

shinyServer(function(input, output, session) {
  datasetInput <- reactive({
    switch(input$dataset,
           "taxicabs" = taxi,
           "liquor stores" = liq)
  })

  observe({
    choices <- c("day", "hour", "source", "service", "relevancy", "tollfree", "distance", "similarity")
    if (grepl("day in weekdays/weekends", input$agg))  {
      choices[1] <- "week"
    }
    if (grepl("relevancy", input$agg))  {
      choices[5] <- "rel"
    }      
    if (grepl("similarity", input$discrete)) {
      choices[8] <- "simi.names"
    }
    if (grepl("distance", input$discrete)) {
      choices[7] <- "dist.names"
    }
    updateCheckboxGroupInput(session, "checkGroup", choices = choices,
                               inline = F, selected = choices)
  })

  datasetagg <- reactive({ 
    cg <- input$checkGroup
    dis <- input$discrete
    cg_not_d_or_s <- cg[!(cg %in% c("distance", "similarity"))]
    if(input$discrete == "similarity & distance") {
      #all discrete 
      right_join(
        datasetInput() %>%
          select_(.dots = cg) %>%
          group_by_(.dots = cg) %>%
          summarise(count=n()),
        datasetInput() %>%
          filter(conversion==1) %>%
          select_(.dots = cg) %>%
          count_(vars = cg)
      ) %>% mutate(prop.conv = n/count)
    } else if(input$discrete %in% c("similarity", "distance")) {
      cg_not_dis <- cg[cg != dis]
        # one continuous
      right_join(
        datasetInput() %>%
          group_by_(.dots = cg_not_dis) %>%
          summarise_(.dots = setNames(c(paste0("mean(", dis, ")"), "n()"),
                                      c(paste0(substr(dis, 1, 4), ".mean"), "count"))) %>%
          select_(.dots = c(cg_not_dis, paste0(substr(dis, 1, 4), ".mean"), "count")),
        datasetInput() %>%
          filter(conversion==1) %>%
          select_(.dots = cg_not_dis) %>%
          count_(vars = cg_not_dis)
      ) %>% mutate(prop.conv = n/count)
    } else if(input$discrete == "none") {
      # two  
      right_join(
        datasetInput() %>%
          select_(.dots = cg) %>%
          group_by_(.dots = cg_not_d_or_s) %>%
          summarise(dist.mean=mean(distance), simi.mean=mean(similarity), count=n()),
        datasetInput() %>%
          filter(conversion==1) %>%
          select_(.dots = cg) %>%
          count_(vars = cg_not_d_or_s)
      ) %>% mutate(prop.conv = n/count)
    }
  })

  # head of the table  
  output$view <- renderTable({
    head(datasetagg(), n = input$obs)
  })

})

2)

create or replace function admin.f_partition_manager(schema_name varchar(100)) returns text as $$
begin
    EXECUTE 'alter table ' || schema_name || '.eventandalarmreportentry alter column receivedate set not null';
    EXECUTE 'alter table ' || schema_name || '.alarmgroupentry alter column date set not null';
    EXECUTE 'alter table ' || schema_name || '.patrolreportentry alter column startdate set not null';
    EXECUTE 'alter table ' || schema_name || '.alarmreportentry alter column detectdate set not null';

    PERFORM partman.create_parent(schema_name || '.alarmgroupentry', 'date', 'time', 'monthly');
    PERFORM partman.create_parent(schema_name || '.eventandalarmreportentry', 'receivedate', 'time', 'monthly');
    PERFORM partman.create_parent(schema_name || '.patrolreportentry', 'startdate', 'time', 'monthly');
    PERFORM partman.create_parent(schema_name || '.alarmreportentry', 'detectdate', 'time', 'monthly');
    return schema_name;
end;
$$ language plpgsql;

当我正在运行第三个脚本时:

create or replace function admin.f_partition_manager_2() returns text as $$
declare
    rek RECORD;
begin
FOR rek IN
  (SELECT *
   FROM information_schema.schemata i
   WHERE i.schema_name LIKE 'activeview_%'
   and i.schema_name <> 'activeview_2664'
   )
  LOOP
PERFORM admin.f_partition_manager(rek.schema_name);
 return null;
  END LOOP;

end;
$$ language plpgsql;

已处理只是一个架构。 有什么问题?

脚本必须在现有数据库上创建分区,并通过partman将其添加到每月创建中。

0 个答案:

没有答案