Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eo8Yz-0003kh-O1 for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Feb 2018 14:06:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eo8Yw-0003fM-Jy for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Feb 2018 14:06:30 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eo8Yv-0003fB-Vf for pgsql-bugs@lists.postgresql.org; Tue, 20 Feb 2018 14:06:30 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eo8Yp-0005J9-9k for pgsql-bugs@postgresql.org; Tue, 20 Feb 2018 14:06:29 +0000 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 1E411215C2; Tue, 20 Feb 2018 09:06:20 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute2.internal (MEProxy); Tue, 20 Feb 2018 09:06:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=imap.cc; h=cc :content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; bh=dKRCvFoMoqBX5vH+T9r8ParIl6luWKXMXZSHSQ3qXWA=; b=HRvBzblk 5UQ3LhVuLhFt4yaS/31JtNvrAMmsaQqXvrmevuvpzYFn4/sleNEAyEgLiiXM/liZ 8vLBK5tRaAYFnj8O3vE5RPfsLAo+uAoLHTHvS/gpY2Y7QFJ9ymyzZjnJirpIyG+q 0coAZQNNjl6PXIhFMiVo7yNLw+ro4paBGCiVBruSuGQXaMAoo89lA+EmesLVw4uN r41OTSMSaKsrf0QNRWkJq4rhQnwUBIEa0pkLBcudx/9XeJLB7nCrF6TthwJDhcl/ hzEbFB5KURJiCIEhVETf/AF0cnqTtfEjDwdFWbAzV7ab1TYGv9qd5UjsnbOCeS+A XFoiGPH/ftHGGg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=dKRCvFoMoqBX5vH+T9r8ParIl6luW KXMXZSHSQ3qXWA=; b=NF2MoU7U1GUTKtXKtUU82TeFAAlq7adJ7fFPRTnv1gjoj D5ivfABQfXesn6PcoaCMVFU9WbUrE4Y+xDbWVa3j92IsB5cYTu7NMAmgcvvF7yyj qBtKXZM9GZGVJbhGOHZbKu0T/Of9KaTojdrzmWcv4NozzOvWbBoMfzqWVtX6Q6eW lLc7+3vtuEdItE+1JU/YKXckdkBnmGu4V/vF89pDbrDh0raFs2qUT+dd9GcCbUs1 f26TCi/7AS0CGuIQmXD/wa8O1MQuJ0MuhnjrlOjo5Fgwgm0xqSYeQVvpcf3cp3kq IvkfFgdCsSArvTSURMvrKCKBI6siD7gHo2t2ZHqTQ== X-ME-Sender: Received: from [10.2.33.124] (unknown [185.25.33.73]) by mail.messagingengine.com (Postfix) with ESMTPA id 627007E3C8; Tue, 20 Feb 2018 09:06:19 -0500 (EST) Subject: Re: pg_dump sometimes misses sequence parameters To: Tom Lane Cc: pgsql-bugs@postgresql.org, Peter Eisentraut References: <16276.1519058985@sss.pgh.pa.us> From: Alexey Bashtanov Message-ID: Date: Tue, 20 Feb 2018 14:06:10 +0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: <16276.1519058985@sss.pgh.pa.us> Content-Type: multipart/mixed; boundary="------------5DB00F1F5E1EF793CEBD8CE0" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------5DB00F1F5E1EF793CEBD8CE0 Content-Type: multipart/alternative; boundary="------------A0E96015C8ABB4CF5601245E" --------------A0E96015C8ABB4CF5601245E Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Hi Tom, > That will only fix it on platforms where long is 64 bits. Oops. I erroneously thought its size could drift the other way. > I think that converting these strings to integer at all is a dumb > idea.  It would be > much better to write the first two tests like > >     if (is_ascending && strcmp(minv, "1") == 0) >         minv = NULL; >     if (!is_ascending && strcmp(maxv, "-1") == 0) >         maxv = NULL; Agree for those two. However, I don't see it as a substantial speedup or simplification if we anyway do the conversions below. > It's tempting to try to make the remaining tests look similar, but > I'm not quite sure how without writing out the exact decimal values > of the constants, which probably isn't better.  I experimented with > stuff like "strcmp(minv, CppAsString2(PG_INT32_MIN))" but that > doesn't quite do what we need. I didn't manage to find a snprintf-like thing implemented in C preprocessor language either, and I don't think it would be easy to write one. Unwilling to define the decimal representations constants, we have to convert those strings representations to binary ones (in case of smallint/integer sequences) or visa versa (bigint). Current code looks somewhat fragile to me, as it takes some time to assure it doesn't pass already nulled out minv or maxv to atoi or strcmp again. I've rewritten it in a bit more clear way, so each comparison and nulling out happens only once. Choosing between atol/atoll vs snprintf+strcmp, I preferred the former, as the latter is slower and uglier to me. Should we need any further speedup, I'd go for decimal representation constants really. BTW what should I do when submitting a patch if my autoconf adds some irrelevant changes? Best regards,   Alexey Bashtanov --------------A0E96015C8ABB4CF5601245E Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit Hi Tom,

That will only fix it on platforms where long is 64 bits.
Oops. I erroneously thought its size could drift the other way.

I think that converting these strings to integer at all is a dumb idea.  It would be
much better to write the first two tests like

    if (is_ascending && strcmp(minv, "1") == 0)
        minv = NULL;
    if (!is_ascending && strcmp(maxv, "-1") == 0)
        maxv = NULL;
Agree for those two. However, I don't see it as a substantial speedup
or simplification if we anyway do the conversions below.

It's tempting to try to make the remaining tests look similar, but
I'm not quite sure how without writing out the exact decimal values
of the constants, which probably isn't better.  I experimented with
stuff like "strcmp(minv, CppAsString2(PG_INT32_MIN))" but that
doesn't quite do what we need.
I didn't manage to find a snprintf-like thing implemented in C
preprocessor language either, and I don't think it would be easy
to write one.

Unwilling to define the decimal representations constants, we have to
convert those strings representations to binary ones
(in case of smallint/integer sequences) or visa versa (bigint).

Current code looks somewhat fragile to me, as it takes some time to assure
it doesn't pass already nulled out minv or maxv to atoi or strcmp again.

I've rewritten it in a bit more clear way, so each comparison and nulling out
happens only once. Choosing between atol/atoll vs snprintf+strcmp,
I preferred the former, as the latter is slower and uglier to me.

Should we need any further speedup, I'd go for decimal representation
constants really.

BTW what should I do when submitting a patch if my autoconf adds
some irrelevant changes?

Best regards,
  Alexey Bashtanov --------------A0E96015C8ABB4CF5601245E-- --------------5DB00F1F5E1EF793CEBD8CE0 Content-Type: text/x-patch; name="pgdump_seq_v2.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="pgdump_seq_v2.diff" diff --git a/configure b/configure new file mode 100755 index 7dcca50..d6945eb *** a/configure --- b/configure *************** else *** 12655,12661 **** We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ ! #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; --- 12655,12661 ---- We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ ! #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; *************** else *** 12701,12707 **** We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ ! #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; --- 12701,12707 ---- We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ ! #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; *************** rm -f core conftest.err conftest.$ac_obj *** 12725,12731 **** We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ ! #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; --- 12725,12731 ---- We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ ! #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; *************** else *** 12770,12776 **** We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ ! #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; --- 12770,12776 ---- We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ ! #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; *************** rm -f core conftest.err conftest.$ac_obj *** 12794,12800 **** We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ ! #define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; --- 12794,12800 ---- We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ ! #define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; *************** $as_echo "#define HAVE_INT_OPTRESET 1" > *** 13962,13967 **** --- 13962,13979 ---- fi + for ac_func in atoll atoq + do : + as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` + ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" + if eval test \"x\$"$as_ac_var"\" = x"yes"; then : + cat >>confdefs.h <<_ACEOF + #define `$as_echo "HAVE_$ac_func" | $as_tr_cpp` 1 + _ACEOF + break + fi + done + for ac_func in strtoll strtoq do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` diff --git a/configure.in b/configure.in new file mode 100644 index 4d26034..1feb1e0 *** a/configure.in --- b/configure.in *************** if test x"$pgac_cv_var_int_optreset" = x *** 1646,1651 **** --- 1646,1652 ---- AC_DEFINE(HAVE_INT_OPTRESET, 1, [Define to 1 if you have the global variable 'int optreset'.]) fi + AC_CHECK_FUNCS([atoll atoq], [break]) AC_CHECK_FUNCS([strtoll strtoq], [break]) AC_CHECK_FUNCS([strtoull strtouq], [break]) diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c new file mode 100644 index 06bbc50..689e4dc *** a/src/bin/pg_dump/pg_dump.c --- b/src/bin/pg_dump/pg_dump.c *************** dumpSequence(Archive *fout, TableInfo *t *** 16843,16848 **** --- 16843,16850 ---- *seqtype; bool cycled; bool is_ascending; + int64 default_minv, + default_maxv; PQExpBuffer query = createPQExpBuffer(); PQExpBuffer delqry = createPQExpBuffer(); PQExpBuffer labelq = createPQExpBuffer(); *************** dumpSequence(Archive *fout, TableInfo *t *** 16914,16951 **** is_ascending = incby[0] != '-'; ! if (is_ascending && atoi(minv) == 1) ! minv = NULL; ! if (!is_ascending && atoi(maxv) == -1) ! maxv = NULL; ! if (strcmp(seqtype, "smallint") == 0) { ! if (!is_ascending && atoi(minv) == PG_INT16_MIN) ! minv = NULL; ! if (is_ascending && atoi(maxv) == PG_INT16_MAX) ! maxv = NULL; } else if (strcmp(seqtype, "integer") == 0) { ! if (!is_ascending && atoi(minv) == PG_INT32_MIN) ! minv = NULL; ! if (is_ascending && atoi(maxv) == PG_INT32_MAX) ! maxv = NULL; } else if (strcmp(seqtype, "bigint") == 0) { ! char bufm[100], ! bufx[100]; ! ! snprintf(bufm, sizeof(bufm), INT64_FORMAT, PG_INT64_MIN); ! snprintf(bufx, sizeof(bufx), INT64_FORMAT, PG_INT64_MAX); ! ! if (!is_ascending && strcmp(minv, bufm) == 0) ! minv = NULL; ! if (is_ascending && strcmp(maxv, bufx) == 0) ! maxv = NULL; } /* * DROP must be fully qualified in case same name appears in pg_catalog --- 16916,16947 ---- is_ascending = incby[0] != '-'; ! /* Calculate default limits for a sequence of this kind */ if (strcmp(seqtype, "smallint") == 0) { ! default_minv = is_ascending ? 1 : PG_INT16_MIN; ! default_maxv = is_ascending ? PG_INT16_MAX : -1; } else if (strcmp(seqtype, "integer") == 0) { ! default_minv = is_ascending ? 1 : PG_INT32_MIN; ! default_maxv = is_ascending ? PG_INT32_MAX : -1; } else if (strcmp(seqtype, "bigint") == 0) { ! default_minv = is_ascending ? 1 : PG_INT64_MIN; ! default_maxv = is_ascending ? PG_INT64_MAX : -1; } + else + { + exit_horribly(NULL, "unrecognized sequence type: %s\n", + seqtype); + } + + if (atoint64(minv) == default_minv) + minv = NULL; + if (atoint64(maxv) == default_maxv) + maxv = NULL; /* * DROP must be fully qualified in case same name appears in pg_catalog diff --git a/src/include/c.h b/src/include/c.h new file mode 100644 index 37c0c39..4ff57f3 *** a/src/include/c.h --- b/src/include/c.h *************** typedef unsigned long int uint64; *** 336,341 **** --- 336,342 ---- #endif #define INT64CONST(x) (x##L) #define UINT64CONST(x) (x##UL) + #define atoint64 atol #elif defined(HAVE_LONG_LONG_INT_64) /* We have working support for "long long int", use that */ *************** typedef unsigned long long int uint64; *** 347,352 **** --- 348,354 ---- #endif #define INT64CONST(x) (x##LL) #define UINT64CONST(x) (x##ULL) + #define atoint64 atoll #else /* neither HAVE_LONG_INT_64 nor HAVE_LONG_LONG_INT_64 */ #error must have a working 64-bit integer datatype *************** extern int vsnprintf(char *str, size_t c *** 1125,1130 **** --- 1127,1138 ---- extern int fdatasync(int fildes); #endif + /* If atoq() exists, rename it to the more standard atoll() */ + #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_ATOLL) && defined(HAVE_ATOQ) + #define atoll atoq + #define HAVE_ATOLL 1 + #endif + /* If strtoq() exists, rename it to the more standard strtoll() */ #if defined(HAVE_LONG_LONG_INT_64) && !defined(HAVE_STRTOLL) && defined(HAVE_STRTOQ) #define strtoll strtoq diff --git a/src/include/pg_config.h.in b/src/include/pg_config.h.in new file mode 100644 index f98f773..281756a *** a/src/include/pg_config.h.in --- b/src/include/pg_config.h.in *************** *** 90,101 **** --- 90,107 ---- /* Define to 1 if you have the `ASN1_STRING_get0_data' function. */ #undef HAVE_ASN1_STRING_GET0_DATA + /* Define to 1 if you have the `atoll' function. */ + #undef HAVE_ATOLL + /* Define to 1 if you want to use atomics if available. */ #undef HAVE_ATOMICS /* Define to 1 if you have the header file. */ #undef HAVE_ATOMIC_H + /* Define to 1 if you have the `atoq' function. */ + #undef HAVE_ATOQ + /* Define to 1 if you have the `BIO_get_data' function. */ #undef HAVE_BIO_GET_DATA --------------5DB00F1F5E1EF793CEBD8CE0--