pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
13+ messages / 5 participants
[nested] [flat]
* BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
@ 2018-03-03 16:53 PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 13+ messages in thread
From: PG Bug reporting form @ 2018-03-03 16:53 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: evgeni-klimov@yandex.ru
The following bug has been logged on the website:
Bug reference: 15096
Logged by: Evgeny
Email address: evgeni-klimov@yandex.ru
PostgreSQL version: 10.2
Operating system: Windows 7 x64
Description:
CREATE TABLE public.test (id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY
KEY, s TEXT);
CREATE TABLE public.test_copy (LIKE public.test INCLUDING ALL);
ERROR: MINVALUE (1) must be less than MAXVALUE (-1)
However, if I change the id column type to INT, the second command
succeeds.
Checked on 10.3 and 10.2.
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
@ 2018-03-03 17:33 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 1 reply; 13+ messages in thread
From: Tom Lane @ 2018-03-03 17:33 UTC (permalink / raw)
To: evgeni-klimov@yandex.ru; +Cc: pgsql-bugs@lists.postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
=?utf-8?q?PG_Bug_reporting_form?= <noreply@postgresql.org> writes:
> CREATE TABLE public.test (id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY
> KEY, s TEXT);
> CREATE TABLE public.test_copy (LIKE public.test INCLUDING ALL);
> ERROR: MINVALUE (1) must be less than MAXVALUE (-1)
Hm ... works fine for me on a 64-bit Linux machine ... but it fails
as described on 32-bit. Something in the LIKE code path is shoving
the sequence's maxvalue through a variable with platform-dependent
width, probably of type "long". No time right now to locate the
exact problem.
regards, tom lane
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-03-03 19:55 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 13+ messages in thread
From: Andrew Gierth @ 2018-03-03 19:55 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: evgeni-klimov@yandex.ru, pgsql-bugs@lists.postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> writes:
>> ERROR: MINVALUE (1) must be less than MAXVALUE (-1)
Tom> Hm ... works fine for me on a 64-bit Linux machine ... but it
Tom> fails as described on 32-bit. Something in the LIKE code path is
Tom> shoving the sequence's maxvalue through a variable with
Tom> platform-dependent width, probably of type "long". No time right
Tom> now to locate the exact problem.
The Value node's "ival" field is declared as a long.
sequence_options thinks it can put all the sequence parameters through
makeInteger and have them come out intact.
--
Andrew (irc:RhodiumToad)
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
@ 2018-03-04 23:56 ` Michael Paquier <michael@paquier.xyz>
2018-03-05 00:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2018-03-04 23:56 UTC (permalink / raw)
To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; evgeni-klimov@yandex.ru, pgsql-bugs@lists.postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
On Sat, Mar 03, 2018 at 07:55:41PM +0000, Andrew Gierth wrote:
> sequence_options thinks it can put all the sequence parameters through
> makeInteger and have them come out intact.
Yeah, on 32b machines "long" is 4 bytes... Perhaps it would be the
occasion to introduce a T_Integer64 type for Value which gets stored as
a string? And as far as I can see defGetInt64 is only used by
sequences.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
@ 2018-03-05 00:16 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-05 05:44 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 13+ messages in thread
From: Andrew Gierth @ 2018-03-05 00:16 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; evgeni-klimov@yandex.ru, pgsql-bugs@lists.postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
>>>>> "Michael" == Michael Paquier <michael@paquier.xyz> writes:
> On Sat, Mar 03, 2018 at 07:55:41PM +0000, Andrew Gierth wrote:
>> sequence_options thinks it can put all the sequence parameters
>> through makeInteger and have them come out intact.
Michael> Yeah, on 32b machines "long" is 4 bytes...
And on 64-bit Windows, too.
Michael> Perhaps it would be the occasion to introduce a T_Integer64
Michael> type for Value which gets stored as a string? And as far as I
Michael> can see defGetInt64 is only used by sequences.
The slightly misnamed T_Float is what's currently used for Value nodes
which contain numeric values as strings. So there'd be no point in a new
type tag if you're still going to store the value as a string.
--
Andrew (irc:RhodiumToad)
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 00:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
@ 2018-03-05 05:44 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-03-05 06:13 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 13+ messages in thread
From: Tom Lane @ 2018-03-05 05:44 UTC (permalink / raw)
To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: Michael Paquier <michael@paquier.xyz>; evgeni-klimov@yandex.ru, pgsql-bugs@lists.postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Andrew Gierth <andrew@tao11.riddles.org.uk> writes:
> "Michael" == Michael Paquier <michael@paquier.xyz> writes:
> Michael> Perhaps it would be the occasion to introduce a T_Integer64
> Michael> type for Value which gets stored as a string? And as far as I
> Michael> can see defGetInt64 is only used by sequences.
> The slightly misnamed T_Float is what's currently used for Value nodes
> which contain numeric values as strings. So there'd be no point in a new
> type tag if you're still going to store the value as a string.
Going forward, maybe we should change the T_Integer case to either int64
or int32, so that it's not got a platform-dependent range. That's not a
workable solution for back-patching into v10, though (and neither is
T_Integer64, really).
regards, tom lane
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 00:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-05 05:44 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-03-05 06:13 ` Michael Paquier <michael@paquier.xyz>
2018-03-05 19:38 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2018-03-05 06:13 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; evgeni-klimov@yandex.ru, pgsql-bugs@lists.postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
On Mon, Mar 05, 2018 at 12:44:32AM -0500, Tom Lane wrote:
> Andrew Gierth <andrew@tao11.riddles.org.uk> writes:
>> "Michael" == Michael Paquier <michael@paquier.xyz> writes:
>> Michael> Perhaps it would be the occasion to introduce a T_Integer64
>> Michael> type for Value which gets stored as a string? And as far as I
>> Michael> can see defGetInt64 is only used by sequences.
>
>> The slightly misnamed T_Float is what's currently used for Value nodes
>> which contain numeric values as strings. So there'd be no point in a new
>> type tag if you're still going to store the value as a string.
>
> Going forward, maybe we should change the T_Integer case to either int64
> or int32, so that it's not got a platform-dependent range.
Serial columns using bigint as type would need int64 anyway, no? Why
int32?
> That's not a workable solution for back-patching into v10, though (and
> neither is T_Integer64, really).
Sure. For v10, using just T_Float should be doable at quick glance. I
have not checked though.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 00:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-05 05:44 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-05 06:13 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
@ 2018-03-05 19:38 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-03-07 20:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 1 reply; 13+ messages in thread
From: Tom Lane @ 2018-03-05 19:38 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; evgeni-klimov@yandex.ru, pgsql-bugs@lists.postgresql.org, Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
Michael Paquier <michael@paquier.xyz> writes:
> On Mon, Mar 05, 2018 at 12:44:32AM -0500, Tom Lane wrote:
>> Going forward, maybe we should change the T_Integer case to either int64
>> or int32, so that it's not got a platform-dependent range.
> Serial columns using bigint as type would need int64 anyway, no? Why
> int32?
int32 might be less work to make fully portable. I don't recall exactly
what-all we do with T_Integer, but if we try to do sscanf() to produce the
value for instance, that's a problem. (Note that configure's tests for
64-bit support only cover sprintf, not sscanf.)
Certainly int64 would be a more forward-looking choice, it just seems
like possibly more work.
regards, tom lane
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 00:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-05 05:44 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-05 06:13 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 19:38 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
@ 2018-03-07 20:16 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-03-08 08:08 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 13+ messages in thread
From: Peter Eisentraut @ 2018-03-07 20:16 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; Michael Paquier <michael@paquier.xyz>; +Cc: Andrew Gierth <andrew@tao11.riddles.org.uk>; evgeni-klimov@yandex.ru; pgsql-bugs@lists.postgresql.org
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
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 00:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-05 05:44 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-05 06:13 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 19:38 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-07 20:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
@ 2018-03-08 08:08 ` Michael Paquier <michael@paquier.xyz>
2018-03-12 17:45 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2018-03-08 08:08 UTC (permalink / raw)
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andrew Gierth <andrew@tao11.riddles.org.uk>; evgeni-klimov@yandex.ru; pgsql-bugs@lists.postgresql.org
On Wed, Mar 07, 2018 at 03:16:44PM -0500, Peter Eisentraut wrote:
> 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.
Thanks Peter for diving in. Patch 1 looks fine to me.
Here are some comments for patch 2.
+ if (endptr != token + length || errno == ERANGE ||
+ /* check for overflow of int4 */
+ val != (long) ((int32) val))
return T_Float;
It would be nice to have this check be consistent with the new
definition of ival and int32, One suggestion is to use directly int32 or
just have a static assertion that sizeof(int) == sizeof(int32)? Or
that's too much nannyism?
By the way, why do you remove HAVE_LONG_INT_64? On platforms where long
is 4 bytes this would still be a no-op. If you care about those, you
could also remove the ones in interval.c and datetime.c...
The comment block on top of the definition of Value in value.h still
mentions "long", while it should mention "int" per your patch.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 00:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-05 05:44 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-05 06:13 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 19:38 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-07 20:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-03-08 08:08 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
@ 2018-03-12 17:45 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-03-13 06:22 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
0 siblings, 1 reply; 13+ messages in thread
From: Peter Eisentraut @ 2018-03-12 17:45 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andrew Gierth <andrew@tao11.riddles.org.uk>; evgeni-klimov@yandex.ru; pgsql-bugs@lists.postgresql.org
On 3/8/18 03:08, Michael Paquier wrote:
> Here are some comments for patch 2.
>
> + if (endptr != token + length || errno == ERANGE ||
> + /* check for overflow of int4 */
> + val != (long) ((int32) val))
> return T_Float;
> It would be nice to have this check be consistent with the new
> definition of ival and int32, One suggestion is to use directly int32 or
> just have a static assertion that sizeof(int) == sizeof(int32)? Or
> that's too much nannyism?
Fixed in the attached next version.
> By the way, why do you remove HAVE_LONG_INT_64? On platforms where long
> is 4 bytes this would still be a no-op.
Right, but the compiler can optimize it away then. No need to have #ifdefs.
> The comment block on top of the definition of Value in value.h still
> mentions "long", while it should mention "int" per your patch.
done
> If you care about those, you
> could also remove the ones in interval.c and datetime.c...
Actually, we could just use the strtoint() defined there and apply it
everywhere, so avoid repeating these patterns. Done so in an additional
patch.
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
Attachments: download all (3 files, .tar.gz)
v2-0001-Fix-CREATE-TABLE-LIKE-with-bigint-identity-column.patch (text/plain, 6.0K) download
From 23a0bbc152529713ba45e44eaaa177b19b4ca814 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 v2 1/3] 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: 4a4e2442a7f7c1434e86dd290cdb3704cfebb24c
--
2.16.2
v2-0002-Change-internal-integer-representation-of-Value-n.patch (text/plain, 5.4K) download
From 7cd34d15240ca7581826b795b599bd507ca43c4b Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Mon, 12 Mar 2018 12:17:58 -0400
Subject: [PATCH v2 2/3] 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 | 14 +++++---------
src/backend/nodes/value.c | 2 +-
src/backend/parser/scan.l | 9 +++------
src/backend/utils/misc/guc.c | 2 +-
src/include/nodes/value.h | 6 +++---
src/interfaces/ecpg/preproc/pgc.l | 9 +++------
7 files changed, 17 insertions(+), 27 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..6e9fa45e37 100644
--- a/src/backend/nodes/read.c
+++ b/src/backend/nodes/read.c
@@ -224,13 +224,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 int */
+ val != (int) val)
return T_Float;
return T_Integer;
}
@@ -387,9 +383,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..97d4dee628 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 int */
+ val != (int) 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 a4f9b3668e..fc3e10c750 100644
--- a/src/backend/utils/misc/guc.c
+++ b/src/backend/utils/misc/guc.c
@@ -6913,7 +6913,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..1665714515 100644
--- a/src/include/nodes/value.h
+++ b/src/include/nodes/value.h
@@ -34,7 +34,7 @@
* better to use the more general representation.)
*
* Note that an integer-looking string will get lexed as T_Float if
- * the value is too large to fit in a 'long'.
+ * the value is too large to fit in an 'int'.
*
* Nulls, of course, don't need the value part at all.
*----------------------
@@ -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..ba1798c77e 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 int */
+ val != (int) val)
{
errno = 0;
base_yylval.str = mm_strdup(yytext);
--
2.16.2
v2-0003-Move-strtoint-to-common.patch (text/plain, 7.3K) download
From aeea50127725058daeef294bf5b10f304e1d14bb Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Mon, 12 Mar 2018 13:44:15 -0400
Subject: [PATCH v2 3/3] Move strtoint() to common
Several places uses similar code to convert a string to an int, so take
the function that we already had and make it globally available.
---
src/backend/nodes/read.c | 11 +++++------
src/backend/parser/scan.l | 9 ++++-----
src/backend/utils/adt/datetime.c | 18 +-----------------
src/common/string.c | 15 +++++++++++++++
src/include/common/string.h | 1 +
src/interfaces/ecpg/pgtypeslib/Makefile | 6 +++++-
src/interfaces/ecpg/pgtypeslib/interval.c | 16 ++--------------
src/interfaces/ecpg/preproc/pgc.l | 10 +++++-----
8 files changed, 38 insertions(+), 48 deletions(-)
diff --git a/src/backend/nodes/read.c b/src/backend/nodes/read.c
index 6e9fa45e37..5437fa3d0a 100644
--- a/src/backend/nodes/read.c
+++ b/src/backend/nodes/read.c
@@ -21,6 +21,7 @@
#include <ctype.h>
+#include "common/string.h"
#include "nodes/pg_list.h"
#include "nodes/readfuncs.h"
#include "nodes/value.h"
@@ -216,17 +217,15 @@ nodeTokenType(char *token, int length)
/*
* Yes. Figure out whether it is integral or float; this requires
* both a syntax check and a range check. strtol() can do both for us.
- * We know the token will end at a character that strtol will stop at,
+ * We know the token will end at a character that strtoint will stop at,
* so we do not need to modify the string.
*/
- long val;
+ int val;
char *endptr;
errno = 0;
- val = strtol(token, &endptr, 10);
- if (endptr != token + length || errno == ERANGE ||
- /* check for overflow of int */
- val != (int) val)
+ val = strtoint(token, &endptr, 10);
+ if (endptr != token + length || errno == ERANGE)
return T_Float;
return T_Integer;
}
diff --git a/src/backend/parser/scan.l b/src/backend/parser/scan.l
index 97d4dee628..0cd782827a 100644
--- a/src/backend/parser/scan.l
+++ b/src/backend/parser/scan.l
@@ -34,6 +34,7 @@
#include <ctype.h>
#include <unistd.h>
+#include "common/string.h"
#include "parser/gramparse.h"
#include "parser/parser.h" /* only needed for GUC variables */
#include "parser/scansup.h"
@@ -1211,14 +1212,12 @@ litbufdup(core_yyscan_t yyscanner)
static int
process_integer_literal(const char *token, YYSTYPE *lval)
{
- long val;
+ int val;
char *endptr;
errno = 0;
- val = strtol(token, &endptr, 10);
- if (*endptr != '\0' || errno == ERANGE ||
- /* check for overflow of int */
- val != (int) val)
+ val = strtoint(token, &endptr, 10);
+ if (*endptr != '\0' || errno == ERANGE)
{
/* integer too large, treat it as a float */
lval->str = pstrdup(token);
diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c
index 8375b93c39..3f0f65c295 100644
--- a/src/backend/utils/adt/datetime.c
+++ b/src/backend/utils/adt/datetime.c
@@ -22,6 +22,7 @@
#include "access/htup_details.h"
#include "access/xact.h"
#include "catalog/pg_type.h"
+#include "common/string.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "nodes/nodeFuncs.h"
@@ -251,23 +252,6 @@ static const datetkn *deltacache[MAXDATEFIELDS] = {NULL};
static const datetkn *abbrevcache[MAXDATEFIELDS] = {NULL};
-/*
- * strtoint --- just like strtol, but returns int not long
- */
-static int
-strtoint(const char *nptr, char **endptr, int base)
-{
- long val;
-
- val = strtol(nptr, endptr, base);
-#ifdef HAVE_LONG_INT_64
- if (val != (long) ((int32) val))
- errno = ERANGE;
-#endif
- return (int) val;
-}
-
-
/*
* Calendar time to Julian date conversions.
* Julian date is commonly used in astronomical applications,
diff --git a/src/common/string.c b/src/common/string.c
index 0e3557076a..a45e9b8858 100644
--- a/src/common/string.c
+++ b/src/common/string.c
@@ -41,3 +41,18 @@ pg_str_endswith(const char *str, const char *end)
str += slen - elen;
return strcmp(str, end) == 0;
}
+
+
+/*
+ * strtoint --- just like strtol, but returns int not long
+ */
+int
+strtoint(const char *restrict str, char **restrict endptr, int base)
+{
+ long val;
+
+ val = strtol(str, endptr, base);
+ if (val != (int) val)
+ errno = ERANGE;
+ return (int) val;
+}
diff --git a/src/include/common/string.h b/src/include/common/string.h
index 06934b9914..23e2e01f0e 100644
--- a/src/include/common/string.h
+++ b/src/include/common/string.h
@@ -11,5 +11,6 @@
#define COMMON_STRING_H
extern bool pg_str_endswith(const char *str, const char *end);
+extern int strtoint(const char *restrict str, char **restrict endptr, int base);
#endif /* COMMON_STRING_H */
diff --git a/src/interfaces/ecpg/pgtypeslib/Makefile b/src/interfaces/ecpg/pgtypeslib/Makefile
index 01206568f1..0945f3d5bc 100644
--- a/src/interfaces/ecpg/pgtypeslib/Makefile
+++ b/src/interfaces/ecpg/pgtypeslib/Makefile
@@ -32,6 +32,7 @@ SHLIB_EXPORTS = exports.txt
OBJS= numeric.o datetime.o common.o dt_common.o timestamp.o interval.o \
pgstrcasecmp.o \
$(filter rint.o snprintf.o strnlen.o, $(LIBOBJS)) \
+ string.o \
$(WIN32RES)
all: all-lib
@@ -47,6 +48,9 @@ include $(top_srcdir)/src/Makefile.shlib
pgstrcasecmp.c rint.c snprintf.c strnlen.c: % : $(top_srcdir)/src/port/%
rm -f $@ && $(LN_S) $< .
+string.c: % : $(top_srcdir)/src/common/%
+ rm -f $@ && $(LN_S) $< .
+
install: all installdirs install-lib
installdirs: installdirs-lib
@@ -54,6 +58,6 @@ installdirs: installdirs-lib
uninstall: uninstall-lib
clean distclean: clean-lib
- rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strnlen.c
+ rm -f $(OBJS) pgstrcasecmp.c rint.c snprintf.c strnlen.c string.c
maintainer-clean: distclean maintainer-clean-lib
diff --git a/src/interfaces/ecpg/pgtypeslib/interval.c b/src/interfaces/ecpg/pgtypeslib/interval.c
index 24a2f36d4d..6146a3c7b6 100644
--- a/src/interfaces/ecpg/pgtypeslib/interval.c
+++ b/src/interfaces/ecpg/pgtypeslib/interval.c
@@ -9,25 +9,13 @@
#error -ffast-math is known to break this code
#endif
+#include "common/string.h"
+
#include "extern.h"
#include "dt.h"
#include "pgtypes_error.h"
#include "pgtypes_interval.h"
-/* copy&pasted from .../src/backend/utils/adt/datetime.c */
-static int
-strtoint(const char *nptr, char **endptr, int base)
-{
- long val;
-
- val = strtol(nptr, endptr, base);
-#ifdef HAVE_LONG_INT_64
- if (val != (long) ((int32) val))
- errno = ERANGE;
-#endif
- return (int) val;
-}
-
/* copy&pasted from .../src/backend/utils/adt/datetime.c
* and changesd struct pg_tm to struct tm
*/
diff --git a/src/interfaces/ecpg/preproc/pgc.l b/src/interfaces/ecpg/preproc/pgc.l
index ba1798c77e..405dee73b0 100644
--- a/src/interfaces/ecpg/preproc/pgc.l
+++ b/src/interfaces/ecpg/preproc/pgc.l
@@ -21,6 +21,8 @@
#include <ctype.h>
#include <limits.h>
+#include "common/string.h"
+
#include "extern.h"
#include "preproc.h"
}
@@ -727,14 +729,12 @@ cppline {space}*#([^i][A-Za-z]*|{if}|{ifdef}|{ifndef}|{import})((\/\*[^*/]*\*+
return PARAM;
}
<C,SQL>{integer} {
- long val;
+ int val;
char* endptr;
errno = 0;
- val = strtol((char *)yytext, &endptr,10);
- if (*endptr != '\0' || errno == ERANGE ||
- /* check for overflow of int */
- val != (int) val)
+ val = strtoint(yytext, &endptr, 10);
+ if (*endptr != '\0' || errno == ERANGE)
{
errno = 0;
base_yylval.str = mm_strdup(yytext);
--
2.16.2
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 00:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-05 05:44 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-05 06:13 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 19:38 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-07 20:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-03-08 08:08 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-12 17:45 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
@ 2018-03-13 06:22 ` Michael Paquier <michael@paquier.xyz>
2018-03-13 14:22 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 1 reply; 13+ messages in thread
From: Michael Paquier @ 2018-03-13 06:22 UTC (permalink / raw)
To: Peter Eisentraut <peter.eisentraut@2ndquadrant.com>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andrew Gierth <andrew@tao11.riddles.org.uk>; evgeni-klimov@yandex.ru; pgsql-bugs@lists.postgresql.org
On Mon, Mar 12, 2018 at 01:45:26PM -0400, Peter Eisentraut wrote:
> On 3/8/18 03:08, Michael Paquier wrote:
>> Here are some comments for patch 2.
>>
>> + if (endptr != token + length || errno == ERANGE ||
>> + /* check for overflow of int4 */
>> + val != (long) ((int32) val))
>> return T_Float;
>> It would be nice to have this check be consistent with the new
>> definition of ival and int32, One suggestion is to use directly int32 or
>> just have a static assertion that sizeof(int) == sizeof(int32)? Or
>> that's too much nannyism?
>
> Fixed in the attached next version.
Thanks, this looks good to me.
>> If you care about those, you
>> could also remove the ones in interval.c and datetime.c...
>
> Actually, we could just use the strtoint() defined there and apply it
> everywhere, so avoid repeating these patterns. Done so in an additional
> patch.
OK, that's a good idea.
+/*
+ * strtoint --- just like strtol, but returns int not long
+ */
It would be nice to document that caller should check for errno for
sanity checks, in which case caller should not rely on the returned
result.
Worth noting that pgbench has its own view of things with strtoint64.
Not worth bothering anyway.
--
Michael
Attachments:
signature.asc (application/pgp-signature, 832B) download
^ permalink raw reply [nested|flat] 13+ messages in thread
* Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 00:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-05 05:44 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-05 06:13 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-05 19:38 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Tom Lane <tgl@sss.pgh.pa.us>
2018-03-07 20:16 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-03-08 08:08 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
2018-03-12 17:45 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-03-13 06:22 ` Re: BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column Michael Paquier <michael@paquier.xyz>
@ 2018-03-13 14:22 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
0 siblings, 0 replies; 13+ messages in thread
From: Peter Eisentraut @ 2018-03-13 14:22 UTC (permalink / raw)
To: Michael Paquier <michael@paquier.xyz>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; Andrew Gierth <andrew@tao11.riddles.org.uk>; evgeni-klimov@yandex.ru; pgsql-bugs@lists.postgresql.org
On 3/13/18 02:22, Michael Paquier wrote:
> Thanks, this looks good to me.
committed (0001 to PG10, 0001-0003 to master)
--
Peter Eisentraut http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services
^ permalink raw reply [nested|flat] 13+ messages in thread
end of thread, other threads:[~2018-03-13 14:22 UTC | newest]
Thread overview: 13+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-03-03 16:53 BUG #15096: Unable to CREATE TABLE LIKE with bigint identity column PG Bug reporting form <noreply@postgresql.org>
2018-03-03 17:33 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-03-03 19:55 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-04 23:56 ` Michael Paquier <michael@paquier.xyz>
2018-03-05 00:16 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2018-03-05 05:44 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-03-05 06:13 ` Michael Paquier <michael@paquier.xyz>
2018-03-05 19:38 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-03-07 20:16 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-03-08 08:08 ` Michael Paquier <michael@paquier.xyz>
2018-03-12 17:45 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.com>
2018-03-13 06:22 ` Michael Paquier <michael@paquier.xyz>
2018-03-13 14:22 ` Peter Eisentraut <peter.eisentraut@2ndquadrant.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