Skip to content

Commit 9a8f2e9

Browse files
committed
Fix ownership bug.
New toast table, toast index, and toast type should not have been owned by the executor of pg_reorg, but by the original owner.
1 parent 0c659ed commit 9a8f2e9

File tree

7 files changed

+424
-246
lines changed

7 files changed

+424
-246
lines changed

bin/pg_reorg.c

Lines changed: 34 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@
88
* @brief Client Modules
99
*/
1010

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";
1414

1515
#include "pgut/pgut.h"
16-
#include "pqexpbuffer.h"
1716

1817
#include <string.h>
1918
#include <stdlib.h>
2019
#include <unistd.h>
2120

22-
#define EXITCODE_HELP 2
2321
#define APPLY_COUNT 1000
2422

2523
#define SQL_XID_SNAPSHOT_80300 \
@@ -37,12 +35,12 @@
3735
" AND pid <> pg_backend_pid() AND transactionid = ANY($1) LIMIT 1"
3836

3937
#define SQL_XID_SNAPSHOT \
40-
(PQserverVersion(current_conn) >= 80300 \
38+
(PQserverVersion(connection) >= 80300 \
4139
? SQL_XID_SNAPSHOT_80300 \
4240
: SQL_XID_SNAPSHOT_80200)
4341

4442
#define SQL_XID_ALIVE \
45-
(PQserverVersion(current_conn) >= 80300 \
43+
(PQserverVersion(connection) >= 80300 \
4644
? SQL_XID_ALIVE_80300 \
4745
: SQL_XID_ALIVE_80200)
4846

@@ -80,7 +78,7 @@ typedef struct reorg_index
8078
} reorg_index;
8179

8280
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);
8482
static void reorg_one_table(const reorg_table *table, const char *orderby);
8583

8684
static char *getstr(PGresult *res, int row, int col);
@@ -89,14 +87,14 @@ static Oid getoid(PGresult *res, int row, int col);
8987
#define SQLSTATE_INVALID_SCHEMA_NAME "3F000"
9088
#define SQLSTATE_LOCK_NOT_AVAILABLE "55P03"
9189

92-
static pqbool sqlstate_equals(PGresult *res, const char *state)
90+
static bool sqlstate_equals(PGresult *res, const char *state)
9391
{
9492
return strcmp(PQresultErrorField(res, PG_DIAG_SQLSTATE), state) == 0;
9593
}
9694

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;
10098

10199
/*
102100
* The table begin re-organized. If not null, we need to cleanup temp
@@ -125,11 +123,11 @@ const struct option pgut_longopts[] = {
125123
{NULL, 0, NULL, 0}
126124
};
127125

128-
pqbool alldb = false;
126+
bool alldb = false;
129127
const char *table = NULL;
130128
const char *orderby = NULL;
131129

132-
pqbool
130+
bool
133131
pgut_argument(int c, const char *arg)
134132
{
135133
switch (c)
@@ -147,13 +145,13 @@ pgut_argument(int c, const char *arg)
147145
alldb = true;
148146
break;
149147
case 't':
150-
table = arg;
148+
assign_option(&table, c, arg);
151149
break;
152150
case 'n':
153-
orderby = "";
151+
assign_option(&orderby, c, "");
154152
break;
155153
case 'o':
156-
orderby = arg;
154+
assign_option(&orderby, c, arg);
157155
break;
158156
default:
159157
return false;
@@ -164,30 +162,18 @@ pgut_argument(int c, const char *arg)
164162
int
165163
main(int argc, char *argv[])
166164
{
167-
int exitcode;
168-
169-
exitcode = pgut_getopt(argc, argv);
170-
if (exitcode)
171-
return exitcode;
165+
parse_options(argc, argv);
172166

173167
if (alldb)
174168
{
175169
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");
182171
reorg_all_databases(orderby);
183172
}
184173
else
185174
{
186175
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);
191177
}
192178

193179
return 0;
@@ -209,13 +195,13 @@ reorg_all_databases(const char *orderby)
209195

210196
for (i = 0; i < PQntuples(result); i++)
211197
{
212-
pqbool ret;
198+
bool ret;
213199

214200
dbname = PQgetvalue(result, i, 0);
215201

216202
if (!quiet)
217203
{
218-
printf("%s: reorg database \"%s\"", progname, dbname);
204+
printf("%s: reorg database \"%s\"", PROGRAM_NAME, dbname);
219205
fflush(stdout);
220206
}
221207

@@ -256,10 +242,10 @@ getoid(PGresult *res, int row, int col)
256242
/*
257243
* Call reorg_one_table for the target table or each table in a database.
258244
*/
259-
static pqbool
245+
static bool
260246
reorg_one_database(const char *orderby, const char *table)
261247
{
262-
pqbool ret = true;
248+
bool ret = true;
263249
PGresult *res;
264250
int i;
265251
int num;
@@ -301,7 +287,7 @@ reorg_one_database(const char *orderby, const char *table)
301287
else
302288
{
303289
/* exit otherwise */
304-
printf("%s", PQerrorMessage(current_conn));
290+
printf("%s", PQerrorMessage(connection));
305291
PQclear(res);
306292
exit(1);
307293
}
@@ -324,10 +310,7 @@ reorg_one_database(const char *orderby, const char *table)
324310
table.ckid = getoid(res, i, c++);
325311

326312
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);
331314

332315
table.create_pktype = getstr(res, i, c++);
333316
table.create_log = getstr(res, i, c++);
@@ -343,10 +326,7 @@ reorg_one_database(const char *orderby, const char *table)
343326
{
344327
/* CLUSTER mode */
345328
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);
350330
appendPQExpBuffer(&sql, "%s ORDER BY %s", create_table, ckey);
351331
table.create_table = sql.data;
352332
}
@@ -455,11 +435,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
455435
" WHERE tgrelid = $1 AND tgname >= 'z_reorg_trigger' LIMIT 1",
456436
1, params);
457437
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);
463439

464440
command(table->create_pktype, 0, NULL);
465441
command(table->create_log, 0, NULL);
@@ -482,7 +458,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
482458
command("BEGIN ISOLATION LEVEL SERIALIZABLE", 0, NULL);
483459
/* SET work_mem = maintenance_work_mem */
484460
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])
486462
command("SET LOCAL synchronize_seqscans = off", 0, NULL);
487463
res = execute(SQL_XID_SNAPSHOT, 0, NULL);
488464
vxid = strdup(PQgetvalue(res, 0, 0));
@@ -578,7 +554,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
578554
else
579555
{
580556
/* exit otherwise */
581-
printf("%s", PQerrorMessage(current_conn));
557+
printf("%s", PQerrorMessage(connection));
582558
PQclear(res);
583559
exit(1);
584560
}
@@ -606,7 +582,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
606582
}
607583

608584
void
609-
pgut_cleanup(pqbool fatal)
585+
pgut_cleanup(bool fatal)
610586
{
611587
if (fatal)
612588
{
@@ -622,11 +598,11 @@ pgut_cleanup(pqbool fatal)
622598
return; /* no needs to cleanup */
623599

624600
/* Rollback current transaction */
625-
if (current_conn)
601+
if (connection)
626602
command("ROLLBACK", 0, NULL);
627603

628604
/* Try reconnection if not available. */
629-
if (PQstatus(current_conn) != CONNECTION_OK)
605+
if (PQstatus(connection) != CONNECTION_OK)
630606
reconnect();
631607

632608
/* do cleanup */
@@ -636,7 +612,7 @@ pgut_cleanup(pqbool fatal)
636612
}
637613
}
638614

639-
int
615+
void
640616
pgut_help(void)
641617
{
642618
fprintf(stderr,
@@ -645,33 +621,11 @@ pgut_help(void)
645621
" %s [OPTION]... [DBNAME]\n"
646622
"\nOptions:\n"
647623
" -a, --all reorg all databases\n"
648-
" -d, --dbname=DBNAME database to reorg\n"
649624
" -t, --table=TABLE reorg specific table only\n"
650625
" -n, --no-order do vacuum full instead of cluster\n"
651626
" -o, --order-by=columns order by columns instead of cluster keys\n"
652627
" -e, --echo show the commands being sent to the server\n"
653628
" -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);
677631
}

0 commit comments

Comments
 (0)