]> granicus.if.org Git - postgresql/commitdiff
Add pgbench option to add foreign key constraints to the standard scenario.
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 19 Jun 2012 22:33:59 +0000 (18:33 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 19 Jun 2012 22:33:59 +0000 (18:33 -0400)
The option --foreign-keys, used at initialization time, will create foreign
key constraints for the columns that represent references to other tables'
primary keys.  This can help in benchmarking FK performance.

Jeff Janes

contrib/pgbench/pgbench.c
doc/src/sgml/pgbench.sgml

index f2fdc6c56f67acb9f56a72eb23d08349fd301cd3..f6cd4aad159a9add1a758ddf15b3cb5bd1d8e722 100644 (file)
@@ -119,6 +119,11 @@ int                        scale = 1;
  */
 int                    fillfactor = 100;
 
+/*
+ * create foreign key constraints on the tables?
+ */
+int                    foreign_keys = 0;
+
 /*
  * use unlogged tables?
  */
@@ -343,6 +348,8 @@ usage(const char *progname)
                   "  -i           invokes initialization mode\n"
                   "  -F NUM       fill factor\n"
                   "  -s NUM       scaling factor\n"
+                  "  --foreign-keys\n"
+                  "               create foreign key constraints between tables\n"
                   "  --index-tablespace=TABLESPACE\n"
                   "               create indexes in the specified tablespace\n"
                   "  --tablespace=TABLESPACE\n"
@@ -1275,9 +1282,9 @@ init(void)
        };
        struct ddlinfo DDLs[] = {
                {
-                       "pgbench_branches",
-                       "bid int not null,bbalance int,filler char(88)",
-                       1
+                       "pgbench_history",
+                       "tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)",
+                       0
                },
                {
                        "pgbench_tellers",
@@ -1290,9 +1297,9 @@ init(void)
                        1
                },
                {
-                       "pgbench_history",
-                       "tid int,bid int,aid int,delta int,mtime timestamp,filler char(22)",
-                       0
+                       "pgbench_branches",
+                       "bid int not null,bbalance int,filler char(88)",
+                       1
                }
        };
        static char *DDLAFTERs[] = {
@@ -1300,6 +1307,13 @@ init(void)
                "alter table pgbench_tellers add primary key (tid)",
                "alter table pgbench_accounts add primary key (aid)"
        };
+       static char *DDLKEYs[] = {
+               "alter table pgbench_tellers add foreign key (bid) references pgbench_branches",
+               "alter table pgbench_accounts add foreign key (bid) references pgbench_branches",
+               "alter table pgbench_history add foreign key (bid) references pgbench_branches",
+               "alter table pgbench_history add foreign key (tid) references pgbench_tellers",
+               "alter table pgbench_history add foreign key (aid) references pgbench_accounts"
+       };
 
        PGconn     *con;
        PGresult   *res;
@@ -1403,7 +1417,7 @@ init(void)
        /*
         * create indexes
         */
-       fprintf(stderr, "set primary key...\n");
+       fprintf(stderr, "set primary keys...\n");
        for (i = 0; i < lengthof(DDLAFTERs); i++)
        {
                char            buffer[256];
@@ -1424,6 +1438,18 @@ init(void)
                executeStatement(con, buffer);
        }
 
+       /*
+        * create foreign keys
+        */
+       if (foreign_keys)
+       {
+               fprintf(stderr, "set foreign keys...\n");
+               for (i = 0; i < lengthof(DDLKEYs); i++)
+               {
+                       executeStatement(con, DDLKEYs[i]);
+               }
+       }
+
        /* vacuum */
        fprintf(stderr, "vacuum...");
        executeStatement(con, "vacuum analyze pgbench_branches");
@@ -1864,6 +1890,7 @@ main(int argc, char **argv)
        int                     i;
 
        static struct option long_options[] = {
+               {"foreign-keys", no_argument, &foreign_keys, 1},
                {"index-tablespace", required_argument, NULL, 3},
                {"tablespace", required_argument, NULL, 2},
                {"unlogged-tables", no_argument, &unlogged_tables, 1},
index 4a80b46416d4e610b6fbc6d174731f99b85653bb..5ce66a6ac04a52b09a6a5fe7d1dde24cbd0d5bfa 100644 (file)
@@ -180,6 +180,15 @@ pgbench <optional> <replaceable>options</> </optional> <replaceable>dbname</>
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <term><option>--foreign-keys</option></term>
+      <listitem>
+       <para>
+        Create foreign key constraints between the standard tables.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term><option>--index-tablespace=<replaceable>index_tablespace</replaceable></option></term>
       <listitem>