pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alexey Bashtanov <bashtanov@imap.cc>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-bugs@postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Subject: Re: pg_dump sometimes misses sequence parameters
Date: Tue, 20 Feb 2018 14:06:10 +0000
Message-ID: <ad692667-fe43-62c5-5e55-e40a8596b621@imap.cc> (raw)
In-Reply-To: <16276.1519058985@sss.pgh.pa.us>
References: <cb85a9a5-946b-c7c4-9cf2-6cd6e25d7a33@imap.cc>
	<16276.1519058985@sss.pgh.pa.us>

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

Attachments:

pgdump_seq_v2.diff (text/x-patch, 9.5K) download 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 <atomic.h> 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

view thread (4+ messages)  latest in thread

Message-ID: <ad692667-fe43-62c5-5e55-e40a8596b621@imap.cc>
Permalink:  ../ad692667-fe43-62c5-5e55-e40a8596b621@imap.cc/
Also on:    postgresql.org/message-id/ad692667-fe43-62c5-5e55-e40a8596b621@imap.cc

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: bashtanov@imap.cc, tgl@sss.pgh.pa.us, peter.eisentraut@2ndquadrant.com
  Subject: Re: pg_dump sometimes misses sequence parameters
  In-Reply-To: <ad692667-fe43-62c5-5e55-e40a8596b621@imap.cc>
  References: <cb85a9a5-946b-c7c4-9cf2-6cd6e25d7a33@imap.cc> <16276.1519058985@sss.pgh.pa.us> <ad692667-fe43-62c5-5e55-e40a8596b621@imap.cc>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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