Skip to content

Commit

Permalink
[ZBX-24095] improved data fetch performance for PostgreSQL (vso)
Browse files Browse the repository at this point in the history
  • Loading branch information
CHERTS committed Apr 11, 2024
1 parent 734fdaa commit dfe2fef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 79 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ A......... [ZBX-21332] fixed runtime errors when linking items from two template
....I..... [ZBX-22783] added automatic detection of Oracle instant client installed from RPM (mprihodko)
...G...PS. [ZBX-23221] fixed memory leaks when using certificate-based encryption in Zabbix Agent 1 and Agent 2; thanks to Masato Hirahata for the patch (akozlovs, Andris)
...G...... [ZBX-23890] removed non-existing headers from compiler checks: mtent.h and knlist.h (arimdjonoks)
.......PS. [ZBX-24095] improved data fetch performance for PostgreSQL (vso)

--------------------------------------------------------------------------------
Changes for 4.4.27
Expand Down
1 change: 1 addition & 0 deletions PATCHLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@
[ZBX-22783] added automatic detection of Oracle instant client installed from RPM (https://support.zabbix.com/browse/ZBX-22783)
[ZBX-23221] fixed memory leaks when using certificate-based encryption in Zabbix Agent 1 and Agent 2; thanks to Masato Hirahata for the patch (https://support.zabbix.com/browse/ZBX-23221)
[ZBX-23890] removed non-existing headers from compiler checks: mtent.h and knlist.h (https://support.zabbix.com/browse/ZBX-23890)
[ZBX-24095] improved data fetch performance for PostgreSQL (https://support.zabbix.com/browse/ZBX-24095)
[ZBXNEXT-170] implemented possibility to "unlink" specific templates in hosts and templates massupdate (https://support.zabbix.com/browse/ZBXNEXT-170)
[ZBXNEXT-284] added timeout parameter to zabbix_sender and zabbix_get (https://support.zabbix.com/browse/ZBXNEXT-284)
[ZBXNEXT-435] Add Oracle TNSNAMES connect support (https://support.zabbix.com/browse/ZBXNEXT-435)
Expand Down
87 changes: 8 additions & 79 deletions src/libs/zbxdb/db.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ static ub4 OCI_DBserver_status(void);

#elif defined(HAVE_POSTGRESQL)
static PGconn *conn = NULL;
static unsigned int ZBX_PG_BYTEAOID = 0;
static int ZBX_PG_SVERSION = 0;
char ZBX_PG_ESCAPE_BACKSLASH = 1;
#elif defined(HAVE_SQLITE3)
Expand Down Expand Up @@ -588,18 +587,6 @@ int zbx_db_connect(char *host, char *user, char *password, char *dbname, char *d
if (ZBX_DB_FAIL == ret || ZBX_DB_DOWN == ret)
goto out;

result = zbx_db_select("select oid from pg_type where typname='bytea'");

if ((DB_RESULT)ZBX_DB_DOWN == result || NULL == result)
{
ret = (NULL == result) ? ZBX_DB_FAIL : ZBX_DB_DOWN;
goto out;
}

if (NULL != (row = zbx_db_fetch(result)))
ZBX_PG_BYTEAOID = atoi(row[0]);
DBfree_result(result);

ZBX_PG_SVERSION = PQserverVersion(conn);
zabbix_log(LOG_LEVEL_DEBUG, "PostgreSQL Server version: %d", ZBX_PG_SVERSION);

Expand Down Expand Up @@ -1667,56 +1654,6 @@ DB_RESULT zbx_db_select_n(const char *query, int n)
#endif
}

#ifdef HAVE_POSTGRESQL
/******************************************************************************
* *
* Purpose: converts the null terminated string into binary buffer *
* *
* Transformations: *
* \ooo == a byte whose value = ooo (ooo is an octal number) *
* \\ == \ *
* *
* Parameters: *
* io - [IN/OUT] null terminated string / binary data *
* *
* Return value: length of the binary buffer *
* *
******************************************************************************/
static size_t zbx_db_bytea_unescape(u_char *io)
{
const u_char *i = io;
u_char *o = io;

while ('\0' != *i)
{
switch (*i)
{
case '\\':
i++;
if ('\\' == *i)
{
*o++ = *i++;
}
else
{
if (0 != isdigit(i[0]) && 0 != isdigit(i[1]) && 0 != isdigit(i[2]))
{
*o = (*i++ - 0x30) << 6;
*o += (*i++ - 0x30) << 3;
*o++ += *i++ - 0x30;
}
}
break;

default:
*o++ = *i++;
}
}

return o - io;
}
#endif

DB_ROW zbx_db_fetch(DB_RESULT result)
{
#if defined(HAVE_ORACLE)
Expand Down Expand Up @@ -1822,35 +1759,27 @@ DB_ROW zbx_db_fetch(DB_RESULT result)

return result->values;
#elif defined(HAVE_POSTGRESQL)
/* free old data */
if (NULL != result->values)
zbx_free(result->values);

/* EOF */
if (result->cursor == result->row_num)
return NULL;

/* init result */
result->fld_num = PQnfields(result->pg_result);
if (0 == result->cursor)
result->fld_num = PQnfields(result->pg_result);

if (result->fld_num > 0)
{
int i;

result->values = zbx_malloc(result->values, sizeof(char *) * result->fld_num);
if (NULL == result->values)
result->values = zbx_malloc(result->values, sizeof(char *) * result->fld_num);

for (i = 0; i < result->fld_num; i++)
{
if (PQgetisnull(result->pg_result, result->cursor, i))
{
result->values[i] = NULL;
}
else
{
result->values[i] = PQgetvalue(result->pg_result, result->cursor, i);
if (PQftype(result->pg_result, i) == ZBX_PG_BYTEAOID) /* binary data type BYTEAOID */
zbx_db_bytea_unescape((u_char *)result->values[i]);
}
result->values[i] = PQgetvalue(result->pg_result, result->cursor, i);

if ('\0' == *result->values[i] && PQgetisnull(result->pg_result, result->cursor, i))
result->values[i] = NULL;
}
}

Expand Down

0 comments on commit dfe2fef

Please sign in to comment.