Skip to content

Commit 7084ec6

Browse files
committed
version 1.0.7.
1 parent 038c075 commit 7084ec6

18 files changed

+1633
-330
lines changed

COPYRIGHT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
1+
Copyright (c) 2008-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
22
All rights reserved.
33

44
Redistribution and use in source and binary forms, with or without

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# pg_reorg: Makefile
33
#
4-
# Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
4+
# Copyright (c) 2008-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
55
#
66
ifdef USE_PGXS
77
PG_CONFIG = pg_config

bin/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#
22
# pg_reorg: bin/Makefile
33
#
4-
# Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
4+
# Copyright (c) 2008-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
55
#
66
SRCS = pg_reorg.c pgut/pgut.c
77
OBJS = $(SRCS:.c=.o)

bin/pg_reorg.c

Lines changed: 61 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/*
22
* pg_reorg.c: bin/pg_reorg.c
33
*
4-
* Copyright (c) 2008-2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
4+
* Copyright (c) 2008-2010, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
55
*/
66

77
/**
88
* @brief Client Modules
99
*/
1010

11-
const char *PROGRAM_VERSION = "1.0.6";
11+
const char *PROGRAM_VERSION = "1.0.7";
1212
const char *PROGRAM_URL = "http://reorg.projects.postgresql.org/";
1313
const char *PROGRAM_EMAIL = "reorg-general@lists.pgfoundry.org";
1414

@@ -77,9 +77,10 @@ typedef struct reorg_index
7777
const char *create_index; /* CREATE INDEX */
7878
} reorg_index;
7979

80-
static void reorg_all_databases(const char *orderby);
81-
static bool reorg_one_database(const char *orderby, const char *table);
82-
static void reorg_one_table(const reorg_table *table, const char *orderby);
80+
static void reorg_all_databases(const char *order_by);
81+
static bool reorg_one_database(const char *order_by, const char *table);
82+
static void reorg_one_table(const reorg_table *table, const char *order_by);
83+
static void reorg_cleanup(bool fatal, void *userdata);
8384

8485
static char *getstr(PGresult *res, int row, int col);
8586
static Oid getoid(PGresult *res, int row, int col);
@@ -92,14 +93,12 @@ static bool sqlstate_equals(PGresult *res, const char *state)
9293
return strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), state) == 0;
9394
}
9495

95-
static bool verbose = false;
96-
static bool analyze = true;
97-
98-
/*
99-
* The table begin re-organized. If not null, we need to cleanup temp
100-
* objects before the program exits.
101-
*/
102-
static const reorg_table *current_table = NULL;
96+
static bool verbose = false;
97+
static bool analyze = true;
98+
static bool alldb = false;
99+
static bool noorder = false;
100+
static char *table = NULL;
101+
static char *orderby = NULL;
103102

104103
/* buffer should have at least 11 bytes */
105104
static char *
@@ -109,53 +108,31 @@ utoa(unsigned int value, char *buffer)
109108
return buffer;
110109
}
111110

112-
const struct option pgut_options[] = {
113-
{"verbose", no_argument, NULL, 'v'},
114-
{"all", no_argument, NULL, 'a'},
115-
{"table", required_argument, NULL, 't'},
116-
{"no-order", no_argument, NULL, 'n'},
117-
{"order-by", required_argument, NULL, 'o'},
118-
{"no-analyze", no_argument, NULL, 'Z'},
119-
{NULL, 0, NULL, 0}
120-
};
121-
122-
bool alldb = false;
123-
const char *table = NULL;
124-
const char *orderby = NULL;
125-
126-
bool
127-
pgut_argument(int c, const char *arg)
111+
static pgut_option options[] =
128112
{
129-
switch (c)
130-
{
131-
case 'v':
132-
verbose = true;
133-
break;
134-
case 'a':
135-
alldb = true;
136-
break;
137-
case 't':
138-
assign_option(&table, c, arg);
139-
break;
140-
case 'n':
141-
assign_option(&orderby, c, "");
142-
break;
143-
case 'o':
144-
assign_option(&orderby, c, arg);
145-
break;
146-
case 'Z':
147-
analyze = false;
148-
break;
149-
default:
150-
return false;
151-
}
152-
return true;
153-
}
113+
{ 'b', 'v', "verbose", &verbose },
114+
{ 'b', 'a', "all", &alldb },
115+
{ 's', 't', "table", &table },
116+
{ 'b', 'n', "no-order", &noorder },
117+
{ 's', 'o', "order-by", &orderby },
118+
{ 'B', 'Z', "no-analyze", &analyze },
119+
{ 0 },
120+
};
154121

155122
int
156123
main(int argc, char *argv[])
157124
{
158-
parse_options(argc, argv);
125+
int i;
126+
127+
i = pgut_getopt(argc, argv, options);
128+
129+
if (i == argc - 1)
130+
dbname = argv[i];
131+
else if (i < argc)
132+
elog(ERROR_ARGS, "too many arguments");
133+
134+
if (noorder)
135+
orderby = "";
159136

160137
if (alldb)
161138
{
@@ -443,7 +420,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
443420
* an advisory lock. The registration should be done after
444421
* the first command is succeeded.
445422
*/
446-
current_table = table;
423+
pgut_atexit_push(&reorg_cleanup, (void *) table);
447424

448425
/*
449426
* 2. Copy tuples into temp table.
@@ -574,13 +551,13 @@ reorg_one_table(const reorg_table *table, const char *orderby)
574551
command("SELECT reorg.reorg_drop($1)", 1, params);
575552
command("COMMIT", 0, NULL);
576553

577-
current_table = NULL;
554+
pgut_atexit_pop(&reorg_cleanup, (void *) table);
578555
free(vxid);
579556

580557
/*
581558
* 7. Analyze.
582-
* Note that current_table is already set to NULL here because analyze
583-
* is an unimportant operation; No clean up even if failed.
559+
* Note that cleanup hook has been already uninstalled here because analyze
560+
* is not an important operation; No clean up even if failed.
584561
*/
585562
if (analyze)
586563
{
@@ -597,22 +574,24 @@ reorg_one_table(const reorg_table *table, const char *orderby)
597574
termStringInfo(&sql);
598575
}
599576

600-
void
601-
pgut_cleanup(bool fatal)
577+
/*
578+
* The userdata pointing a table being re-organized. We need to cleanup temp
579+
* objects before the program exits.
580+
*/
581+
static void
582+
reorg_cleanup(bool fatal, void *userdata)
602583
{
584+
const reorg_table *table = (const reorg_table *) userdata;
585+
603586
if (fatal)
604587
{
605-
if (current_table)
606-
fprintf(stderr, "!!!FATAL ERROR!!! Please refer to a manual.\n\n");
588+
fprintf(stderr, "!!!FATAL ERROR!!! Please refer to a manual.\n\n");
607589
}
608590
else
609591
{
610592
char buffer[12];
611593
const char *params[1];
612594

613-
if (current_table == NULL)
614-
return; /* no needs to cleanup */
615-
616595
/* Rollback current transaction */
617596
if (connection)
618597
command("ROLLBACK", 0, NULL);
@@ -622,25 +601,26 @@ pgut_cleanup(bool fatal)
622601
reconnect();
623602

624603
/* do cleanup */
625-
params[0] = utoa(current_table->target_oid, buffer);
604+
params[0] = utoa(table->target_oid, buffer);
626605
command("SELECT reorg.reorg_drop($1)", 1, params);
627-
current_table = NULL;
628606
}
629607
}
630608

631609
void
632-
pgut_help(void)
610+
pgut_help(bool details)
633611
{
634-
fprintf(stderr,
635-
"%s re-organizes a PostgreSQL database.\n\n"
636-
"Usage:\n"
637-
" %s [OPTION]... [DBNAME]\n"
638-
"\nOptions:\n"
639-
" -a, --all reorg all databases\n"
640-
" -t, --table=TABLE reorg specific table only\n"
641-
" -n, --no-order do vacuum full instead of cluster\n"
642-
" -o, --order-by=columns order by columns instead of cluster keys\n"
643-
" -Z, --no-analyze don't analyze at end\n"
644-
" -v, --verbose display detailed information during processing\n",
645-
PROGRAM_NAME, PROGRAM_NAME);
612+
printf("%s re-organizes a PostgreSQL database.\n\n", PROGRAM_NAME);
613+
printf("Usage:\n");
614+
printf(" %s [OPTION]... [DBNAME]\n", PROGRAM_NAME);
615+
616+
if (!details)
617+
return;
618+
619+
printf("Options:\n");
620+
printf(" -a, --all reorg all databases\n");
621+
printf(" -t, --table=TABLE reorg specific table only\n");
622+
printf(" -n, --no-order do vacuum full instead of cluster\n");
623+
printf(" -o, --order-by=columns order by columns instead of cluster keys\n");
624+
printf(" -Z, --no-analyze don't analyze at end\n");
625+
printf(" -v, --verbose display detailed information during processing\n");
646626
}

0 commit comments

Comments
 (0)