Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e7dfda0

Browse files
committed
no-ddl option to pgbench
1 parent 285ec6d commit e7dfda0

File tree

1 file changed

+71
-46
lines changed

1 file changed

+71
-46
lines changed

src/bin/pgbench/pgbench.c

Lines changed: 71 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ int fillfactor = 100;
116116
*/
117117
int foreign_keys = 0;
118118

119+
/*
120+
* Don't create tables and indexes
121+
*/
122+
int no_ddl = 0;
123+
124+
119125
/*
120126
* use unlogged tables?
121127
*/
@@ -479,6 +485,7 @@ usage(void)
479485
" -n, --no-vacuum do not run VACUUM after initialization\n"
480486
" -q, --quiet quiet logging (one message each 5 seconds)\n"
481487
" -s, --scale=NUM scaling factor\n"
488+
" --no-ddl don't create tables and indexes"
482489
" --foreign-keys create foreign key constraints between tables\n"
483490
" --index-tablespace=TABLESPACE\n"
484491
" create indexes in the specified tablespace\n"
@@ -2648,40 +2655,55 @@ init(bool is_no_vacuum)
26482655
if ((con = doConnect()) == NULL)
26492656
exit(1);
26502657

2651-
for (i = 0; i < lengthof(DDLs); i++)
2658+
if (!no_ddl)
26522659
{
2653-
char opts[256];
2654-
char buffer[256];
2655-
const struct ddlinfo *ddl = &DDLs[i];
2656-
const char *cols;
2657-
2658-
/* Remove old table, if it exists. */
2659-
snprintf(buffer, sizeof(buffer), "drop table if exists %s", ddl->table);
2660-
executeStatement(con, buffer);
2661-
2662-
/* Construct new create table statement. */
2663-
opts[0] = '\0';
2664-
if (ddl->declare_fillfactor)
2665-
snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts),
2666-
" with (fillfactor=%d)", fillfactor);
2667-
if (tablespace != NULL)
2660+
for (i = 0; i < lengthof(DDLs); i++)
26682661
{
2669-
char *escape_tablespace;
2662+
char opts[256];
2663+
char buffer[256];
2664+
const struct ddlinfo *ddl = &DDLs[i];
2665+
const char *cols;
2666+
2667+
/* Remove old table, if it exists. */
2668+
snprintf(buffer, sizeof(buffer), "drop table if exists %s", ddl->table);
2669+
executeStatement(con, buffer);
2670+
2671+
/* Construct new create table statement. */
2672+
opts[0] = '\0';
2673+
if (ddl->declare_fillfactor)
2674+
snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts),
2675+
" with (fillfactor=%d)", fillfactor);
2676+
if (tablespace != NULL)
2677+
{
2678+
char *escape_tablespace;
26702679

2671-
escape_tablespace = PQescapeIdentifier(con, tablespace,
2672-
strlen(tablespace));
2673-
snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts),
2674-
" tablespace %s", escape_tablespace);
2675-
PQfreemem(escape_tablespace);
2676-
}
2680+
escape_tablespace = PQescapeIdentifier(con, tablespace,
2681+
strlen(tablespace));
2682+
snprintf(opts + strlen(opts), sizeof(opts) - strlen(opts),
2683+
" tablespace %s", escape_tablespace);
2684+
PQfreemem(escape_tablespace);
2685+
}
2686+
2687+
cols = (scale >= SCALE_32BIT_THRESHOLD) ? ddl->bigcols : ddl->smcols;
26772688

2678-
cols = (scale >= SCALE_32BIT_THRESHOLD) ? ddl->bigcols : ddl->smcols;
2689+
snprintf(buffer, sizeof(buffer), "create%s table %s(%s)%s",
2690+
unlogged_tables ? " unlogged" : "",
2691+
ddl->table, cols, opts);
26792692

2680-
snprintf(buffer, sizeof(buffer), "create%s table %s(%s)%s",
2681-
unlogged_tables ? " unlogged" : "",
2682-
ddl->table, cols, opts);
2693+
executeStatement(con, buffer);
2694+
}
2695+
}
2696+
else
2697+
{
2698+
fprintf(stderr, "erasing tables...\n");
2699+
for (i = 0; i < lengthof(DDLs); i++)
2700+
{
2701+
char buffer[256];
2702+
const struct ddlinfo *ddl = &DDLs[i];
26832703

2684-
executeStatement(con, buffer);
2704+
snprintf(buffer, sizeof(buffer), "delete from %s;", ddl->table);
2705+
executeStatement(con, buffer);
2706+
}
26852707
}
26862708

26872709
executeStatement(con, "begin");
@@ -2709,10 +2731,9 @@ init(bool is_no_vacuum)
27092731
/*
27102732
* fill the pgbench_accounts table with some data
27112733
*/
2712-
fprintf(stderr, "creating tables...\n");
2734+
fprintf(stderr, "inserting data into pgbench_accounts...\n");
27132735

27142736
executeStatement(con, "begin");
2715-
executeStatement(con, "truncate pgbench_accounts");
27162737

27172738
res = PQexec(con, "copy pgbench_accounts from stdin");
27182739
if (PQresultStatus(res) != PGRES_COPY_IN)
@@ -2802,31 +2823,34 @@ init(bool is_no_vacuum)
28022823
/*
28032824
* create indexes
28042825
*/
2805-
fprintf(stderr, "set primary keys...\n");
2806-
for (i = 0; i < lengthof(DDLINDEXes); i++)
2826+
if (!no_ddl)
28072827
{
2808-
char buffer[256];
2828+
fprintf(stderr, "set primary keys...\n");
2829+
for (i = 0; i < lengthof(DDLINDEXes); i++)
2830+
{
2831+
char buffer[256];
28092832

2810-
strlcpy(buffer, DDLINDEXes[i], sizeof(buffer));
2833+
strlcpy(buffer, DDLINDEXes[i], sizeof(buffer));
28112834

2812-
if (index_tablespace != NULL)
2813-
{
2814-
char *escape_tablespace;
2835+
if (index_tablespace != NULL)
2836+
{
2837+
char *escape_tablespace;
28152838

2816-
escape_tablespace = PQescapeIdentifier(con, index_tablespace,
2817-
strlen(index_tablespace));
2818-
snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
2819-
" using index tablespace %s", escape_tablespace);
2820-
PQfreemem(escape_tablespace);
2821-
}
2839+
escape_tablespace = PQescapeIdentifier(con, index_tablespace,
2840+
strlen(index_tablespace));
2841+
snprintf(buffer + strlen(buffer), sizeof(buffer) - strlen(buffer),
2842+
" using index tablespace %s", escape_tablespace);
2843+
PQfreemem(escape_tablespace);
2844+
}
28222845

2823-
executeStatement(con, buffer);
2846+
executeStatement(con, buffer);
2847+
}
28242848
}
28252849

28262850
/*
28272851
* create foreign keys
28282852
*/
2829-
if (foreign_keys)
2853+
if (foreign_keys && !no_ddl)
28302854
{
28312855
fprintf(stderr, "set foreign keys...\n");
28322856
for (i = 0; i < lengthof(DDLKEYs); i++)
@@ -3630,6 +3654,7 @@ main(int argc, char **argv)
36303654
{"vacuum-all", no_argument, NULL, 'v'},
36313655
/* long-named only options */
36323656
{"foreign-keys", no_argument, &foreign_keys, 1},
3657+
{"no-ddl", no_argument, &no_ddl, 1},
36333658
{"index-tablespace", required_argument, NULL, 3},
36343659
{"tablespace", required_argument, NULL, 2},
36353660
{"unlogged-tables", no_argument, &unlogged_tables, 1},
@@ -3940,7 +3965,7 @@ main(int argc, char **argv)
39403965
break;
39413966
case 0:
39423967
/* This covers long options which take no argument. */
3943-
if (foreign_keys || unlogged_tables)
3968+
if (foreign_keys || unlogged_tables || no_ddl)
39443969
initialization_option_set = true;
39453970
break;
39463971
case 2: /* tablespace */

0 commit comments

Comments
 (0)