pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
[PATCH] Remove unncessary localtime() calls during data type conversion
7+ messages / 4 participants
[nested] [flat]

* [PATCH] Remove unncessary localtime() calls during data type conversion
@ 2015-06-13 00:42  Nikhil Deshpande <ndeshpande@vmware.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Nikhil Deshpande @ 2015-06-13 00:42 UTC (permalink / raw)
  To: pgsql-odbc

Hi,

While doing some profiling work on an ODBC client, we noticed tons
of libc localtime() and __tz_convert() calls on a hot code path
(SQLFetch()), but most of the fields being retrieved were not of
date/time type.


These libc calls are costly (non-trivial work in terms of string ops,
timzone conversion etc.) and the cost adds up quickly for high-frequency
call like SQLFetch(). We noticed that this penalty is paid even
for data types which aren't date/time related. Code inspection
showed localtime() call in convert.c:copy_and_convert_field()
at function start, being invoked regardless of whether output of
localtime() is used or not.

Attached is a non-intrusive patch that moves the call down
to where it's needed (for both getting data from server and
sending data to server), but I'm not sure if the patch is complete
(it covers the case where source data type is time but
destination needs date part, "TIME->DATE[TIME]").

Could you please review this patch and modify it for correctness
and consider for committing it?

---

A longer term cleaner approach would be to refactor the code to aim for
at most one call to localtime() (e.g. if it's already invoked once
in copy_and_convert_field() before, reuse that data). However
that is much more intrusive change.

I checked the test/src/result-conversions-test.c code and the
test case specific to this scenario is commented out (since it
the expected result would depend on actual current date).
I un-commented those test cases, ran the test with and without
my patch and got same results. Also the data looks ok for
"TIME->DATE[TIME]" case, input is time, output is date.

   1114a1176
   > '13:23:34' (time) as SQL_C_TYPE_DATE: y: 2015 m: 6 d: 12

2015-6-12 is indeed current date.

Thanks,
  Nikhil

-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc

Attachments:

0001-Remove-unncessary-localtime-calls-during-data-type-c.patch (text/plain, 3.2K) download From e4f32886b34198d5620335b507e3ee5d0363d738 Mon Sep 17 00:00:00 2001 From: Nikhil R Deshpande <ndeshpande@vmware.com> Date: Fri, 12 Jun 2015 11:53:09 -0700 Subject: [PATCH 1/1] Remove unncessary localtime() calls during data type conversion While sending/fetching field of time data type to/from server, it might get converted to a data type that needs date part. In this case, the driver chooses to fill up the missing date part as current date, obtained through localtime() libc call. However this localtime() call is invoked for every combination of source and destination data types, and not just for above specific time->date[time] case. localtime() is not a cheap call (involves libc doing string processing and timezone conversion operations), and both send/fetch data paths are very high occurrence/hot code paths. This penalty adds up to significant impact on overall driver performance. --- convert.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/convert.c b/convert.c index 8802e6e..d22885a 100644 --- a/convert.c +++ b/convert.c @@ -912,16 +912,6 @@ copy_and_convert_field(StatementClass *stmt, memset(&std_time, 0, sizeof(SIMPLE_TIME)); - /* Initialize current date */ -#ifdef HAVE_LOCALTIME_R - tim = localtime_r(&stmt_t, &tm); -#else - tim = localtime(&stmt_t); -#endif /* HAVE_LOCALTIME_R */ - std_time.m = tim->tm_mon + 1; - std_time.d = tim->tm_mday; - std_time.y = tim->tm_year + 1900; - mylog("copy_and_convert: field_type = %d, fctype = %d, value = '%s', cbValueMax=%d\n", field_type, fCType, (value == NULL) ? "<NULL>" : value, cbValueMax); if (!value) @@ -1017,6 +1007,17 @@ mylog("null_cvt_date_string=%d\n", conn->connInfo.cvt_null_date_string); case PG_TYPE_TIME: sscanf(value, "%2d:%2d:%2d", &std_time.hh, &std_time.mm, &std_time.ss); + /* Initialize date in case conversion destination + * expects date part from this source time data. + */ +#ifdef HAVE_LOCALTIME_R + tim = localtime_r(&stmt_t, &tm); +#else + tim = localtime(&stmt_t); +#endif /* HAVE_LOCALTIME_R */ + std_time.m = tim->tm_mon + 1; + std_time.d = tim->tm_mday; + std_time.y = tim->tm_year + 1900; break; case PG_TYPE_ABSTIME: @@ -4139,15 +4140,6 @@ inolog("ipara=%p paramType=%d %d proc_return=%d\n", ipara, ipara ? ipara->paramT param_string[0] = '\0'; cbuf[0] = '\0'; memset(&st, 0, sizeof(st)); - t = SC_get_time(qb->stmt); -#ifdef HAVE_LOCALTIME_R - tim = localtime_r(&t, &tm); -#else - tim = localtime(&t); -#endif /* HAVE_LOCALTIME_R */ - st.m = tim->tm_mon + 1; - st.d = tim->tm_mday; - st.y = tim->tm_year + 1900; ivstruct = (SQL_INTERVAL_STRUCT *) buffer; /* Convert input C type to a neutral format */ @@ -4290,7 +4282,18 @@ mylog("C_WCHAR=%s(%d)\n", buffer, used); st.hh = ts->hour; st.mm = ts->minute; st.ss = ts->second; - + /* Initialize date in case conversion destination + * expects date part from this source time data. + */ + t = SC_get_time(qb->stmt); +#ifdef HAVE_LOCALTIME_R + tim = localtime_r(&t, &tm); +#else + tim = localtime(&t); +#endif /* HAVE_LOCALTIME_R */ + st.m = tim->tm_mon + 1; + st.d = tim->tm_mday; + st.y = tim->tm_year + 1900; break; } -- 1.8.5.6
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: [PATCH] Remove unncessary localtime() calls during data type conversion
@ 2015-06-14 03:36  Michael Paquier <michael.paquier@gmail.com>
  parent: Nikhil Deshpande <ndeshpande@vmware.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Michael Paquier @ 2015-06-14 03:36 UTC (permalink / raw)
  To: Nikhil Deshpande <ndeshpande@vmware.com>; +Cc: pgsql-odbc

On Sat, Jun 13, 2015 at 9:42 AM, Nikhil Deshpande <ndeshpande@vmware.com> wrote:
> While doing some profiling work on an ODBC client, we noticed tons
> of libc localtime() and __tz_convert() calls on a hot code path
> (SQLFetch()), but most of the fields being retrieved were not of
> date/time type.
>
>
> These libc calls are costly (non-trivial work in terms of string ops,
> timzone conversion etc.) and the cost adds up quickly for high-frequency
> call like SQLFetch(). We noticed that this penalty is paid even
> for data types which aren't date/time related. Code inspection
> showed localtime() call in convert.c:copy_and_convert_field()
> at function start, being invoked regardless of whether output of
> localtime() is used or not.
>
> Attached is a non-intrusive patch that moves the call down
> to where it's needed (for both getting data from server and
> sending data to server), but I'm not sure if the patch is complete
> (it covers the case where source data type is time but
> destination needs date part, "TIME->DATE[TIME]").
>
> Could you please review this patch and modify it for correctness
> and consider for committing it?

Indeed. I would move as well the declarations of tim more within the
inner loops.

> A longer term cleaner approach would be to refactor the code to aim for
> at most one call to localtime() (e.g. if it's already invoked once
> in copy_and_convert_field() before, reuse that data). However
> that is much more intrusive change.

Yep. This would be saner long-term.
-- 
Michael


-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [PATCH] Remove unncessary localtime() calls during data type conversion
@ 2015-09-01 09:11  Heikki Linnakangas <hlinnaka@iki.fi>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Heikki Linnakangas @ 2015-09-01 09:11 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; Nikhil Deshpande <ndeshpande@vmware.com>; +Cc: pgsql-odbc

On 06/14/2015 06:36 AM, Michael Paquier wrote:
> On Sat, Jun 13, 2015 at 9:42 AM, Nikhil Deshpande <ndeshpande@vmware.com> wrote:
>> While doing some profiling work on an ODBC client, we noticed tons
>> of libc localtime() and __tz_convert() calls on a hot code path
>> (SQLFetch()), but most of the fields being retrieved were not of
>> date/time type.
>>
>>
>> These libc calls are costly (non-trivial work in terms of string ops,
>> timzone conversion etc.) and the cost adds up quickly for high-frequency
>> call like SQLFetch(). We noticed that this penalty is paid even
>> for data types which aren't date/time related. Code inspection
>> showed localtime() call in convert.c:copy_and_convert_field()
>> at function start, being invoked regardless of whether output of
>> localtime() is used or not.
>>
>> Attached is a non-intrusive patch that moves the call down
>> to where it's needed (for both getting data from server and
>> sending data to server), but I'm not sure if the patch is complete
>> (it covers the case where source data type is time but
>> destination needs date part, "TIME->DATE[TIME]").
>>
>> Could you please review this patch and modify it for correctness
>> and consider for committing it?
>
> Indeed. I would move as well the declarations of tim more within the
> inner loops.

This changes behaviour, when converting from text to date column. 
Without this patch, if the string you're converting from is missing a 
year/month/date field, it will be filled with the current date. This 
modification to the result-conversions test case shows the difference:

inline patch 1: test/src/result-conversions-test.c (+2/-0 lines) download inline-1.patch--- a/test/src/result-conversions-test.c +++ b/test/src/result-conversions-test.c @@ -626,6 +626,8 @@ int main(int argc, char **argv) test_conversion("float8", "Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20); test_conversion("float8", "-Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20); + test_conversion("text", "", SQL_C_TYPE_DATE, "SQL_C_TYPE_DATE", 20); + /* Clean up */ test_disconnect();
We don't currently contain tests like that in the test suite, as mentioned in the comment earlier in result-conversions-test.c: > /* > * Converting arbitrary things to date and timestamp produces results that > * depend on the current timestamp, because the driver substitutes the > * current year/month/datefor missing values. Disable for now, to get a > * reproducible result. > */ Would be nice to include those tests, perhaps by printing the difference to current date instead of the absolute date. - Heikki -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc ^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: [PATCH] Remove unncessary localtime() calls during data type conversion
@ 2016-04-25 06:30  Michael Paquier <michael.paquier@gmail.com>
  parent: Heikki Linnakangas <hlinnaka@iki.fi>
  0 siblings, 1 reply; 7+ messages in thread

From: Michael Paquier @ 2016-04-25 06:30 UTC (permalink / raw)
  To: Heikki Linnakangas <hlinnaka@iki.fi>; +Cc: Nikhil Deshpande <ndeshpande@vmware.com>; pgsql-odbc

On Tue, Sep 1, 2015 at 6:11 PM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
> This changes behaviour, when converting from text to date column. Without
> this patch, if the string you're converting from is missing a
> year/month/date field, it will be filled with the current date.

Waking up an old thread, after being reminded by some colleagues that
there are no improvements in the upstream code yet regarding that...

> This modification to the result-conversions test case shows the difference:
>
> --- a/test/src/result-conversions-test.c
> +++ b/test/src/result-conversions-test.c
> @@ -626,6 +626,8 @@ int main(int argc, char **argv)
>         test_conversion("float8", "Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE",
> 20);
>         test_conversion("float8", "-Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE",
> 20);
>
> +       test_conversion("text", "", SQL_C_TYPE_DATE, "SQL_C_TYPE_DATE", 20);
> +
>         /* Clean up */
>         test_disconnect();
>
> We don't currently contain tests like that in the test suite, as mentioned
> in the comment earlier in result-conversions-test.c:

Yes, I can see the difference. I am noticing that only SQL_C_TYPE_DATE
and SQL_C_TYPE_TIMESTAMP are actually converting the non-defined
fields using the localtime values. SQL_C_TYPE_TIME is not impacted at
all. Is that expected?

>>         /*
>>          * Converting arbitrary things to date and timestamp produces
>> results that
>>          * depend on the current timestamp, because the driver substitutes
>> the
>>          * current year/month/datefor missing values. Disable for now, to
>> get a
>>          * reproducible result.
>>          */
>
>
> Would be nice to include those tests, perhaps by printing the difference to
> current date instead of the absolute date.

Yeah, I have done that in the attached with 0001, however I don't
really see how we can actually remove those comments, because some of
the tests include a fixed timestamp, and need to use a fixed
timestamp.

I am attaching patch 0002 that is a rebase of the main patch for HEAD,
that introduces no regressions based on the tests I have added in
0001. 0002 is proving to improve performance of the ODBC driver by
more or less 20% by reducing those localtime() calls in perf specs for
an application that is pretty hot with SQLFetch. That's quite
something.
-- 
Michael

-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc

Attachments: download all (2 files, .tar.gz)

0001-Improve-tests-for-localtime-calls-for-NULL-text-valu.patch (invalid/octet-stream, 10.6K) download From 5e1870ca73ebff51ed00350bad4a17e0b6f99eb2 Mon Sep 17 00:00:00 2001 From: Michael Paquier <michael@otacoo.com> Date: Mon, 25 Apr 2016 14:34:40 +0900 Subject: [PATCH 1/2] Improve tests for localtime() calls for NULL text values The ODBC driver allocates by default values for year, day and month depending on result of localtime() when those fields are not filled by the client during a cast operation. This commit adds some tests to make sure that those code paths are working correctly. --- test/expected/result-conversions.out | 3 + test/src/result-conversions-test.c | 131 +++++++++++++++++++++++------------ 2 files changed, 90 insertions(+), 44 deletions(-) diff --git a/test/expected/result-conversions.out b/test/expected/result-conversions.out index fe1ada3..41bb5ae 100644 --- a/test/expected/result-conversions.out +++ b/test/expected/result-conversions.out @@ -1319,6 +1319,9 @@ Executed: SET bytea_output=hex '2011-02-15 15:49:18' (timestamp) as SQL_C_TIMESTAMP: y: 2011 m: 2 d: 15 h: 15 m: 49 s: 18 f: 0 '2011-02-16 17:49:18+03' (timestamptz) as SQL_C_DATE: y: 2011 m: 2 d: 16 '2011-02-16 17:49:18+03' (timestamptz) as SQL_C_TIMESTAMP: y: 2011 m: 2 d: 16 h: 6 m: 49 s: 18 f: 0 +'' (text) as SQL_C_TYPE_DATE: y: 0 m: 0 d: 0 +'' (text) as SQL_C_TYPE_TIME: h: 0 m: 0 s: 0 +'' (text) as SQL_C_TYPE_TIMESTAMP: y: 0 m: 0 d: 0 h: 0 m: 0 s: 0 f: 0 'foobar' (text) as SQL_C_CHAR: foob (truncated) 'foobar' (text) as SQL_C_CHAR: fooba (truncated) 'foobar' (text) as SQL_C_CHAR: foobar diff --git a/test/src/result-conversions-test.c b/test/src/result-conversions-test.c index 857be1d..1aaaaa2 100644 --- a/test/src/result-conversions-test.c +++ b/test/src/result-conversions-test.c @@ -169,7 +169,7 @@ printdouble(double d) } void -print_sql_type(int sql_c_type, void *buf, SQLLEN strlen_or_ind) +print_sql_type(int sql_c_type, void *buf, SQLLEN strlen_or_ind, int use_time) { switch(sql_c_type) { @@ -225,22 +225,61 @@ print_sql_type(int sql_c_type, void *buf, SQLLEN strlen_or_ind) case SQL_C_TYPE_DATE: { DATE_STRUCT *ds = (DATE_STRUCT *) buf; - printf("y: %d m: %u d: %u", ds->year, ds->month, ds->day); + if (use_time != 0) + { + time_t t = 0; + struct tm *tim; + + t = time(NULL); + tim = localtime(&t); + printf("y: %d m: %u d: %u", + ds->year - (tim->tm_year + 1900), + ds->month - (tim->tm_mon + 1), + ds->day - tim->tm_mday); + } + else + printf("y: %d m: %u d: %u", ds->year, ds->month, ds->day); } break; case SQL_C_TYPE_TIME: { TIME_STRUCT *ts = (TIME_STRUCT *) buf; - printf("h: %d m: %u s: %u", ts->hour, ts->minute, ts->second); + + if (use_time != 0) + { + time_t t = 0; + struct tm *tim; + + t = time(NULL); + tim = localtime(&t); + } + else + printf("h: %d m: %u s: %u", ts->hour, ts->minute, ts->second); } break; case SQL_C_TYPE_TIMESTAMP: { TIMESTAMP_STRUCT *tss = (TIMESTAMP_STRUCT *) buf; - printf("y: %d m: %u d: %u h: %d m: %u s: %u f: %u", - tss->year, tss->month, tss->day, - tss->hour, tss->minute, tss->second, - (unsigned int) tss->fraction); + + if (use_time) + { + time_t t = 0; + struct tm *tim; + + t = time(NULL); + tim = localtime(&t); + printf("y: %d m: %u d: %u h: %d m: %u s: %u f: %u", + tss->year - (tim->tm_year + 1900), + tss->month - (tim->tm_mon + 1), + tss->day - tim->tm_mday, + tss->hour, tss->minute, tss->second, + (unsigned int) tss->fraction); + } + else + printf("y: %d m: %u d: %u h: %d m: %u s: %u f: %u", + tss->year, tss->month, tss->day, + tss->hour, tss->minute, tss->second, + (unsigned int) tss->fraction); } break; case SQL_C_NUMERIC: @@ -426,7 +465,7 @@ get_sql_type_size(int sql_c_type) static char *resultbuf = NULL; void -test_conversion(const char *pgtype, const char *pgvalue, int sqltype, const char *sqltypestr, int buflen) +test_conversion(const char *pgtype, const char *pgvalue, int sqltype, const char *sqltypestr, int buflen, int use_time) { char sql[500]; SQLRETURN rc; @@ -467,7 +506,7 @@ test_conversion(const char *pgtype, const char *pgvalue, int sqltype, const char rc = SQLGetData(hstmt, 1, sqltype, resultbuf, buflen, &len_or_ind); if (SQL_SUCCEEDED(rc)) { - print_sql_type(sqltype, resultbuf, len_or_ind); + print_sql_type(sqltype, resultbuf, len_or_ind, use_time); if (rc == SQL_SUCCESS_WITH_INFO) { @@ -557,7 +596,7 @@ int main(int argc, char **argv) int sqltype = sqltypes[sqltype_i].sqltype; const char *sqltypestr = sqltypes[sqltype_i].str; - test_conversion(pgtype, value, sqltype, sqltypestr, 100); + test_conversion(pgtype, value, sqltype, sqltypestr, 100, 0); } } @@ -573,58 +612,62 @@ int main(int argc, char **argv) * from the hex format separately later. */ exec_cmd("SET bytea_output=hex"); - test_conversion("bytea", "\\x464F4F", SQL_C_CHAR, "SQL_C_CHAR", 100); - test_conversion("bytea", "\\x464F4F", SQL_C_WCHAR, "SQL_C_WCHAR", 100); + test_conversion("bytea", "\\x464F4F", SQL_C_CHAR, "SQL_C_CHAR", 100, 0); + test_conversion("bytea", "\\x464F4F", SQL_C_WCHAR, "SQL_C_WCHAR", 100, 0); /* Conversion to GUID throws error if the string is not of correct form */ - test_conversion("text", "543c5e21-435a-440b-943c-64af1ad571f1", SQL_C_GUID, "SQL_C_GUID", -1); + test_conversion("text", "543c5e21-435a-440b-943c-64af1ad571f1", SQL_C_GUID, "SQL_C_GUID", -1, 0); /* Date/timestamp tests of non-date input depends on current date */ - test_conversion("date", "2011-02-13", SQL_C_TYPE_DATE, "SQL_C_DATE", -1); - test_conversion("date", "2011-02-13", SQL_C_TYPE_TIMESTAMP, "SQL_C_TIMESTAMP", -1); - test_conversion("timestamp", "2011-02-15 15:49:18", SQL_C_TYPE_DATE, "SQL_C_DATE", -1); - test_conversion("timestamp", "2011-02-15 15:49:18", SQL_C_TYPE_TIMESTAMP, "SQL_C_TIMESTAMP", -1); - test_conversion("timestamptz", "2011-02-16 17:49:18+03", SQL_C_TYPE_DATE, "SQL_C_DATE", -1); - test_conversion("timestamptz", "2011-02-16 17:49:18+03", SQL_C_TYPE_TIMESTAMP, "SQL_C_TIMESTAMP", -1); + test_conversion("date", "2011-02-13", SQL_C_TYPE_DATE, "SQL_C_DATE", -1, 0); + test_conversion("date", "2011-02-13", SQL_C_TYPE_TIMESTAMP, "SQL_C_TIMESTAMP", -1, 0); + test_conversion("timestamp", "2011-02-15 15:49:18", SQL_C_TYPE_DATE, "SQL_C_DATE", -1, 0); + test_conversion("timestamp", "2011-02-15 15:49:18", SQL_C_TYPE_TIMESTAMP, "SQL_C_TIMESTAMP", -1, 0); + test_conversion("timestamptz", "2011-02-16 17:49:18+03", SQL_C_TYPE_DATE, "SQL_C_DATE", -1, 0); + test_conversion("timestamptz", "2011-02-16 17:49:18+03", SQL_C_TYPE_TIMESTAMP, "SQL_C_TIMESTAMP", -1, 0); + + /* cast of empty text values using localtime() */ + test_conversion("text", "", SQL_C_TYPE_DATE, "SQL_C_TYPE_DATE", -1, 1); + test_conversion("text", "", SQL_C_TYPE_TIME, "SQL_C_TYPE_TIME", -1, 0); + test_conversion("text", "", SQL_C_TYPE_TIMESTAMP, "SQL_C_TYPE_TIMESTAMP", -1, 1); /* * Test for truncations. */ - test_conversion("text", "foobar", SQL_C_CHAR, "SQL_C_CHAR", 5); - test_conversion("text", "foobar", SQL_C_CHAR, "SQL_C_CHAR", 6); - test_conversion("text", "foobar", SQL_C_CHAR, "SQL_C_CHAR", 7); - test_conversion("text", "foobar", SQL_C_WCHAR, "SQL_C_WCHAR", 10); - test_conversion("text", "foobar", SQL_C_WCHAR, "SQL_C_WCHAR", 11); - test_conversion("text", "foobar", SQL_C_WCHAR, "SQL_C_WCHAR", 12); - test_conversion("text", "foobar", SQL_C_WCHAR, "SQL_C_WCHAR", 13); - test_conversion("text", "foobar", SQL_C_WCHAR, "SQL_C_WCHAR", 14); + test_conversion("text", "foobar", SQL_C_CHAR, "SQL_C_CHAR", 5, 0); + test_conversion("text", "foobar", SQL_C_CHAR, "SQL_C_CHAR", 6, 0); + test_conversion("text", "foobar", SQL_C_CHAR, "SQL_C_CHAR", 7, 0); + test_conversion("text", "foobar", SQL_C_WCHAR, "SQL_C_WCHAR", 10, 0); + test_conversion("text", "foobar", SQL_C_WCHAR, "SQL_C_WCHAR", 11, 0); + test_conversion("text", "foobar", SQL_C_WCHAR, "SQL_C_WCHAR", 12, 0); + test_conversion("text", "foobar", SQL_C_WCHAR, "SQL_C_WCHAR", 13, 0); + test_conversion("text", "foobar", SQL_C_WCHAR, "SQL_C_WCHAR", 14, 0); - test_conversion("text", "", SQL_C_CHAR, "SQL_C_CHAR", 1); - test_conversion("text", "", SQL_C_WCHAR, "SQL_C_WCHAR", 1); + test_conversion("text", "", SQL_C_CHAR, "SQL_C_CHAR", 1, 0); + test_conversion("text", "", SQL_C_WCHAR, "SQL_C_WCHAR", 1, 0); - test_conversion("timestamp", "2011-02-15 15:49:18", SQL_C_CHAR, "SQL_C_CHAR", 19); + test_conversion("timestamp", "2011-02-15 15:49:18", SQL_C_CHAR, "SQL_C_CHAR", 19, 0); /* * Test for a specific bug, where the driver used to overrun the output * buffer because it assumed that a timestamp value is always max 20 bytes * long (not true for BC values, or with years > 10000) */ - test_conversion("timestamp", "2011-02-15 15:49:18 BC", SQL_C_CHAR, "SQL_C_CHAR", 20); - + test_conversion("timestamp", "2011-02-15 15:49:18 BC", SQL_C_CHAR, "SQL_C_CHAR", 20, 0); /* Test special float values */ - test_conversion("float4", "NaN", SQL_C_FLOAT, "SQL_C_FLOAT", 20); - test_conversion("float4", "Infinity", SQL_C_FLOAT, "SQL_C_FLOAT", 20); - test_conversion("float4", "-Infinity", SQL_C_FLOAT, "SQL_C_FLOAT", 20); - test_conversion("float8", "NaN", SQL_C_FLOAT, "SQL_C_FLOAT", 20); - test_conversion("float8", "Infinity", SQL_C_FLOAT, "SQL_C_FLOAT", 20); - test_conversion("float8", "-Infinity", SQL_C_FLOAT, "SQL_C_FLOAT", 20); - test_conversion("float4", "NaN", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20); - test_conversion("float4", "Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20); - test_conversion("float4", "-Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20); - test_conversion("float8", "NaN", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20); - test_conversion("float8", "Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20); - test_conversion("float8", "-Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20); + test_conversion("float4", "NaN", SQL_C_FLOAT, "SQL_C_FLOAT", 20, 0); + test_conversion("float4", "Infinity", SQL_C_FLOAT, "SQL_C_FLOAT", 20, 0); + test_conversion("float4", "-Infinity", SQL_C_FLOAT, "SQL_C_FLOAT", 20, 0); + test_conversion("float8", "NaN", SQL_C_FLOAT, "SQL_C_FLOAT", 20, 0); + test_conversion("float8", "Infinity", SQL_C_FLOAT, "SQL_C_FLOAT", 20, 0); + test_conversion("float8", "-Infinity", SQL_C_FLOAT, "SQL_C_FLOAT", 20, 0); + test_conversion("float4", "NaN", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20, 0); + test_conversion("float4", "Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20, 0); + test_conversion("float4", "-Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20, 0); + test_conversion("float8", "NaN", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20, 0); + test_conversion("float8", "Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20, 0); + test_conversion("float8", "-Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE", 20, 0); /* Clean up */ test_disconnect(); -- 2.8.1
0002-Largely-reduce-overhead-of-localtime-in-SQLFetch.patch (invalid/octet-stream, 4.1K) download From 1b8528952725a7d8d0044fdfca7302c94b733449 Mon Sep 17 00:00:00 2001 From: Michael Paquier <michael@otacoo.com> Date: Mon, 25 Apr 2016 15:16:56 +0900 Subject: [PATCH 2/2] Largely reduce overhead of localtime() in SQLFetch localtime() is a very costly system call causing serious grief on performance of the driver for OLTP-type applications using a lot SQLFetch and this one is being called for all data types when performing an in-line conversion, while it is being used only in the case where some of the date and/or timestamp fields are not initialized. While testing performance of an internal application, this patch has proved to improve the overall performance of an application by 20%. --- convert.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 19 deletions(-) diff --git a/convert.c b/convert.c index 830910a..0d2e07f 100644 --- a/convert.c +++ b/convert.c @@ -953,16 +953,6 @@ copy_and_convert_field(StatementClass *stmt, memset(&std_time, 0, sizeof(SIMPLE_TIME)); - /* Initialize current date */ -#ifdef HAVE_LOCALTIME_R - tim = localtime_r(&stmt_t, &tm); -#else - tim = localtime(&stmt_t); -#endif /* HAVE_LOCALTIME_R */ - std_time.m = tim->tm_mon + 1; - std_time.d = tim->tm_mday; - std_time.y = tim->tm_year + 1900; - mylog("copy_and_convert: field_type = %d, fctype = %d, value = '%s', cbValueMax=%d\n", field_type, fCType, (value == NULL) ? "<NULL>" : value, cbValueMax); if (!value) @@ -1607,6 +1597,25 @@ inolog("2stime fr=%d\n", std_time.fr); ds = (DATE_STRUCT *) rgbValueBindRow; else ds = (DATE_STRUCT *) rgbValue + bind_row; + + /* + * Initialize date in case conversion destination + * expects date part from this source time data. + * A value may be partially set here, so do some + * sanity checks on the existing values before + * setting them. + */ +#ifdef HAVE_LOCALTIME_R + tim = localtime_r(&stmt_t, &tm); +#else + tim = localtime(&stmt_t); +#endif /* HAVE_LOCALTIME_R */ + if (std_time.m == 0) + std_time.m = tim->tm_mon + 1; + if (std_time.d == 0) + std_time.d = tim->tm_mday; + if (std_time.y == 0) + std_time.y = tim->tm_year + 1900; ds->year = std_time.y; ds->month = std_time.m; ds->day = std_time.d; @@ -1639,6 +1648,26 @@ inolog("2stime fr=%d\n", std_time.fr); ts = (TIMESTAMP_STRUCT *) rgbValueBindRow; else ts = (TIMESTAMP_STRUCT *) rgbValue + bind_row; + + /* + * Initialize date in case conversion destination + * expects date part from this source time data. + * A value may be partially set here, so do some + * sanity checks on the existing values before + * setting them. + */ +#ifdef HAVE_LOCALTIME_R + tim = localtime_r(&stmt_t, &tm); +#else + tim = localtime(&stmt_t); +#endif /* HAVE_LOCALTIME_R */ + if (std_time.m == 0) + std_time.m = tim->tm_mon + 1; + if (std_time.d == 0) + std_time.d = tim->tm_mday; + if (std_time.y == 0) + std_time.y = tim->tm_year + 1900; + ts->year = std_time.y; ts->month = std_time.m; ts->day = std_time.d; @@ -4173,14 +4202,6 @@ inolog("ipara=%p paramType=%d %d proc_return=%d\n", ipara, ipara ? ipara->paramT cbuf[0] = '\0'; memset(&st, 0, sizeof(st)); t = SC_get_time(qb->stmt); -#ifdef HAVE_LOCALTIME_R - tim = localtime_r(&t, &tm); -#else - tim = localtime(&t); -#endif /* HAVE_LOCALTIME_R */ - st.m = tim->tm_mon + 1; - st.d = tim->tm_mday; - st.y = tim->tm_year + 1900; ivstruct = (SQL_INTERVAL_STRUCT *) buffer; /* Convert input C type to a neutral format */ @@ -4326,7 +4347,19 @@ mylog("C_WCHAR=%s(%d)\n", buffer, used); st.hh = ts->hour; st.mm = ts->minute; st.ss = ts->second; - + /* + * Initialize date in case conversion destination + * expects date part from this source time data. + */ + t = SC_get_time(qb->stmt); +#ifdef HAVE_LOCALTIME_R + tim = localtime_r(&t, &tm); +#else + tim = localtime(&t); +#endif /* HAVE_LOCALTIME_R */ + st.m = tim->tm_mon + 1; + st.d = tim->tm_mday; + st.y = tim->tm_year + 1900; break; } -- 2.8.1
^ permalink raw reply [nested|flat] 7+ messages in thread

* Re: [PATCH] Remove unncessary localtime() calls during data type conversion
@ 2016-04-28 22:58  Inoue, Hiroshi <h-inoue@dream.email.ne.jp>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Inoue, Hiroshi @ 2016-04-28 22:58 UTC (permalink / raw)
  To: Michael Paquier <michael.paquier@gmail.com>; Heikki Linnakangas <hlinnaka@iki.fi>; +Cc: Nikhil Deshpande <ndeshpande@vmware.com>; pgsql-odbc

Hi Michael,

Sorry for the late reply.

On 2016/04/25 15:30, Michael Paquier wrote:
> On Tue, Sep 1, 2015 at 6:11 PM, Heikki Linnakangas <hlinnaka@iki.fi> wrote:
>> This changes behaviour, when converting from text to date column. Without
>> this patch, if the string you're converting from is missing a
>> year/month/date field, it will be filled with the current date.
> Waking up an old thread, after being reminded by some colleagues that
> there are no improvements in the upstream code yet regarding that...
>
>> This modification to the result-conversions test case shows the difference:
>>
>> --- a/test/src/result-conversions-test.c
>> +++ b/test/src/result-conversions-test.c
>> @@ -626,6 +626,8 @@ int main(int argc, char **argv)
>>          test_conversion("float8", "Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE",
>> 20);
>>          test_conversion("float8", "-Infinity", SQL_C_DOUBLE, "SQL_C_DOUBLE",
>> 20);
>>
>> +       test_conversion("text", "", SQL_C_TYPE_DATE, "SQL_C_TYPE_DATE", 20);
>> +
>>          /* Clean up */
>>          test_disconnect();
>>
>> We don't currently contain tests like that in the test suite, as mentioned
>> in the comment earlier in result-conversions-test.c:
> Yes, I can see the difference. I am noticing that only SQL_C_TYPE_DATE
> and SQL_C_TYPE_TIMESTAMP are actually converting the non-defined
> fields using the localtime values. SQL_C_TYPE_TIME is not impacted at
> all. Is that expected?
>
>>>          /*
>>>           * Converting arbitrary things to date and timestamp produces
>>> results that
>>>           * depend on the current timestamp, because the driver substitutes
>>> the
>>>           * current year/month/datefor missing values. Disable for now, to
>>> get a
>>>           * reproducible result.
>>>           */
>>
>> Would be nice to include those tests, perhaps by printing the difference to
>> current date instead of the absolute date.
> Yeah, I have done that in the attached with 0001, however I don't
> really see how we can actually remove those comments, because some of
> the tests include a fixed timestamp, and need to use a fixed
> timestamp.
>
> I am attaching patch 0002 that is a rebase of the main patch for HEAD,
> that introduces no regressions based on the tests I have added in
> 0001. 0002 is proving to improve performance of the ODBC driver by
> more or less 20% by reducing those localtime() calls in perf specs for
> an application that is pretty hot with SQLFetch. That's quite
> something.

I would take care of  the patches.
Thanks.

regards,
Hiroshi Inoue


-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [PATCH] Remove unncessary localtime() calls during data type conversion
@ 2016-04-29 00:15  Michael Paquier <michael.paquier@gmail.com>
  parent: Inoue, Hiroshi <h-inoue@dream.email.ne.jp>
  0 siblings, 1 reply; 7+ messages in thread

From: Michael Paquier @ 2016-04-29 00:15 UTC (permalink / raw)
  To: Inoue, Hiroshi <h-inoue@dream.email.ne.jp>; +Cc: Heikki Linnakangas <hlinnaka@iki.fi>; Nikhil Deshpande <ndeshpande@vmware.com>; pgsql-odbc

Hi Inoue-san,

On Fri, Apr 29, 2016 at 7:58 AM, Inoue, Hiroshi
<h-inoue@dream.email.ne.jp> wrote:
> On 2016/04/25 15:30, Michael Paquier wrote:
>> I am attaching patch 0002 that is a rebase of the main patch for HEAD,
>> that introduces no regressions based on the tests I have added in
>> 0001. 0002 is proving to improve performance of the ODBC driver by
>> more or less 20% by reducing those localtime() calls in perf specs for
>> an application that is pretty hot with SQLFetch. That's quite
>> something.
>
> I would take care of  the patches.

Thanks! If you need any help regarding those things, just don't
hesitate to ping me. I can easily allocate even company time for this
stuff.
-- 
Michael


-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: [PATCH] Remove unncessary localtime() calls during data type conversion
@ 2016-06-16 12:51  Michael Paquier <michael.paquier@gmail.com>
  parent: Michael Paquier <michael.paquier@gmail.com>
  0 siblings, 0 replies; 7+ messages in thread

From: Michael Paquier @ 2016-06-16 12:51 UTC (permalink / raw)
  To: Inoue, Hiroshi <h-inoue@dream.email.ne.jp>; +Cc: Heikki Linnakangas <hlinnaka@iki.fi>; Nikhil Deshpande <ndeshpande@vmware.com>; pgsql-odbc

On Fri, Apr 29, 2016 at 9:15 AM, Michael Paquier
<michael.paquier@gmail.com> wrote:
> Thanks! If you need any help regarding those things, just don't
> hesitate to ping me. I can easily allocate even company time for this
> stuff.

I am really late at the party here... My two patches has been
committed as 34cdbd1f and c2794dbb. Thanks Inoue-san for pushing them!
-- 
Michael


-- 
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbc



^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2016-06-16 12:51 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2015-06-13 00:42 [PATCH] Remove unncessary localtime() calls during data type conversion Nikhil Deshpande <ndeshpande@vmware.com>
2015-06-14 03:36 ` Michael Paquier <michael.paquier@gmail.com>
2015-09-01 09:11   ` Heikki Linnakangas <hlinnaka@iki.fi>
2016-04-25 06:30     ` Michael Paquier <michael.paquier@gmail.com>
2016-04-28 22:58       ` Inoue, Hiroshi <h-inoue@dream.email.ne.jp>
2016-04-29 00:15         ` Michael Paquier <michael.paquier@gmail.com>
2016-06-16 12:51           ` Michael Paquier <michael.paquier@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox