Uppercase SQL keywords. Remove spurious dot from error message. Improve comments
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 9 Jan 2015 09:39:54 +0000 (11:39 +0200)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 16 Jan 2015 11:37:07 +0000 (13:37 +0200)
contrib/pg_rewind/filemap.c
contrib/pg_rewind/filemap.h
contrib/pg_rewind/libpq_fetch.c

index 3d6910093052cce954bf6ff8bbecca34c531b972..0e62d7971b100c7a82a85dd902401687a9f9317d 100644 (file)
@@ -94,7 +94,7 @@ process_remote_file(const char *path, file_type_t type, size_t newsize,
         */
        if (type != FILE_TYPE_REGULAR && isRelDataFile(path))
        {
-               fprintf(stderr, "data file in source \"%s\" is a directory.\n", path);
+               fprintf(stderr, "data file in source \"%s\" is a directory\n", path);
                exit(1);
        }
 
@@ -483,6 +483,11 @@ print_filemap(void)
 
 /*
  * Does it look like a relation data file?
+ *
+ * For our purposes, only files belonging to the main fork are considered
+ * relation files. Other forks are alwayes copied in toto, because we cannot
+ * reliably track changes to the, because WAL only contains block references
+ * for the main fork.
  */
 static bool
 isRelDataFile(const char *path)
index d806adff2164b517c726f32108c8b9152dca4fac..1ce82a433b7731ad209b9d23342dc504811f408e 100644 (file)
@@ -72,7 +72,7 @@ struct filemap_t
 
        /*
         * After processing all the remote files, the entries in the linked list
-        * are moved to this array. After processing local file, too, all the
+        * are moved to this array. After processing local files, too, all the
         * local entries are added to the array by filemap_finalize, and sorted
         * in the final order. After filemap_finalize, all the entries are in
         * the array, and the linked list is empty.
index b5f5314a4aa7bbb5f823e836144a90f44e47689c..a781b7aa4a3cb83d9af9cd66dc0dec90c61ac3a3 100644 (file)
@@ -30,6 +30,9 @@
 
 static PGconn *conn = NULL;
 
+/*
+ * Relation files are fetched max CHUNKSIZE bytes at a time.
+ */
 #define CHUNKSIZE 1000000
 
 static void receiveFileChunks(const char *sql);
@@ -62,17 +65,17 @@ libpqProcessFileList(void)
 
        sql =
                "-- Create a recursive directory listing of the whole data directory\n"
-               "with recursive files (path, filename, size, isdir) as (\n"
-               "  select '' as path, filename, size, isdir from\n"
-               "  (select pg_ls_dir('.') as filename) as fn,\n"
-               "        pg_stat_file(fn.filename) as this\n"
-               "  union all\n"
-               "  select parent.path || parent.filename || '/' as path,\n"
+               "WITH RECURSIVE files (path, filename, size, isdir) AS (\n"
+               "  SELECT '' AS path, filename, size, isdir FROM\n"
+               "  (SELECT pg_ls_dir('.') AS filename) AS fn,\n"
+               "        pg_stat_file(fn.filename) AS this\n"
+               "  UNION ALL\n"
+               "  SELECT parent.path || parent.filename || '/' AS path,\n"
                "         fn, this.size, this.isdir\n"
-               "  from files as parent,\n"
-               "       pg_ls_dir(parent.path || parent.filename) as fn,\n"
-               "       pg_stat_file(parent.path || parent.filename || '/' || fn) as this\n"
-               "       where parent.isdir = 't'\n"
+               "  FROM files AS parent,\n"
+               "       pg_ls_dir(parent.path || parent.filename) AS fn,\n"
+               "       pg_stat_file(parent.path || parent.filename || '/' || fn) AS this\n"
+               "       WHERE parent.isdir = 't'\n"
                ")\n"
                "-- Using the cte, fetch a listing of the all the files.\n"
                "--\n"
@@ -81,11 +84,11 @@ libpqProcessFileList(void)
                "-- link's target in general, so if the admin has put any custom\n"
                "-- symbolic links in the data directory, they won't be copied\n"
                "-- correctly)\n"
-               "select path || filename, size, isdir,\n"
-               "       pg_tablespace_location(pg_tablespace.oid) as link_target\n"
-               "from files\n"
-               "left outer join pg_tablespace on files.path = 'pg_tblspc/'\n"
-               "                             and oid::text = files.filename\n";
+               "SELECT path || filename, size, isdir,\n"
+               "       pg_tablespace_location(pg_tablespace.oid) AS link_target\n"
+               "FROM files\n"
+               "LEFT OUTER JOIN pg_tablespace ON files.path = 'pg_tblspc/'\n"
+               "                             AND oid::text = files.filename\n";
        res = PQexec(conn, sql);
 
        if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -235,7 +238,7 @@ libpqGetFile(const char *filename, size_t *filesize)
        const char *paramValues[1];
        paramValues[0] = filename;
 
-       res = PQexecParams(conn, "select pg_read_binary_file($1)",
+       res = PQexecParams(conn, "SELECT pg_read_binary_file($1)",
                                           1, NULL, paramValues, NULL, NULL, 1);
 
        if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -310,7 +313,7 @@ libpq_executeFileMap(filemap_t *map)
         * First create a temporary table, and load it with the blocks that
         * we need to fetch.
         */
-       sql = "create temporary table fetchchunks(path text, begin int4, len int4);";
+       sql = "CREATE TEMPORARY TABLE fetchchunks(path text, begin int4, len int4);";
        res = PQexec(conn, sql);
 
        if (PQresultStatus(res) != PGRES_COMMAND_OK)
@@ -385,9 +388,9 @@ libpq_executeFileMap(filemap_t *map)
        /* Ok, we've sent the file list. Now receive the files */
        sql =
                "-- fetch all the blocks listed in the temp table.\n"
-               "select path, begin, \n"
-               "  pg_read_binary_file(path, begin, len) as chunk\n"
-               "from fetchchunks\n";
+               "SELECT path, begin, \n"
+               "  pg_read_binary_file(path, begin, len) AS chunk\n"
+               "FROM fetchchunks\n";
 
        receiveFileChunks(sql);
 }