Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e7b7037

Browse files
committed
Sane error message if connection to myself in alter_system_c failed.
Also, yet another bug in check_sub_sync covered: sometimes learning sub lsn returns row, but the lsn itself is null.
1 parent 240f19a commit e7b7037

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/copypart.c

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -930,39 +930,53 @@ int
930930
check_sub_sync(const char *subname, PGconn **conn, XLogRecPtr ref_lsn,
931931
const char *log_pref)
932932
{
933-
PGresult *res;
933+
PGresult *res = NULL;
934934
char *received_lsn_str;
935935
XLogRecPtr received_lsn;
936936
char *sql = received_lsn_sql(subname);
937937

938938
res = PQexec(*conn, sql);
939-
pfree(sql);
940939
if (PQresultStatus(res) != PGRES_TUPLES_OK)
941940
{
942941
shmn_elog(LOG, "%s: failed to learn sub lsn on src: %s",
943942
log_pref, PQerrorMessage(*conn));
944943
reset_pqconn_and_res(conn, res);
945-
return -1;
944+
res = NULL; /* TODO: reset_res */
945+
goto fail;
946946
}
947947
/* FIXME: this should never be true, but sometimes it is. */
948948
if (PQntuples(res) != 1)
949949
{
950950
shmn_elog(LOG, "learning sub %s lsn returned %d rows, query %s",
951951
subname, PQntuples(res), sql);
952-
PQclear(res);
953-
return -1;
952+
goto fail;
953+
}
954+
/* FIXME: this should never be true, but sometimes it is. */
955+
if (PQgetisnull(res, 0, 0))
956+
{
957+
shmn_elog(LOG, "learning sub %s lsn returned NULL received_lsn, query %s",
958+
subname, sql);
959+
goto fail;
954960
}
955961
received_lsn_str = PQgetvalue(res, 0, 0);
956962
shmn_elog(DEBUG1, "%s: received_lsn is %s", log_pref, received_lsn_str);
957963
received_lsn = pg_lsn_in_c(received_lsn_str);
958-
PQclear(res);
959964
if (received_lsn < ref_lsn)
960965
{
961966
shmn_elog(DEBUG1, "%s: sub is not yet synced, received_lsn is %lu, "
962967
" but we wait for %lu", log_pref, received_lsn, ref_lsn);
963-
return -1;
968+
goto fail;
964969
}
970+
971+
PQclear(res);
972+
pfree(sql);
965973
return 0;
974+
975+
fail:
976+
/* TODO: reset_res */
977+
PQclear(res);
978+
pfree(sql);
979+
return -1;
966980
}
967981

968982
/*

src/pg_shardman.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ listen_cmd_log_inserts(void)
306306
{
307307
PGresult *res;
308308

309+
/* TODO: make sure we connect to ourselves, probably cmp ports */
309310
conn = PQconnectdb(shardman_shardlord_connstring);
310311
/* Check to see that the backend connection was successfully made */
311312
if (PQstatus(conn) != CONNECTION_OK)
@@ -864,13 +865,13 @@ rm_node(Cmd *cmd)
864865

865866
/*
866867
* Finish pq connection and set ptr to NULL. You must be sure that the
867-
* connection exists!
868+
* connection exists or ptr is NULL.
868869
*/
869870
void
870871
reset_pqconn(PGconn **conn) { PQfinish(*conn); *conn = NULL; }
871872
/*
872873
* Same, but also clear res. You must be sure that both connection and res
873-
* exist.
874+
* exist or they are NULL ptrs.
874875
*/
875876
void
876877
reset_pqconn_and_res(PGconn **conn, PGresult *res)

src/udf.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -415,20 +415,23 @@ alter_system_c(PG_FUNCTION_ARGS)
415415
conn = PQconnectdb(connstr);
416416
if (PQstatus(conn) != CONNECTION_OK)
417417
{
418-
PQfinish(conn);
419-
elog(ERROR, "Connection to myself with connstr %s failed", connstr);
420-
418+
elog(WARNING, "Connection to myself with connstr %s failed: %s",
419+
connstr, PQerrorMessage(conn));
420+
goto fail;
421421
}
422422
SPI_finish(); /* Can't do earlier since connstr is allocated there */
423423
res = PQexec(conn, cmd);
424424
if (PQresultStatus(res) != PGRES_COMMAND_OK)
425425
{
426-
PQclear(res);
427-
PQfinish(conn);
428-
elog(ERROR, "setting %s to %s failed", opt, val);
426+
elog(WARNING, "setting %s to %s failed", opt, val);
427+
goto fail;
429428
}
430429

431430
PQclear(res);
432431
PQfinish(conn);
433432
PG_RETURN_VOID();
433+
434+
fail:
435+
reset_pqconn_and_res(&conn, res);
436+
elog(ERROR, "alter_system_c failed");
434437
}

0 commit comments

Comments
 (0)