How can I ensure that a materialized view is always up to date?

前端 未结 2 1826
[愿得一人]
[愿得一人] 2020-12-07 08:26

I\'ll need to invoke REFRESH MATERIALIZED VIEW on each change to the tables involved, right? I\'m surprised to not find much discussion of this on the web.

2条回答
  •  庸人自扰
    2020-12-07 09:20

    Let me point out three things on the previous answer by MatheusOl - the pglater technology.

    1. As the last element of long_options array it should include "{0, 0, 0, 0}" element as pointed at https://linux.die.net/man/3/getopt_long by the phrase "The last element of the array has to be filled with zeros." So, it should read -

      static struct option long_options[] =     {
            //......
            {"help", no_argument, NULL, '?'},
            {0, 0, 0, 0} 
      };
      
    2. On the malloc/free thing -- one free(for char listen = malloc(...);) is missing. Anyhow, malloc caused pglater process to crash on CentOS (but not on Ubuntu - I don't know why). So, I recommend using char array and assign the array name to the char pointer(to both char and char**). You many need to force type conversion while you do that(pointer assignment).

      char block4[100];
      ...
      password_prompt = block4;
      ...
      char block1[500];
      const char **keywords = (const char **)&block1;
      ...
      char block3[300];
      char *listen = block3;
      sprintf(listen, "listen %s", id);
      PQfreemem(id);
      res = PQexec(db, listen);
      
    3. Use below table to calculate timeout where md is mature_duration which is the time difference between the latest refresh(lr) time point and current time.

      when md >= callback_delay(cd) ==> timeout: 0

      when md + PING_INTERVAL >= cd ==> timeout: cd-md[=cd-(now-lr)]

      when md + PING_INTERVAL < cd ==> timeout: PI

    To implement this algorithm(3rd point), you should init 'lr' as follows -

    res = PQexec(db, command);
    latest_refresh = time(0);
    if (PQresultStatus(res) == PGRES_COMMAND_OK) {
    

提交回复
热议问题