]> granicus.if.org Git - postgis/commitdiff
Jeff Adams patch to support tablespaces (#67)
authorPaul Ramsey <pramsey@cleverelephant.ca>
Wed, 16 Mar 2011 13:02:43 +0000 (13:02 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Wed, 16 Mar 2011 13:02:43 +0000 (13:02 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@6905 b70326c6-7e19-0410-871a-916f4a2858ee

loader/shp2pgsql-cli.c
loader/shp2pgsql-core.c
loader/shp2pgsql-core.h

index 7174fa1dc8b1edf6e8e85ebf1bfcc22af7787dd0..a61078dc1ace44284b0c594b4329f8d4f0727abb 100644 (file)
@@ -43,6 +43,12 @@ usage()
                  "      attribute column. (default: \"UTF-8\")\n" ));
        printf(_( "  -N <policy> NULL geometries handling policy (insert*,skip,abort).\n" ));
        printf(_( "  -n  Only import DBF file.\n" ));
+       printf(_( "  -T <tablespace> Specify the tablespace for the new table.\n" 
+                  "      Note that indexes will still use the default tablespace unless the"
+                  "      -X flag is also used."));
+       printf(_( "  -X <tablespace> Specify the tablespace for the table's indexes.\n"
+                  "      This applies to the primary key, and the spatial index if"
+                  "      the -I flag is used." ));
        printf(_( "  -?  Display this help screen.\n" ));
 }
 
@@ -73,7 +79,7 @@ main (int argc, char **argv)
        config = malloc(sizeof(SHPLOADERCONFIG));
        set_config_defaults(config);
 
-       while ((c = pgis_getopt(argc, argv, "kcdapGDs:Sg:iW:wIN:n")) != EOF)
+       while ((c = pgis_getopt(argc, argv, "kcdapGDs:Sg:iW:wIN:nT:X:")) != EOF)
        {
                switch (c)
                {
@@ -155,6 +161,14 @@ main (int argc, char **argv)
                        }
                        break;
 
+               case 'T':
+                       config->tablespace = pgis_optarg;
+                       break;
+
+               case 'X':
+                       config->idxtablespace = pgis_optarg;
+                       break;
+
                case '?':
                        usage();
                        exit(0);
index 48043b1e29efdfc166fe819155f76b9eb85c5fff..957ef89ee83bade087f25c72e60680f0d014bc5f 100644 (file)
@@ -797,6 +797,8 @@ set_config_defaults(SHPLOADERCONFIG *config)
        config->null_policy = POLICY_NULL_INSERT;
        config->sr_id = -1;
        config->hwgeom = 0;
+       config->tablespace = NULL;
+       config->idxtablespace = NULL;
 }
 
 /* Create a new shapefile state object */
@@ -1249,12 +1251,12 @@ ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
                */
                if (state->config->schema)
                {
-                       stringbuffer_aprintf(sb, "CREATE TABLE \"%s\".\"%s\" (gid serial PRIMARY KEY",
+                       stringbuffer_aprintf(sb, "CREATE TABLE \"%s\".\"%s\" (gid serial",
                                             state->config->schema, state->config->table);
                }
                else
                {
-                       stringbuffer_aprintf(sb, "CREATE TABLE \"%s\" (gid serial PRIMARY KEY", state->config->table);
+                       stringbuffer_aprintf(sb, "CREATE TABLE \"%s\" (gid serial", state->config->table);
                }
 
                /* Generate the field types based upon the shapefile information */
@@ -1334,7 +1336,47 @@ ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
                        stringbuffer_aprintf(sb, ",\n\"%s\" geography(%s%s,%d)", state->config->geom, state->pgtype, dimschar, 4326);
                }
 
-               stringbuffer_aprintf(sb, ");\n");
+               stringbuffer_aprintf(sb, ")");
+               /* Tablespace is optional. */
+               if (state->config->tablespace != NULL)
+               {
+                       stringbuffer_aprintf(sb, " TABLESPACE \"%s\"", state->config->tablespace);
+               }
+               stringbuffer_aprintf(sb, ";\n");
+
+               /* Create the primary key.  This is done separately because the index for the PK needs
+                 * to be in the correct tablespace. */
+
+               /* TODO: Currently PostgreSQL does not allow specifying an index to use for a PK (so you get
+                 *       a default one called table_pkey) and it does not provide a way to create a PK index
+                 *       in a specific tablespace.  So as a hacky solution we create the PK, then move the
+                 *       index to the correct tablespace.  Eventually this should be:
+                *           CREATE INDEX table_pkey on table(gid) TABLESPACE tblspc;
+                 *           ALTER TABLE table ADD PRIMARY KEY (gid) USING INDEX table_pkey;
+                *       A patch has apparently been submitted to PostgreSQL to enable this syntax, see this thread:
+                *           http://archives.postgresql.org/pgsql-hackers/2011-01/msg01405.php */
+               stringbuffer_aprintf(sb, "ALTER TABLE ");
+               /* Schema is optional, include if present. */
+               if (state->config->schema)
+               {
+                       stringbuffer_aprintf(sb, "\"%s\".",state->config->schema);
+               }
+               stringbuffer_aprintf(sb, "\"%s\" ADD PRIMARY KEY (gid);\n", state->config->table);
+               /* Tablespace is optional for the index. */
+               if (state->config->idxtablespace != NULL)
+               {
+                       stringbuffer_aprintf(sb, "ALTER INDEX ");
+                       if (state->config->schema)
+                       {
+                               stringbuffer_aprintf(sb, "\"%s\".",state->config->schema);
+                       }
+                       /* WARNING: We're assuming the default "table_pkey" name for the primary
+                        *          key index.  PostgreSQL may use "table_pkey1" or similar in the
+                        *          case of a name conflict, so you may need to edit the produced
+                        *          SQL in this rare case. */
+                       stringbuffer_aprintf(sb, "\"%s_pkey\" SET TABLESPACE \"%s\";\n",
+                                               state->config->table, state->config->idxtablespace);
+               }
 
                /* Create the geometry column with an addgeometry call */
                if (state->config->readshape == 1 && (!state->config->geography))
@@ -1754,15 +1796,19 @@ ShpLoaderGetSQLFooter(SHPLOADERSTATE *state, char **strfooter)
        /* Create gist index if specified and not in "prepare" mode */
        if (state->config->createindex)
        {
+               stringbuffer_aprintf(sb, "CREATE INDEX \"%s_%s_gist\" ON ", state->config->table, state->config->geom);
+               /* Schema is optional, include if present. */
                if (state->config->schema)
                {
-                       stringbuffer_aprintf(sb, "CREATE INDEX \"%s_%s_gist\" ON \"%s\".\"%s\" using gist (\"%s\" %s);\n", state->config->table, state->config->geom,
-                                            state->config->schema, state->config->table, state->config->geom, ops);
+                       stringbuffer_aprintf(sb, "\"%s\".",state->config->schema);
                }
-               else
+               stringbuffer_aprintf(sb, "\"%s\" USING GIST (\"%s\" %s)", state->config->table, state->config->geom, ops);
+               /* Tablespace is also optional. */
+               if (state->config->idxtablespace != NULL)
                {
-                       stringbuffer_aprintf(sb, "CREATE INDEX \"%s_%s_gist\" ON \"%s\" using gist (\"%s\" %s);\n", state->config->table, state->config->geom, state->config->table, state->config->geom, ops);
+                       stringbuffer_aprintf(sb, " TABLESPACE \"%s\"", state->config->idxtablespace);
                }
+               stringbuffer_aprintf(sb, ";\n");
        }
 
        /* End the transaction */
index 849b7916b7c529d175de13fbe005412db9074a6f..8c93ebacc33fb84616cdf91e68ec09b33b17e5cb 100644 (file)
@@ -123,6 +123,12 @@ typedef struct shp_loader_config
        /* iconv encoding name */
        char *encoding;
 
+       /* tablespace name for the table */
+       char *tablespace;
+
+       /* tablespace name for the indexes */
+       char *idxtablespace;
+
        /* how to handle nulls */
        int null_policy;