1
1
/*
2
2
* pg_reorg.c: bin/pg_reorg.c
3
3
*
4
- * Copyright (c) 2008-2009 , NIPPON TELEGRAPH AND TELEPHONE CORPORATION
4
+ * Copyright (c) 2008-2010 , NIPPON TELEGRAPH AND TELEPHONE CORPORATION
5
5
*/
6
6
7
7
/**
8
8
* @brief Client Modules
9
9
*/
10
10
11
- const char * PROGRAM_VERSION = "1.0.6 " ;
11
+ const char * PROGRAM_VERSION = "1.0.7 " ;
12
12
const char * PROGRAM_URL = "http://reorg.projects.postgresql.org/" ;
13
13
const char * PROGRAM_EMAIL = "reorg-general@lists.pgfoundry.org" ;
14
14
@@ -77,9 +77,10 @@ typedef struct reorg_index
77
77
const char * create_index ; /* CREATE INDEX */
78
78
} reorg_index ;
79
79
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 );
83
84
84
85
static char * getstr (PGresult * res , int row , int col );
85
86
static Oid getoid (PGresult * res , int row , int col );
@@ -92,14 +93,12 @@ static bool sqlstate_equals(PGresult *res, const char *state)
92
93
return strcmp (PQresultErrorField (res , PG_DIAG_SQLSTATE ), state ) == 0 ;
93
94
}
94
95
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 ;
103
102
104
103
/* buffer should have at least 11 bytes */
105
104
static char *
@@ -109,53 +108,31 @@ utoa(unsigned int value, char *buffer)
109
108
return buffer ;
110
109
}
111
110
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 [] =
128
112
{
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
+ };
154
121
155
122
int
156
123
main (int argc , char * argv [])
157
124
{
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 = "" ;
159
136
160
137
if (alldb )
161
138
{
@@ -443,7 +420,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
443
420
* an advisory lock. The registration should be done after
444
421
* the first command is succeeded.
445
422
*/
446
- current_table = table ;
423
+ pgut_atexit_push ( & reorg_cleanup , ( void * ) table ) ;
447
424
448
425
/*
449
426
* 2. Copy tuples into temp table.
@@ -574,13 +551,13 @@ reorg_one_table(const reorg_table *table, const char *orderby)
574
551
command ("SELECT reorg.reorg_drop($1)" , 1 , params );
575
552
command ("COMMIT" , 0 , NULL );
576
553
577
- current_table = NULL ;
554
+ pgut_atexit_pop ( & reorg_cleanup , ( void * ) table ) ;
578
555
free (vxid );
579
556
580
557
/*
581
558
* 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.
584
561
*/
585
562
if (analyze )
586
563
{
@@ -597,22 +574,24 @@ reorg_one_table(const reorg_table *table, const char *orderby)
597
574
termStringInfo (& sql );
598
575
}
599
576
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 )
602
583
{
584
+ const reorg_table * table = (const reorg_table * ) userdata ;
585
+
603
586
if (fatal )
604
587
{
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" );
607
589
}
608
590
else
609
591
{
610
592
char buffer [12 ];
611
593
const char * params [1 ];
612
594
613
- if (current_table == NULL )
614
- return ; /* no needs to cleanup */
615
-
616
595
/* Rollback current transaction */
617
596
if (connection )
618
597
command ("ROLLBACK" , 0 , NULL );
@@ -622,25 +601,26 @@ pgut_cleanup(bool fatal)
622
601
reconnect ();
623
602
624
603
/* do cleanup */
625
- params [0 ] = utoa (current_table -> target_oid , buffer );
604
+ params [0 ] = utoa (table -> target_oid , buffer );
626
605
command ("SELECT reorg.reorg_drop($1)" , 1 , params );
627
- current_table = NULL ;
628
606
}
629
607
}
630
608
631
609
void
632
- pgut_help (void )
610
+ pgut_help (bool details )
633
611
{
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" );
646
626
}
0 commit comments