From: Tomas Vondra Date: Tue, 23 Aug 2016 16:36:13 +0000 (+0200) Subject: minor PgxcNodeListAndCount() refactoring to fix compiler warnings X-Git-Tag: XL9_5_R1_4~38 X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=a0919c75b40c6c06925ff609ef967e03cd64a8af;p=postgres-xl.git minor PgxcNodeListAndCount() refactoring to fix compiler warnings While tweaking the code, I've noticed this is the only place using NodeHealthStatus, apparently attempting to save a few bytes. That seems rather pointless given how little memory this will need and how short-lived the memory is (and also how AllocSet doubles the allocation sizes anyway), so I got rid of this. This makes copying the old state easier (a simple memcpy instead of for loops). Note: The PgxcNodeListAndCount() seems a bit unfortunate, as the name suggests to list and count nodes, but in practice it refreshes the status of nodes from catalog, while keeping the health status. --- diff --git a/src/backend/pgxc/nodemgr/nodemgr.c b/src/backend/pgxc/nodemgr/nodemgr.c index 683cd095d5..5e3290d419 100644 --- a/src/backend/pgxc/nodemgr/nodemgr.c +++ b/src/backend/pgxc/nodemgr/nodemgr.c @@ -326,37 +326,32 @@ PgxcNodeListAndCount(void) Relation rel; HeapScanDesc scan; HeapTuple tuple; - NodeHealthStatus *nodehealth; - int numNodes = 0; + NodeDefinition *nodes = NULL; + int numNodes; LWLockAcquire(NodeTableLock, LW_EXCLUSIVE); + numNodes = *shmemNumCoords + *shmemNumDataNodes; + + Assert((*shmemNumCoords >= 0) && (*shmemNumDataNodes >= 0)); + /* * Save the existing health status values because nodes * might get added or deleted here. We will save * nodeoid, status. No need to differentiate between * coords and datanodes since oids will be unique anyways */ - if (*shmemNumDataNodes != 0 || *shmemNumCoords != 0) + if (numNodes > 0) { - int i, j; + nodes = (NodeDefinition*)palloc(numNodes * sizeof(NodeDefinition)); - numNodes = *shmemNumCoords + *shmemNumDataNodes; - nodehealth = palloc0( - numNodes * sizeof(NodeHealthStatus)); + /* XXX It's possible to call memcpy with */ + if (*shmemNumCoords > 0) + memcpy(nodes, coDefs, *shmemNumCoords * sizeof(NodeDefinition)); - for (i = 0; i < *shmemNumCoords; i++) - { - nodehealth[i].nodeoid = coDefs[i].nodeoid; - nodehealth[i].nodeishealthy = coDefs[i].nodeishealthy; - } - - j = i; - for (i = 0; i < *shmemNumDataNodes; i++) - { - nodehealth[j].nodeoid = dnDefs[i].nodeoid; - nodehealth[j++].nodeishealthy = dnDefs[i].nodeishealthy; - } + if (*shmemNumDataNodes > 0) + memcpy(nodes + *shmemNumCoords, dnDefs, + *shmemNumDataNodes * sizeof(NodeDefinition)); } *shmemNumCoords = 0; @@ -404,9 +399,9 @@ PgxcNodeListAndCount(void) node->nodeishealthy = true; for (i = 0; i < numNodes; i++) { - if (nodehealth[i].nodeoid == node->nodeoid) + if (nodes[i].nodeoid == node->nodeoid) { - node->nodeishealthy = nodehealth[i].nodeishealthy; + node->nodeishealthy = nodes[i].nodeishealthy; break; } } @@ -418,7 +413,7 @@ PgxcNodeListAndCount(void) *shmemNumCoords, *shmemNumDataNodes); if (numNodes) - pfree(nodehealth); + pfree(nodes); /* Finally sort the lists */ if (*shmemNumCoords > 1) diff --git a/src/include/pgxc/nodemgr.h b/src/include/pgxc/nodemgr.h index 976384a4e8..0c1dca7abe 100644 --- a/src/include/pgxc/nodemgr.h +++ b/src/include/pgxc/nodemgr.h @@ -41,12 +41,6 @@ typedef struct bool nodeishealthy; } NodeDefinition; -typedef struct -{ - Oid nodeoid; - bool nodeishealthy; -} NodeHealthStatus; - extern void NodeTablesShmemInit(void); extern Size NodeTablesShmemSize(void);