Skip to content

Commit f551b56

Browse files
committed
Merge branch 'master' into concurrent_indexes.
Conflicts: bin/pg_repack.c
2 parents aa428b6 + 7213e2f commit f551b56

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

bin/pg_repack.c

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ repack_all_databases(const char *orderby)
325325
}
326326
}
327327

328-
PQclear(result);
328+
CLEARPGRES(result);
329329
}
330330

331331
/* result is not copied */
@@ -435,7 +435,7 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
435435
}
436436
goto cleanup;
437437
}
438-
PQclear(res);
438+
CLEARPGRES(res);
439439

440440
/* Disable statement timeout. */
441441
command("SET statement_timeout = 0", 0, NULL);
@@ -560,8 +560,7 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
560560
ret = true;
561561

562562
cleanup:
563-
if (res)
564-
PQclear(res);
563+
CLEARPGRES(res);
565564
disconnect();
566565
termStringInfo(&sql);
567566
return ret;
@@ -586,7 +585,7 @@ apply_log(PGconn *conn, const repack_table *table, int count)
586585
"SELECT repack.repack_apply($1, $2, $3, $4, $5, $6)",
587586
6, params);
588587
result = atoi(PQgetvalue(res, 0, 0));
589-
PQclear(res);
588+
CLEARPGRES(res);
590589

591590
return result;
592591
}
@@ -683,7 +682,6 @@ rebuild_indexes(const repack_table *table)
683682
elog(WARNING, "Error sending async query: %s\n%s",
684683
index_jobs[i].create_index,
685684
PQerrorMessage(workers.conns[i]));
686-
PQclear(res);
687685
have_error = true;
688686
goto cleanup;
689687
}
@@ -692,15 +690,14 @@ rebuild_indexes(const repack_table *table)
692690
* available. That's OK, we'll get to them later.
693691
*/
694692
}
695-
PQclear(res);
693+
CLEARPGRES(res);
696694

697695
if (num_workers > 1)
698696
{
699697
int freed_worker = -1;
700698
int ret;
701699

702700
/* Prefer poll() over select(), following PostgreSQL custom. */
703-
#undef HAVE_POLL
704701
#ifdef HAVE_POLL
705702
struct pollfd *input_fds;
706703

@@ -749,6 +746,8 @@ rebuild_indexes(const repack_table *table)
749746
if (ret < 0 && errno != EINTR)
750747
elog(ERROR, "poll() failed: %d, %d", ret, errno);
751748

749+
elog(DEBUG2, "Poll returned: %d", ret);
750+
752751
for (i = 0; i < num_indexes; i++)
753752
{
754753
if (index_jobs[i].status == INPROGRESS)
@@ -774,11 +773,10 @@ rebuild_indexes(const repack_table *table)
774773
{
775774
elog(WARNING, "Error with create index: %s",
776775
PQerrorMessage(workers.conns[index_jobs[i].worker_idx]));
777-
PQclear(res);
778776
have_error = true;
779777
goto cleanup;
780778
}
781-
PQclear(res);
779+
CLEARPGRES(res);
782780
}
783781

784782
/* We are only going to re-queue one worker, even
@@ -825,6 +823,7 @@ rebuild_indexes(const repack_table *table)
825823
}
826824

827825
cleanup:
826+
CLEARPGRES(res);
828827
return (!have_error);
829828
}
830829

@@ -835,7 +834,7 @@ rebuild_indexes(const repack_table *table)
835834
static void
836835
repack_one_table(const repack_table *table, const char *orderby)
837836
{
838-
PGresult *res;
837+
PGresult *res = NULL;
839838
const char *params[2];
840839
int num;
841840
int num_waiting = 0;
@@ -900,11 +899,10 @@ repack_one_table(const repack_table *table, const char *orderby)
900899
(errcode(E_PG_COMMAND),
901900
errmsg("trigger %s conflicted for %s",
902901
PQgetvalue(res, 0, 0), table->target_name)));
903-
PQclear(res);
904902
have_error = true;
905903
goto cleanup;
906904
}
907-
PQclear(res);
905+
CLEARPGRES(res);
908906

909907
command(table->create_pktype, 0, NULL);
910908
command(table->create_log, 0, NULL);
@@ -928,13 +926,12 @@ repack_one_table(const repack_table *table, const char *orderby)
928926
if (PQresultStatus(res) != PGRES_TUPLES_OK)
929927
{
930928
printf("%s", PQerrorMessage(conn2));
931-
PQclear(res);
932929
have_error = true;
933930
goto cleanup;
934931
}
935932
buffer[0] = '\0';
936933
strncat(buffer, PQgetvalue(res, 0, 0), sizeof(buffer) - 1);
937-
PQclear(res);
934+
CLEARPGRES(res);
938935

939936
/*
940937
* Not using lock_access_share() here since we know that
@@ -995,11 +992,10 @@ repack_one_table(const repack_table *table, const char *orderby)
995992
if (PQresultStatus(res) != PGRES_COMMAND_OK)
996993
{
997994
elog(WARNING, "Error with LOCK TABLE: %s", PQerrorMessage(conn2));
998-
PQclear(res);
999995
have_error = true;
1000996
goto cleanup;
1001997
}
1002-
PQclear(res);
998+
CLEARPGRES(res);
1003999
}
10041000

10051001
/* Turn conn2 back into blocking mode for further non-async use. */
@@ -1034,11 +1030,10 @@ repack_one_table(const repack_table *table, const char *orderby)
10341030
{
10351031
elog(WARNING, "Unable to allocate vxid, length: %d\n",
10361032
PQgetlength(res, 0, 0));
1037-
PQclear(res);
10381033
have_error = true;
10391034
goto cleanup;
10401035
}
1041-
PQclear(res);
1036+
CLEARPGRES(res);
10421037

10431038
/* Delete any existing entries in the log table now, since we have not
10441039
* yet run the CREATE TABLE ... AS SELECT, which will take in all existing
@@ -1079,6 +1074,7 @@ repack_one_table(const repack_table *table, const char *orderby)
10791074
have_error = true;
10801075
goto cleanup;
10811076
}
1077+
CLEARPGRES(res);
10821078

10831079
/*
10841080
* 4. Apply log to temp table until no tuples are left in the log
@@ -1109,15 +1105,15 @@ repack_one_table(const repack_table *table, const char *orderby)
11091105
num_waiting = num;
11101106
}
11111107

1112-
PQclear(res);
1108+
CLEARPGRES(res);
11131109
sleep(1);
11141110
continue;
11151111
}
11161112
else
11171113
{
11181114
/* All old transactions are finished;
11191115
* go to next step. */
1120-
PQclear(res);
1116+
CLEARPGRES(res);
11211117
break;
11221118
}
11231119
}
@@ -1169,6 +1165,7 @@ repack_one_table(const repack_table *table, const char *orderby)
11691165
}
11701166

11711167
cleanup:
1168+
CLEARPGRES(res);
11721169
termStringInfo(&sql);
11731170
if (vxid)
11741171
free(vxid);
@@ -1210,7 +1207,7 @@ kill_ddl(PGconn *conn, Oid relid, bool terminate)
12101207
"Canceled %d unsafe queries. Terminating any remaining PIDs.",
12111208
PQntuples(res));
12121209

1213-
PQclear(res);
1210+
CLEARPGRES(res);
12141211
printfStringInfo(&sql, KILL_COMPETING_LOCKS, relid);
12151212
res = pgut_execute(conn, sql.data, 0, NULL);
12161213
if (PQresultStatus(res) != PGRES_TUPLES_OK)
@@ -1225,7 +1222,7 @@ kill_ddl(PGconn *conn, Oid relid, bool terminate)
12251222
else
12261223
elog(DEBUG2, "No competing DDL to cancel.");
12271224

1228-
PQclear(res);
1225+
CLEARPGRES(res);
12291226
termStringInfo(&sql);
12301227

12311228
return ret;
@@ -1285,21 +1282,21 @@ lock_access_share(PGconn *conn, Oid relid, const char *target_name)
12851282
res = pgut_execute_elevel(conn, sql.data, 0, NULL, DEBUG2);
12861283
if (PQresultStatus(res) == PGRES_COMMAND_OK)
12871284
{
1288-
PQclear(res);
1285+
CLEARPGRES(res);
12891286
break;
12901287
}
12911288
else if (sqlstate_equals(res, SQLSTATE_QUERY_CANCELED))
12921289
{
12931290
/* retry if lock conflicted */
1294-
PQclear(res);
1291+
CLEARPGRES(res);
12951292
pgut_rollback(conn);
12961293
continue;
12971294
}
12981295
else
12991296
{
13001297
/* exit otherwise */
13011298
elog(WARNING, "%s", PQerrorMessage(connection));
1302-
PQclear(res);
1299+
CLEARPGRES(res);
13031300
ret = false;
13041301
break;
13051302
}
@@ -1371,21 +1368,21 @@ lock_exclusive(PGconn *conn, const char *relid, const char *lock_query, bool sta
13711368
res = pgut_execute_elevel(conn, lock_query, 0, NULL, DEBUG2);
13721369
if (PQresultStatus(res) == PGRES_COMMAND_OK)
13731370
{
1374-
PQclear(res);
1371+
CLEARPGRES(res);
13751372
break;
13761373
}
13771374
else if (sqlstate_equals(res, SQLSTATE_QUERY_CANCELED))
13781375
{
13791376
/* retry if lock conflicted */
1380-
PQclear(res);
1377+
CLEARPGRES(res);
13811378
pgut_rollback(conn);
13821379
continue;
13831380
}
13841381
else
13851382
{
13861383
/* exit otherwise */
13871384
printf("%s", PQerrorMessage(connection));
1388-
PQclear(res);
1385+
CLEARPGRES(res);
13891386
ret = false;
13901387
break;
13911388
}

bin/pgut/pgut-fe.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,9 @@ extern void pgut_readopt(const char *path, pgut_option options[], int elevel);
8484
extern void pgut_setopt(pgut_option *opt, const char *optarg, pgut_optsrc src);
8585
extern bool pgut_keyeq(const char *lhs, const char *rhs);
8686

87+
/* So we don't need to fret over multiple calls to PQclear(), e.g.
88+
* in cleanup labels.
89+
*/
90+
#define CLEARPGRES(pgres) do { PQclear(pgres); pgres = NULL; } while (0)
91+
8792
#endif /* PGUT_FE_H */

0 commit comments

Comments
 (0)