8
8
* @brief Client Modules
9
9
*/
10
10
11
- #define PROGRAM_VERSION "1.0.4"
12
- #define PROGRAM_URL "http://reorg.projects.postgresql.org/"
13
- #define PROGRAM_EMAIL "reorg-general@lists.pgfoundry.org"
11
+ const char * PROGRAM_VERSION = "1.0.4" ;
12
+ const char * PROGRAM_URL = "http://reorg.projects.postgresql.org/" ;
13
+ const char * PROGRAM_EMAIL = "reorg-general@lists.pgfoundry.org" ;
14
14
15
15
#include "pgut/pgut.h"
16
- #include "pqexpbuffer.h"
17
16
18
17
#include <string.h>
19
18
#include <stdlib.h>
20
19
#include <unistd.h>
21
20
22
- #define EXITCODE_HELP 2
23
21
#define APPLY_COUNT 1000
24
22
25
23
#define SQL_XID_SNAPSHOT_80300 \
37
35
" AND pid <> pg_backend_pid() AND transactionid = ANY($1) LIMIT 1"
38
36
39
37
#define SQL_XID_SNAPSHOT \
40
- (PQserverVersion(current_conn ) >= 80300 \
38
+ (PQserverVersion(connection ) >= 80300 \
41
39
? SQL_XID_SNAPSHOT_80300 \
42
40
: SQL_XID_SNAPSHOT_80200)
43
41
44
42
#define SQL_XID_ALIVE \
45
- (PQserverVersion(current_conn ) >= 80300 \
43
+ (PQserverVersion(connection ) >= 80300 \
46
44
? SQL_XID_ALIVE_80300 \
47
45
: SQL_XID_ALIVE_80200)
48
46
@@ -80,7 +78,7 @@ typedef struct reorg_index
80
78
} reorg_index ;
81
79
82
80
static void reorg_all_databases (const char * orderby );
83
- static pqbool reorg_one_database (const char * orderby , const char * table );
81
+ static bool reorg_one_database (const char * orderby , const char * table );
84
82
static void reorg_one_table (const reorg_table * table , const char * orderby );
85
83
86
84
static char * getstr (PGresult * res , int row , int col );
@@ -89,14 +87,14 @@ static Oid getoid(PGresult *res, int row, int col);
89
87
#define SQLSTATE_INVALID_SCHEMA_NAME "3F000"
90
88
#define SQLSTATE_LOCK_NOT_AVAILABLE "55P03"
91
89
92
- static pqbool sqlstate_equals (PGresult * res , const char * state )
90
+ static bool sqlstate_equals (PGresult * res , const char * state )
93
91
{
94
92
return strcmp (PQresultErrorField (res , PG_DIAG_SQLSTATE ), state ) == 0 ;
95
93
}
96
94
97
- static pqbool echo = false;
98
- static pqbool verbose = false;
99
- static pqbool quiet = false;
95
+ static bool echo = false;
96
+ static bool verbose = false;
97
+ static bool quiet = false;
100
98
101
99
/*
102
100
* The table begin re-organized. If not null, we need to cleanup temp
@@ -125,11 +123,11 @@ const struct option pgut_longopts[] = {
125
123
{NULL , 0 , NULL , 0 }
126
124
};
127
125
128
- pqbool alldb = false;
126
+ bool alldb = false;
129
127
const char * table = NULL ;
130
128
const char * orderby = NULL ;
131
129
132
- pqbool
130
+ bool
133
131
pgut_argument (int c , const char * arg )
134
132
{
135
133
switch (c )
@@ -147,13 +145,13 @@ pgut_argument(int c, const char *arg)
147
145
alldb = true;
148
146
break ;
149
147
case 't' :
150
- table = arg ;
148
+ assign_option ( & table , c , arg ) ;
151
149
break ;
152
150
case 'n' :
153
- orderby = "" ;
151
+ assign_option ( & orderby , c , "" ) ;
154
152
break ;
155
153
case 'o' :
156
- orderby = arg ;
154
+ assign_option ( & orderby , c , arg ) ;
157
155
break ;
158
156
default :
159
157
return false;
@@ -164,30 +162,18 @@ pgut_argument(int c, const char *arg)
164
162
int
165
163
main (int argc , char * argv [])
166
164
{
167
- int exitcode ;
168
-
169
- exitcode = pgut_getopt (argc , argv );
170
- if (exitcode )
171
- return exitcode ;
165
+ parse_options (argc , argv );
172
166
173
167
if (alldb )
174
168
{
175
169
if (table )
176
- {
177
- fprintf (stderr , "%s: cannot reorg a specific table in all databases\n" ,
178
- progname );
179
- exit (1 );
180
- }
181
-
170
+ elog (ERROR , "cannot reorg a specific table in all databases" );
182
171
reorg_all_databases (orderby );
183
172
}
184
173
else
185
174
{
186
175
if (!reorg_one_database (orderby , table ))
187
- {
188
- fprintf (stderr , "ERROR: %s is not installed\n" , progname );
189
- return 1 ;
190
- }
176
+ elog (ERROR , "%s is not installed" , PROGRAM_NAME );
191
177
}
192
178
193
179
return 0 ;
@@ -209,13 +195,13 @@ reorg_all_databases(const char *orderby)
209
195
210
196
for (i = 0 ; i < PQntuples (result ); i ++ )
211
197
{
212
- pqbool ret ;
198
+ bool ret ;
213
199
214
200
dbname = PQgetvalue (result , i , 0 );
215
201
216
202
if (!quiet )
217
203
{
218
- printf ("%s: reorg database \"%s\"" , progname , dbname );
204
+ printf ("%s: reorg database \"%s\"" , PROGRAM_NAME , dbname );
219
205
fflush (stdout );
220
206
}
221
207
@@ -256,10 +242,10 @@ getoid(PGresult *res, int row, int col)
256
242
/*
257
243
* Call reorg_one_table for the target table or each table in a database.
258
244
*/
259
- static pqbool
245
+ static bool
260
246
reorg_one_database (const char * orderby , const char * table )
261
247
{
262
- pqbool ret = true;
248
+ bool ret = true;
263
249
PGresult * res ;
264
250
int i ;
265
251
int num ;
@@ -301,7 +287,7 @@ reorg_one_database(const char *orderby, const char *table)
301
287
else
302
288
{
303
289
/* exit otherwise */
304
- printf ("%s" , PQerrorMessage (current_conn ));
290
+ printf ("%s" , PQerrorMessage (connection ));
305
291
PQclear (res );
306
292
exit (1 );
307
293
}
@@ -324,10 +310,7 @@ reorg_one_database(const char *orderby, const char *table)
324
310
table .ckid = getoid (res , i , c ++ );
325
311
326
312
if (table .pkid == 0 )
327
- {
328
- fprintf (stderr , "ERROR: relation \"%s\" has no primary key\n" , table .target_name );
329
- exit (1 );
330
- }
313
+ elog (ERROR , "relation \"%s\" has no primary key" , table .target_name );
331
314
332
315
table .create_pktype = getstr (res , i , c ++ );
333
316
table .create_log = getstr (res , i , c ++ );
@@ -343,10 +326,7 @@ reorg_one_database(const char *orderby, const char *table)
343
326
{
344
327
/* CLUSTER mode */
345
328
if (ckey == NULL )
346
- {
347
- fprintf (stderr , "ERROR: relation \"%s\" has no cluster key\n" , table .target_name );
348
- exit (1 );
349
- }
329
+ elog (ERROR , "relation \"%s\" has no cluster key" , table .target_name );
350
330
appendPQExpBuffer (& sql , "%s ORDER BY %s" , create_table , ckey );
351
331
table .create_table = sql .data ;
352
332
}
@@ -455,11 +435,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
455
435
" WHERE tgrelid = $1 AND tgname >= 'z_reorg_trigger' LIMIT 1" ,
456
436
1 , params );
457
437
if (PQntuples (res ) > 0 )
458
- {
459
- fprintf (stderr , "%s: trigger conflicted for %s\n" ,
460
- progname , table -> target_name );
461
- exit (1 );
462
- }
438
+ elog (ERROR , "trigger conflicted for %s" , table -> target_name );
463
439
464
440
command (table -> create_pktype , 0 , NULL );
465
441
command (table -> create_log , 0 , NULL );
@@ -482,7 +458,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
482
458
command ("BEGIN ISOLATION LEVEL SERIALIZABLE" , 0 , NULL );
483
459
/* SET work_mem = maintenance_work_mem */
484
460
command ("SELECT set_config('work_mem', current_setting('maintenance_work_mem'), true)" , 0 , NULL );
485
- if (PQserverVersion (current_conn ) >= 80300 && orderby && !orderby [0 ])
461
+ if (PQserverVersion (connection ) >= 80300 && orderby && !orderby [0 ])
486
462
command ("SET LOCAL synchronize_seqscans = off" , 0 , NULL );
487
463
res = execute (SQL_XID_SNAPSHOT , 0 , NULL );
488
464
vxid = strdup (PQgetvalue (res , 0 , 0 ));
@@ -578,7 +554,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
578
554
else
579
555
{
580
556
/* exit otherwise */
581
- printf ("%s" , PQerrorMessage (current_conn ));
557
+ printf ("%s" , PQerrorMessage (connection ));
582
558
PQclear (res );
583
559
exit (1 );
584
560
}
@@ -606,7 +582,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
606
582
}
607
583
608
584
void
609
- pgut_cleanup (pqbool fatal )
585
+ pgut_cleanup (bool fatal )
610
586
{
611
587
if (fatal )
612
588
{
@@ -622,11 +598,11 @@ pgut_cleanup(pqbool fatal)
622
598
return ; /* no needs to cleanup */
623
599
624
600
/* Rollback current transaction */
625
- if (current_conn )
601
+ if (connection )
626
602
command ("ROLLBACK" , 0 , NULL );
627
603
628
604
/* Try reconnection if not available. */
629
- if (PQstatus (current_conn ) != CONNECTION_OK )
605
+ if (PQstatus (connection ) != CONNECTION_OK )
630
606
reconnect ();
631
607
632
608
/* do cleanup */
@@ -636,7 +612,7 @@ pgut_cleanup(pqbool fatal)
636
612
}
637
613
}
638
614
639
- int
615
+ void
640
616
pgut_help (void )
641
617
{
642
618
fprintf (stderr ,
@@ -645,33 +621,11 @@ pgut_help(void)
645
621
" %s [OPTION]... [DBNAME]\n"
646
622
"\nOptions:\n"
647
623
" -a, --all reorg all databases\n"
648
- " -d, --dbname=DBNAME database to reorg\n"
649
624
" -t, --table=TABLE reorg specific table only\n"
650
625
" -n, --no-order do vacuum full instead of cluster\n"
651
626
" -o, --order-by=columns order by columns instead of cluster keys\n"
652
627
" -e, --echo show the commands being sent to the server\n"
653
628
" -q, --quiet don't write any messages\n"
654
- " -v, --verbose display detailed information during processing\n"
655
- " --help show this help, then exit\n"
656
- " --version output version information, then exit\n"
657
- "\nConnection options:\n"
658
- " -h, --host=HOSTNAME database server host or socket directory\n"
659
- " -p, --port=PORT database server port\n"
660
- " -U, --username=USERNAME user name to connect as\n"
661
- " -W, --password force password prompt\n" ,
662
- progname , progname );
663
- #ifdef PROGRAM_URL
664
- fprintf (stderr ,"\nRead the website for details. <" PROGRAM_URL ">\n" );
665
- #endif
666
- #ifdef PROGRAM_EMAIL
667
- fprintf (stderr ,"\nReport bugs to <" PROGRAM_EMAIL ">.\n" );
668
- #endif
669
- return EXITCODE_HELP ;
670
- }
671
-
672
- int
673
- pgut_version (void )
674
- {
675
- fprintf (stderr , "%s %s\n" , progname , PROGRAM_VERSION );
676
- return EXITCODE_HELP ;
629
+ " -v, --verbose display detailed information during processing\n" ,
630
+ PROGRAM_NAME , PROGRAM_NAME );
677
631
}
0 commit comments