pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
To: Michael Paquier <michael@paquier.xyz>
Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>
Cc: evgeni-klimov@yandex.ru
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
Date: Wed, 7 Mar 2018 15:16:44 -0500
Message-ID: <d9f1443c-9006-3705-1e1b-7b65301dae42@2ndquadrant.com> (raw)
In-Reply-To: <26456.1520278713@sss.pgh.pa.us>
References: <152009601619.21242.262498029671910378@wrigleys.postgresql.org>
	<31194.1520098424@sss.pgh.pa.us>
	<87po4lkkaj.fsf@news-spur.riddles.org.uk>
	<20180304235654.GA32165@paquier.xyz>
	<87lgf7l6sj.fsf@news-spur.riddles.org.uk>
	<21016.1520228672@sss.pgh.pa.us>
	<20180305061325.GF2266@paquier.xyz>
	<26456.1520278713@sss.pgh.pa.us>

Here is a patch that fixes this bug, and a second patch (not for
backpatching) that changes the Value node to use int instead of long.

-- 
Peter Eisentraut              http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services

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

0001-Fix-CREATE-TABLE-LIKE-with-bigint-identity-column.patch (text/plain, 6.0K) download From 132de6de3bd5e429069169cc3572332300eb2197 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <peter_e@gmx.net> Date: Wed, 7 Mar 2018 14:38:35 -0500 Subject: [PATCH 1/2] Fix CREATE TABLE / LIKE with bigint identity column CREATE TABLE / LIKE with a bigint identity column would fail on platforms where long is 32 bits. Copying the sequence values used makeInteger(), which would truncate the 64-bit sequence data to 32 bits. To fix, use makeFloat() instead, like the parser. (This does not actually make use of floats, but stores the values as strings.) Bug: #15096 --- src/backend/commands/sequence.c | 19 +++++++++++------ src/test/regress/expected/create_table_like.out | 28 ++++++++++++------------- src/test/regress/sql/create_table_like.sql | 2 +- 3 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/backend/commands/sequence.c b/src/backend/commands/sequence.c index ef3ca8c00b..dcc0aa536a 100644 --- a/src/backend/commands/sequence.c +++ b/src/backend/commands/sequence.c @@ -1752,12 +1752,19 @@ sequence_options(Oid relid) elog(ERROR, "cache lookup failed for sequence %u", relid); pgsform = (Form_pg_sequence) GETSTRUCT(pgstuple); - options = lappend(options, makeDefElem("cache", (Node *) makeInteger(pgsform->seqcache), -1)); - options = lappend(options, makeDefElem("cycle", (Node *) makeInteger(pgsform->seqcycle), -1)); - options = lappend(options, makeDefElem("increment", (Node *) makeInteger(pgsform->seqincrement), -1)); - options = lappend(options, makeDefElem("maxvalue", (Node *) makeInteger(pgsform->seqmax), -1)); - options = lappend(options, makeDefElem("minvalue", (Node *) makeInteger(pgsform->seqmin), -1)); - options = lappend(options, makeDefElem("start", (Node *) makeInteger(pgsform->seqstart), -1)); + /* Use makeFloat() for 64-bit integers, like gram.y does. */ + options = lappend(options, + makeDefElem("cache", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqcache)), -1)); + options = lappend(options, + makeDefElem("cycle", (Node *) makeInteger(pgsform->seqcycle), -1)); + options = lappend(options, + makeDefElem("increment", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqincrement)), -1)); + options = lappend(options, + makeDefElem("maxvalue", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqmax)), -1)); + options = lappend(options, + makeDefElem("minvalue", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqmin)), -1)); + options = lappend(options, + makeDefElem("start", (Node *) makeFloat(psprintf(INT64_FORMAT, pgsform->seqstart)), -1)); ReleaseSysCache(pgstuple); diff --git a/src/test/regress/expected/create_table_like.out b/src/test/regress/expected/create_table_like.out index 52ff18c8ca..8d4543bfe8 100644 --- a/src/test/regress/expected/create_table_like.out +++ b/src/test/regress/expected/create_table_like.out @@ -66,13 +66,13 @@ SELECT * FROM inhg; /* Two records with three columns in order x=x, xx=text, y=y (2 rows) DROP TABLE inhg; -CREATE TABLE test_like_id_1 (a int GENERATED ALWAYS AS IDENTITY, b text); +CREATE TABLE test_like_id_1 (a bigint GENERATED ALWAYS AS IDENTITY, b text); \d test_like_id_1 Table "public.test_like_id_1" - Column | Type | Collation | Nullable | Default ---------+---------+-----------+----------+------------------------------ - a | integer | | not null | generated always as identity - b | text | | | + Column | Type | Collation | Nullable | Default +--------+--------+-----------+----------+------------------------------ + a | bigint | | not null | generated always as identity + b | text | | | INSERT INTO test_like_id_1 (b) VALUES ('b1'); SELECT * FROM test_like_id_1; @@ -83,11 +83,11 @@ SELECT * FROM test_like_id_1; CREATE TABLE test_like_id_2 (LIKE test_like_id_1); \d test_like_id_2 - Table "public.test_like_id_2" - Column | Type | Collation | Nullable | Default ---------+---------+-----------+----------+--------- - a | integer | | not null | - b | text | | | + Table "public.test_like_id_2" + Column | Type | Collation | Nullable | Default +--------+--------+-----------+----------+--------- + a | bigint | | not null | + b | text | | | INSERT INTO test_like_id_2 (b) VALUES ('b2'); ERROR: null value in column "a" violates not-null constraint @@ -100,10 +100,10 @@ SELECT * FROM test_like_id_2; -- identity was not copied CREATE TABLE test_like_id_3 (LIKE test_like_id_1 INCLUDING IDENTITY); \d test_like_id_3 Table "public.test_like_id_3" - Column | Type | Collation | Nullable | Default ---------+---------+-----------+----------+------------------------------ - a | integer | | not null | generated always as identity - b | text | | | + Column | Type | Collation | Nullable | Default +--------+--------+-----------+----------+------------------------------ + a | bigint | | not null | generated always as identity + b | text | | | INSERT INTO test_like_id_3 (b) VALUES ('b3'); SELECT * FROM test_like_id_3; -- identity was copied and applied diff --git a/src/test/regress/sql/create_table_like.sql b/src/test/regress/sql/create_table_like.sql index 32d861621b..42cad6826b 100644 --- a/src/test/regress/sql/create_table_like.sql +++ b/src/test/regress/sql/create_table_like.sql @@ -37,7 +37,7 @@ CREATE TABLE inhg (x text, LIKE inhx INCLUDING CONSTRAINTS, y text); /* Copies c SELECT * FROM inhg; /* Two records with three columns in order x=x, xx=text, y=y */ DROP TABLE inhg; -CREATE TABLE test_like_id_1 (a int GENERATED ALWAYS AS IDENTITY, b text); +CREATE TABLE test_like_id_1 (a bigint GENERATED ALWAYS AS IDENTITY, b text); \d test_like_id_1 INSERT INTO test_like_id_1 (b) VALUES ('b1'); SELECT * FROM test_like_id_1; base-commit: bcdd40538a11a39fee55ef1c4b79085717a5fb76 -- 2.16.2
0002-Change-internal-integer-representation-of-Value-node.patch (text/plain, 5.2K) download From c18328493a4a241164416a3a304ef3c22bd92ee2 Mon Sep 17 00:00:00 2001 From: Peter Eisentraut <peter_e@gmx.net> Date: Wed, 7 Mar 2018 14:42:17 -0500 Subject: [PATCH 2/2] Change internal integer representation of Value node A Value node would store an integer as a long. This causes needless portability risks, as long can be of varying sizes. Change it to use int instead. All code using this was already careful to only store 32-bit values anyway. --- src/backend/nodes/outfuncs.c | 2 +- src/backend/nodes/read.c | 13 +++++-------- src/backend/nodes/value.c | 2 +- src/backend/parser/scan.l | 9 +++------ src/backend/utils/misc/guc.c | 2 +- src/include/nodes/value.h | 4 ++-- src/interfaces/ecpg/preproc/pgc.l | 9 +++------ 7 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 1785ea3918..fd80891954 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -3235,7 +3235,7 @@ _outValue(StringInfo str, const Value *value) switch (value->type) { case T_Integer: - appendStringInfo(str, "%ld", value->val.ival); + appendStringInfo(str, "%d", value->val.ival); break; case T_Float: diff --git a/src/backend/nodes/read.c b/src/backend/nodes/read.c index 76414029d8..b2ba736b8f 100644 --- a/src/backend/nodes/read.c +++ b/src/backend/nodes/read.c @@ -225,12 +225,9 @@ nodeTokenType(char *token, int length) errno = 0; val = strtol(token, &endptr, 10); (void) val; /* avoid compiler warning if unused */ - if (endptr != token + length || errno == ERANGE -#ifdef HAVE_LONG_INT_64 - /* if long > 32 bits, check for overflow of int4 */ - || val != (long) ((int32) val) -#endif - ) + if (endptr != token + length || errno == ERANGE || + /* check for overflow of int4 */ + val != (long) ((int32) val)) return T_Float; return T_Integer; } @@ -387,9 +384,9 @@ nodeRead(char *token, int tok_len) case T_Integer: /* - * we know that the token terminates on a char atol will stop at + * we know that the token terminates on a char atoi will stop at */ - result = (Node *) makeInteger(atol(token)); + result = (Node *) makeInteger(atoi(token)); break; case T_Float: { diff --git a/src/backend/nodes/value.c b/src/backend/nodes/value.c index 8f0428fce1..2a30307baf 100644 --- a/src/backend/nodes/value.c +++ b/src/backend/nodes/value.c @@ -20,7 +20,7 @@ * makeInteger */ Value * -makeInteger(long i) +makeInteger(int i) { Value *v = makeNode(Value); diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l index eedef7c005..c3a904b562 100644 --- a/src/backend/parser/scan.l +++ b/src/backend/parser/scan.l @@ -1216,12 +1216,9 @@ process_integer_literal(const char *token, YYSTYPE *lval) errno = 0; val = strtol(token, &endptr, 10); - if (*endptr != '\0' || errno == ERANGE -#ifdef HAVE_LONG_INT_64 - /* if long > 32 bits, check for overflow of int4 */ - || val != (long) ((int32) val) -#endif - ) + if (*endptr != '\0' || errno == ERANGE || + /* check for overflow of int4 */ + val != (long) ((int32) val)) { /* integer too large, treat it as a float */ lval->str = pstrdup(token); diff --git a/src/backend/utils/misc/guc.c b/src/backend/utils/misc/guc.c index 0c4cc9160d..78c9ab7f4d 100644 --- a/src/backend/utils/misc/guc.c +++ b/src/backend/utils/misc/guc.c @@ -6912,7 +6912,7 @@ flatten_set_variable_args(const char *name, List *args) switch (nodeTag(&con->val)) { case T_Integer: - appendStringInfo(&buf, "%ld", intVal(&con->val)); + appendStringInfo(&buf, "%d", intVal(&con->val)); break; case T_Float: /* represented as a string, so just copy it */ diff --git a/src/include/nodes/value.h b/src/include/nodes/value.h index 94d09e176e..cef97812ef 100644 --- a/src/include/nodes/value.h +++ b/src/include/nodes/value.h @@ -44,7 +44,7 @@ typedef struct Value NodeTag type; /* tag appropriately (eg. T_String) */ union ValUnion { - long ival; /* machine integer */ + int ival; /* machine integer */ char *str; /* string */ } val; } Value; @@ -53,7 +53,7 @@ typedef struct Value #define floatVal(v) atof(((Value *)(v))->val.str) #define strVal(v) (((Value *)(v))->val.str) -extern Value *makeInteger(long i); +extern Value *makeInteger(int i); extern Value *makeFloat(char *numericStr); extern Value *makeString(char *str); extern Value *makeBitString(char *str); diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/preproc/pgc.l index e99b7ff586..b02fe47ab5 100644 --- a/src/interfaces/ecpg/preproc/pgc.l +++ b/src/interfaces/ecpg/preproc/pgc.l @@ -732,12 +732,9 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+ errno = 0; val = strtol((char *)yytext, &endptr,10); - if (*endptr != '\0' || errno == ERANGE -#ifdef HAVE_LONG_INT_64 - /* if long > 32 bits, check for overflow of int4 */ - || val != (long) ((int32) val) -#endif - ) + if (*endptr != '\0' || errno == ERANGE || + /* check for overflow of int4 */ + val != (long) ((int32) val)) { errno = 0; base_yylval.str = mm_strdup(yytext); -- 2.16.2

view thread (13+ messages)  latest in thread

Message-ID: <d9f1443c-9006-3705-1e1b-7b65301dae42@2ndquadrant.com>
Permalink:  ../d9f1443c-9006-3705-1e1b-7b65301dae42@2ndquadrant.com/
Also on:    postgresql.org/message-id/d9f1443c-9006-3705-1e1b-7b65301dae42@2ndquadrant.com

 ·  · 

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: peter.eisentraut@2ndquadrant.com, tgl@sss.pgh.pa.us, michael@paquier.xyz, andrew@tao11.riddles.org.uk, evgeni-klimov@yandex.ru
  Subject: Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
  In-Reply-To: <d9f1443c-9006-3705-1e1b-7b65301dae42@2ndquadrant.com>
  References: <152009601619.21242.262498029671910378@wrigleys.postgresql.org> <31194.1520098424@sss.pgh.pa.us> <87po4lkkaj.fsf@news-spur.riddles.org.uk> <20180304235654.GA32165@paquier.xyz> <87lgf7l6sj.fsf@news-spur.riddles.org.uk> <21016.1520228672@sss.pgh.pa.us> <20180305061325.GF2266@paquier.xyz> <26456.1520278713@sss.pgh.pa.us> <d9f1443c-9006-3705-1e1b-7b65301dae42@2ndquadrant.com>

* 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