pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pg_dump sometimes misses sequence parameters
4+ messages / 2 participants
[nested] [flat]

Commits from this thread:
  2018-02-20 6753f6c419 3486bcf9e8  Fix pg_dump's logic for eliding sequence limits that match the defaults.
* pg_dump sometimes misses sequence parameters
@ 2018-02-19 13:32 Alexey Bashtanov <bashtanov@imap.cc>
  2018-02-19 16:49 ` Re: pg_dump sometimes misses sequence parameters Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Alexey Bashtanov @ 2018-02-19 13:32 UTC (permalink / raw)
  To: pgsql-bugs

The bug affects REL_10_STABLE and master branches.
9.4..9.6 are unaffected.

To reproduce:
psql -c 'DROP SEQUENCE IF EXISTS foo'
psql -c 'CREATE SEQUENCE foo INCREMENT BY -1 MINVALUE 
-9223372036854775808 MAXVALUE 9223372036854775807'
pg_dump -t foo > tmp
psql -c 'DROP SEQUENCE foo'
psql <tmp

The last psql call fails with "START value (9223372036854775807) cannot 
be greater than MAXVALUE (-1)",
as pg_dump does not record MAXVALUE properly.

The reason is atoi is used and those large numbers are interpreted as 0 
and -1 respectively.
I'd propose to use atol instead of atoi, please see the patch attached.

Best,
   Alexey

Attachments:

pgdump_seq.diff (text/x-patch, 1.4K) download diff --git a/src/bin/pg_dump/pg_dump.c b/src/bin/pg_dump/pg_dump.c new file mode 100644 index 06bbc50..d9d331e *** a/src/bin/pg_dump/pg_dump.c --- b/src/bin/pg_dump/pg_dump.c *************** dumpSequence(Archive *fout, TableInfo *t *** 16914,16936 **** 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) --- 16914,16936 ---- is_ascending = incby[0] != '-'; ! if (is_ascending && atol(minv) == 1) minv = NULL; ! if (!is_ascending && atol(maxv) == -1) maxv = NULL; if (strcmp(seqtype, "smallint") == 0) { ! if (!is_ascending && atol(minv) == PG_INT16_MIN) minv = NULL; ! if (is_ascending && atol(maxv) == PG_INT16_MAX) maxv = NULL; } else if (strcmp(seqtype, "integer") == 0) { ! if (!is_ascending && atol(minv) == PG_INT32_MIN) minv = NULL; ! if (is_ascending && atol(maxv) == PG_INT32_MAX) maxv = NULL; } else if (strcmp(seqtype, "bigint") == 0)
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: pg_dump sometimes misses sequence parameters
  2018-02-19 13:32 pg_dump sometimes misses sequence parameters Alexey Bashtanov <bashtanov@imap.cc>
@ 2018-02-19 16:49 ` Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-20 14:06   ` Re: pg_dump sometimes misses sequence parameters Alexey Bashtanov <bashtanov@imap.cc>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2018-02-19 16:49 UTC (permalink / raw)
  To: Alexey Bashtanov <bashtanov@imap.cc>; +Cc: pgsql-bugs@postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

Alexey Bashtanov <bashtanov@imap.cc> writes:
> The bug affects REL_10_STABLE and master branches.
> 9.4..9.6 are unaffected.

> To reproduce:
> psql -c 'DROP SEQUENCE IF EXISTS foo'
> psql -c 'CREATE SEQUENCE foo INCREMENT BY -1 MINVALUE 
> -9223372036854775808 MAXVALUE 9223372036854775807'
> pg_dump -t foo > tmp
> psql -c 'DROP SEQUENCE foo'
> psql <tmp

> The last psql call fails with "START value (9223372036854775807) cannot 
> be greater than MAXVALUE (-1)",
> as pg_dump does not record MAXVALUE properly.

Ugh.

> The reason is atoi is used and those large numbers are interpreted as 0 
> and -1 respectively.
> I'd propose to use atol instead of atoi, please see the patch attached.

That will only fix it on platforms where long is 64 bits.  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;

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.

			regards, tom lane




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

* Re: pg_dump sometimes misses sequence parameters
  2018-02-19 13:32 pg_dump sometimes misses sequence parameters Alexey Bashtanov <bashtanov@imap.cc>
  2018-02-19 16:49 ` Re: pg_dump sometimes misses sequence parameters Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-02-20 14:06   ` Alexey Bashtanov <bashtanov@imap.cc>
  2018-02-20 16:39     ` Re: pg_dump sometimes misses sequence parameters Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Alexey Bashtanov @ 2018-02-20 14:06 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs@postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

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
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: pg_dump sometimes misses sequence parameters
  2018-02-19 13:32 pg_dump sometimes misses sequence parameters Alexey Bashtanov <bashtanov@imap.cc>
  2018-02-19 16:49 ` Re: pg_dump sometimes misses sequence parameters Tom Lane <tgl@sss.pgh.pa.us>
  2018-02-20 14:06   ` Re: pg_dump sometimes misses sequence parameters Alexey Bashtanov <bashtanov@imap.cc>
@ 2018-02-20 16:39     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2018-02-20 16:39 UTC (permalink / raw)
  To: Alexey Bashtanov <bashtanov@imap.cc>; +Cc: pgsql-bugs@postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>

Alexey Bashtanov <bashtanov@imap.cc> writes:
> 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.

Good idea ...

> Choosing between atol/atoll vs snprintf+strcmp,
> I preferred the former, as the latter is slower and uglier to me.

... but I was not thrilled with the way you did that.  I don't like
having to back-patch new portability requirements with no time for them
to go through beta testing.  What's more, this is randomly unlike the way
we're doing things elsewhere.  I call your attention to pg_strtouint64
in src/backend/utils/adt/numutils.c.

What I did for the moment was to implement the comparison using
snprintf+strcmp, since that seemed like a very safely back-patchable
approach.  While there's nothing particularly wrong with leaving
it like that, we could imagine creating a src/port module that
contains pg_strtouint64 and a corresponding implementation of
pg_strtoint64, and then fixing pg_dump to use pg_strtoint64 here.
I'd only consider doing that in HEAD though.

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

The easy way is just to omit the configure file from the submitted
patch, noting that the committer must run autoconf.  Most committers
probably would do that anyway (I certainly would).

However, if you want to do it right, or if you're someday given a
commit bit and you have to do it right, our project policy is that
we use exactly the GNU-released autoconf of the appropriate version,
so that anyone can reproduce the results, independently of patches
that their distribution might install into their platform's copy of
autoconf.  So just pull the right autoconf tarball from any handy
GNU archive mirror, "configure --prefix=/something; make; make install",
prepend /something/bin to your PATH, and away you go.  Personally
I keep autoconf N.NN in /usr/local/autoconf-N.NN/ so that I can use
whichever version is appropriate for the PG branch I'm working on.

			regards, tom lane




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


end of thread, other threads:[~2018-02-20 16:39 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-02-19 13:32 pg_dump sometimes misses sequence parameters Alexey Bashtanov <bashtanov@imap.cc>
2018-02-19 16:49 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-02-20 14:06   ` Alexey Bashtanov <bashtanov@imap.cc>
2018-02-20 16:39     ` Tom Lane <tgl@sss.pgh.pa.us>

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