Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1ead020

Browse files
committed
Fix CREATE TABLE ... LIKE ... WITH OIDS.
Having a WITH OIDS specification should result in the creation of an OIDcolumn, but commitb943f50 broke that in the case that there were LIKEtables without OIDS. Commentary in that patch makes it look like this wasintentional, but if so it was based on a faulty reading of what inheritancedoes: the parent tables can add an OID column, but they can't subtract one.AFAICS, the behavior ought to be that you get an OID column if any of theinherited tables, LIKE tables, or WITH clause ask for one.Also, revert that patch's unnecessary split of transformCreateStmt's loopover the tableElts list into two passes. That seems to have been based ona misunderstanding as well: we already have two-pass processing here,we don't need three passes.Per bug #14474 from Jeff Dafoe. Back-patch to 9.6 where the misbehaviorwas introduced.Report:https://postgr.es/m/20161222145304.25620.47445@wrigleys.postgresql.org
1 parent22434dd commit1ead020

File tree

3 files changed

+30
-36
lines changed

3 files changed

+30
-36
lines changed

‎src/backend/parser/parse_utilcmd.c

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@
6161
#include"rewrite/rewriteManip.h"
6262
#include"utils/acl.h"
6363
#include"utils/builtins.h"
64-
#include"utils/guc.h"
6564
#include"utils/lsyscache.h"
6665
#include"utils/rel.h"
6766
#include"utils/ruleutils.h"
@@ -278,10 +277,7 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
278277

279278
/*
280279
* Run through each primary element in the table creation clause. Separate
281-
* column defs from constraints, and do preliminary analysis. We have to
282-
* process column-defining clauses first because it can control the
283-
* presence of columns which are referenced by columns referenced by
284-
* constraints.
280+
* column defs from constraints, and do preliminary analysis.
285281
*/
286282
foreach(elements,stmt->tableElts)
287283
{
@@ -293,17 +289,13 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
293289
transformColumnDefinition(&cxt, (ColumnDef*)element);
294290
break;
295291

296-
caseT_TableLikeClause:
297-
if (!like_found)
298-
{
299-
cxt.hasoids= false;
300-
like_found= true;
301-
}
302-
transformTableLikeClause(&cxt, (TableLikeClause*)element);
292+
caseT_Constraint:
293+
transformTableConstraint(&cxt, (Constraint*)element);
303294
break;
304295

305-
caseT_Constraint:
306-
/* process later */
296+
caseT_TableLikeClause:
297+
like_found= true;
298+
transformTableLikeClause(&cxt, (TableLikeClause*)element);
307299
break;
308300

309301
default:
@@ -313,27 +305,19 @@ transformCreateStmt(CreateStmt *stmt, const char *queryString)
313305
}
314306
}
315307

316-
if (like_found)
317-
{
318-
/*
319-
* To match INHERITS, the existence of any LIKE table with OIDs causes
320-
* the new table to have oids. For the same reason, WITH/WITHOUT OIDs
321-
* is also ignored with LIKE. We prepend because the first oid option
322-
* list entry is honored. Our prepended WITHOUT OIDS clause will be
323-
* overridden if an inherited table has oids.
324-
*/
308+
/*
309+
* If we had any LIKE tables, they may require creation of an OID column
310+
* even though the command's own WITH clause didn't ask for one (or,
311+
* perhaps, even specifically rejected having one). Insert a WITH option
312+
* to ensure that happens. We prepend to the list because the first oid
313+
* option will be honored, and we want to override anything already there.
314+
* (But note that DefineRelation will override this again to add an OID
315+
* column if one appears in an inheritance parent table.)
316+
*/
317+
if (like_found&&cxt.hasoids)
325318
stmt->options=lcons(makeDefElem("oids",
326-
(Node*)makeInteger(cxt.hasoids),-1),
319+
(Node*)makeInteger(true),-1),
327320
stmt->options);
328-
}
329-
330-
foreach(elements,stmt->tableElts)
331-
{
332-
Node*element=lfirst(elements);
333-
334-
if (nodeTag(element)==T_Constraint)
335-
transformTableConstraint(&cxt, (Constraint*)element);
336-
}
337321

338322
/*
339323
* transformIndexConstraints wants cxt.alist to contain only index
@@ -975,7 +959,7 @@ transformTableLikeClause(CreateStmtContext *cxt, TableLikeClause *table_like_cla
975959
}
976960

977961
/* We use oids if at least one LIKE'ed table has oids. */
978-
cxt->hasoids=cxt->hasoids||relation->rd_rel->relhasoids;
962+
cxt->hasoids|=relation->rd_rel->relhasoids;
979963

980964
/*
981965
* Copy CHECK constraints if requested, being careful to adjust attribute

‎src/test/regress/expected/create_table_like.out

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,11 @@ SELECT oid FROM like_test4;
254254
-----
255255
(0 rows)
256256

257-
DROP TABLE has_oid, no_oid, like_test, like_test2, like_test3, like_test4;
257+
CREATE TABLE like_test5 (z INTEGER, LIKE no_oid) WITH OIDS;
258+
SELECT oid FROM like_test5;
259+
oid
260+
-----
261+
(0 rows)
262+
263+
DROP TABLE has_oid, no_oid, like_test, like_test2, like_test3,
264+
like_test4, like_test5;

‎src/test/regress/sql/create_table_like.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,7 @@ CREATE TABLE like_test3 (z INTEGER, LIKE has_oid, LIKE no_oid);
131131
SELECToidFROM like_test3;
132132
CREATETABLElike_test4 (zINTEGER,PRIMARY KEY(oid),LIKE has_oid);
133133
SELECToidFROM like_test4;
134-
DROPTABLE has_oid, no_oid, like_test, like_test2, like_test3, like_test4;
134+
CREATETABLElike_test5 (zINTEGER,LIKE no_oid) WITH OIDS;
135+
SELECToidFROM like_test5;
136+
DROPTABLE has_oid, no_oid, like_test, like_test2, like_test3,
137+
like_test4, like_test5;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp