Skip to content

Commit 98dcf08

Browse files
author
Neil Conway
committed
Implement "WITH / WITHOID OIDS" clause for CREATE TABLE AS. This is
intended to allow application authors to insulate themselves from changes to the default value of 'default_with_oids' in future releases of PostgreSQL. This patch also fixes a bug in the earlier implementation of the 'default_with_oids' GUC variable: code in gram.y should not examine the value of GUC variables directly due to synchronization issues.
1 parent cf4c925 commit 98dcf08

File tree

14 files changed

+185
-58
lines changed

14 files changed

+185
-58
lines changed

doc/src/sgml/ref/create_table_as.sgml

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_table_as.sgml,v 1.19 2003/12/13 23:59:07 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_table_as.sgml,v 1.20 2004/01/10 23:28:44 neilc Exp $
33
PostgreSQL documentation
44
-->
55

@@ -20,7 +20,7 @@ PostgreSQL documentation
2020

2121
<refsynopsisdiv>
2222
<synopsis>
23-
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE <replaceable>table_name</replaceable> [ (<replaceable>column_name</replaceable> [, ...] ) ]
23+
CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE <replaceable>table_name</replaceable> [ (<replaceable>column_name</replaceable> [, ...] ) ] [ [ WITH | WITHOUT ] OIDS ]
2424
AS <replaceable>query</replaceable>
2525
</synopsis>
2626
</refsynopsisdiv>
@@ -98,6 +98,20 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE <replaceable>table_name
9898
</listitem>
9999
</varlistentry>
100100

101+
<varlistentry>
102+
<term><literal>WITH OIDS</literal></term>
103+
<term><literal>WITHOUT OIDS</literal></term>
104+
<listitem>
105+
<para>
106+
This optional clause specifies whether the table created by
107+
<command>CREATE TABLE AS</command> should include OIDs. If
108+
neither form of this clause if specified, the value of the
109+
<varname>default_with_oids</varname> configuration parameter is
110+
used.
111+
</para>
112+
</listitem>
113+
</varlistentry>
114+
101115
<varlistentry>
102116
<term><replaceable>query</replaceable></term>
103117
<listitem>
@@ -121,23 +135,30 @@ CREATE [ [ GLOBAL | LOCAL ] { TEMPORARY | TEMP } ] TABLE <replaceable>table_name
121135
This command is functionally similar to <xref
122136
linkend="sql-selectinto" endterm="sql-selectinto-title">, but it is
123137
preferred since it is less likely to be confused with other uses of
124-
the <command>SELECT INTO</command> syntax.
138+
the <command>SELECT INTO</> syntax. Furthermore, <command>CREATE
139+
TABLE AS</command> offers a superset of the functionality offerred
140+
by <command>SELECT INTO</command>.
125141
</para>
126142

127143
<para>
128-
Prior to <productname>PostgreSQL</> 7.5, <command>CREATE TABLE
129-
AS</command> always included OIDs in the table it
144+
Prior to <productname>PostgreSQL</productname> 7.5, <command>CREATE
145+
TABLE AS</command> always included OIDs in the table it
130146
produced. Furthermore, these OIDs were newly generated: they were
131147
distinct from the OIDs of any of the rows in the source tables of
132148
the <command>SELECT</command> or <command>EXECUTE</command>
133149
statement. Therefore, if <command>CREATE TABLE AS</command> was
134150
frequently executed, the OID counter would be rapidly
135-
incremented. As of <productname>PostgreSQL</> 7.5, the inclusion of
136-
OIDs in the table generated by <command>CREATE TABLE AS</command>
137-
is controlled by the <varname>default_with_oids</varname>
138-
configuration variable. This variable currently defaults to true,
139-
but will likely default to false in a future release of
140-
<productname>PostgreSQL</>.
151+
incremented. As of <productname>PostgresSQL</productname> 7.5,
152+
the <command>CREATE TABLE AS</command> command allows the user to
153+
explicitely specify whether OIDs should be included. If the
154+
presence of OIDs is not explicitely specified,
155+
the <varname>default_with_oids</varname> configuration variable is
156+
used. While this variable currently defaults to true, the default
157+
value may be changed in the future. Therefore, applications that
158+
require OIDs in the table created by <command>CREATE TABLE
159+
AS</command> should explicitely specify <literal>WITH
160+
OIDS</literal> to ensure compatibility with future versions
161+
of <productname>PostgreSQL</productname>.
141162
</para>
142163
</refsect1>
143164

doc/src/sgml/runtime.sgml

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.229 2004/01/06 17:26:22 neilc Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/runtime.sgml,v 1.230 2004/01/10 23:28:43 neilc Exp $
33
-->
44

55
<Chapter Id="runtime">
@@ -2437,17 +2437,17 @@ dynamic_library_path = '/usr/local/lib/postgresql:/home/my_project/lib:$libdir'
24372437
<term><varname>default_with_oids</varname> (<type>boolean</type>)</term>
24382438
<listitem>
24392439
<para>
2440-
This controls whether <command>CREATE TABLE</command> will
2441-
include OIDs in newly-created tables, if neither <literal>WITH
2442-
OIDS</literal> or <literal>WITHOUT OIDS</literal> have been
2443-
specified. It also determines whether OIDs will be included in
2444-
the table generated by <command>SELECT INTO</command> and
2445-
<command>CREATE TABLE AS</command>. In
2440+
This controls whether <command>CREATE TABLE</command>
2441+
and <command>CREATE TABLE AS</command> will include OIDs in
2442+
newly-created tables, if neither <literal>WITH OIDS</literal>
2443+
or <literal>WITHOUT OIDS</literal> have been specified. It
2444+
also determines whether OIDs will be included in the table
2445+
created by <command>SELECT INTO</command>. In
24462446
<productname>PostgreSQL</productname> &version;
2447-
<varname>default_with_oids</varname> defaults to true. This is
2448-
also the behavior of previous versions of
2449-
<productname>PostgreSQL</productname>. However, assuming that
2450-
tables will contain OIDs by default is not
2447+
<varname>default_with_oids</varname> defaults to
2448+
true. This is also the behavior of previous versions
2449+
of <productname>PostgreSQL</productname>. However, assuming
2450+
that tables will contain OIDs by default is not
24512451
encouraged. Therefore, this option will default to false in a
24522452
future release of <productname>PostgreSQL</productname>.
24532453
</para>

src/backend/commands/sequence.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/sequence.c,v 1.107 2004/01/07 18:56:25 neilc Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/sequence.c,v 1.108 2004/01/10 23:28:44 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -178,7 +178,7 @@ DefineSequence(CreateSeqStmt *seq)
178178
stmt->relation = seq->sequence;
179179
stmt->inhRelations = NIL;
180180
stmt->constraints = NIL;
181-
stmt->hasoids = false;
181+
stmt->hasoids = MUST_NOT_HAVE_OIDS;
182182
stmt->oncommit = ONCOMMIT_NOOP;
183183

184184
seqoid = DefineRelation(stmt, RELKIND_SEQUENCE);

src/backend/commands/tablecmds.c

+21-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.94 2003/11/29 19:51:47 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/tablecmds.c,v 1.95 2004/01/10 23:28:44 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -47,6 +47,7 @@
4747
#include "utils/acl.h"
4848
#include "utils/builtins.h"
4949
#include "utils/fmgroids.h"
50+
#include "utils/guc.h"
5051
#include "utils/inval.h"
5152
#include "utils/lsyscache.h"
5253
#include "utils/relcache.h"
@@ -185,7 +186,25 @@ DefineRelation(CreateStmt *stmt, char relkind)
185186
*/
186187
descriptor = BuildDescForRelation(schema);
187188

188-
descriptor->tdhasoid = (stmt->hasoids || parentHasOids);
189+
if (parentHasOids)
190+
descriptor->tdhasoid = true;
191+
else
192+
{
193+
switch (stmt->hasoids)
194+
{
195+
case MUST_HAVE_OIDS:
196+
descriptor->tdhasoid = true;
197+
break;
198+
199+
case MUST_NOT_HAVE_OIDS:
200+
descriptor->tdhasoid = false;
201+
break;
202+
203+
case DEFAULT_OIDS:
204+
descriptor->tdhasoid = default_with_oids;
205+
break;
206+
}
207+
}
189208

190209
if (old_constraints != NIL)
191210
{

src/backend/commands/typecmds.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.51 2003/11/29 19:51:47 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/typecmds.c,v 1.52 2004/01/10 23:28:44 neilc Exp $
1212
*
1313
* DESCRIPTION
1414
* The "DefineFoo" routines take the parse tree and pick out the
@@ -1066,7 +1066,7 @@ DefineCompositeType(const RangeVar *typevar, List *coldeflist)
10661066
createStmt->tableElts = coldeflist;
10671067
createStmt->inhRelations = NIL;
10681068
createStmt->constraints = NIL;
1069-
createStmt->hasoids = false;
1069+
createStmt->hasoids = MUST_NOT_HAVE_OIDS;
10701070
createStmt->oncommit = ONCOMMIT_NOOP;
10711071

10721072
/*

src/backend/commands/view.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/view.c,v 1.79 2003/11/29 19:51:48 pgsql Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/view.c,v 1.80 2004/01/10 23:28:44 neilc Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -141,7 +141,7 @@ DefineVirtualRelation(const RangeVar *relation, List *tlist, bool replace)
141141
createStmt->tableElts = attrList;
142142
createStmt->inhRelations = NIL;
143143
createStmt->constraints = NIL;
144-
createStmt->hasoids = false;
144+
createStmt->hasoids = MUST_NOT_HAVE_OIDS;
145145
createStmt->oncommit = ONCOMMIT_NOOP;
146146

147147
/*

src/backend/executor/execMain.c

+2-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
*
2828
* IDENTIFICATION
29-
* $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.225 2004/01/07 18:56:26 neilc Exp $
29+
* $PostgreSQL: pgsql/src/backend/executor/execMain.c,v 1.226 2004/01/10 23:28:44 neilc Exp $
3030
*
3131
*-------------------------------------------------------------------------
3232
*/
@@ -593,14 +593,7 @@ InitPlan(QueryDesc *queryDesc, bool explainOnly)
593593
if (operation == CMD_SELECT && parseTree->into != NULL)
594594
{
595595
do_select_into = true;
596-
597-
/*
598-
* The presence of OIDs in the result set of SELECT INTO is
599-
* controlled by the default_with_oids GUC parameter. The
600-
* behavior in versions of PostgreSQL prior to 7.5 is to
601-
* always include OIDs.
602-
*/
603-
estate->es_force_oids = default_with_oids;
596+
estate->es_force_oids = parseTree->intoHasOids;
604597
}
605598

606599
/*

src/backend/nodes/copyfuncs.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.275 2004/01/06 23:55:18 tgl Exp $
18+
* $PostgreSQL: pgsql/src/backend/nodes/copyfuncs.c,v 1.276 2004/01/10 23:28:44 neilc Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -1549,6 +1549,7 @@ _copyQuery(Query *from)
15491549
COPY_NODE_FIELD(utilityStmt);
15501550
COPY_SCALAR_FIELD(resultRelation);
15511551
COPY_NODE_FIELD(into);
1552+
COPY_SCALAR_FIELD(intoHasOids);
15521553
COPY_SCALAR_FIELD(hasAggs);
15531554
COPY_SCALAR_FIELD(hasSubLinks);
15541555
COPY_NODE_FIELD(rtable);
@@ -1623,6 +1624,7 @@ _copySelectStmt(SelectStmt *from)
16231624
COPY_NODE_FIELD(distinctClause);
16241625
COPY_NODE_FIELD(into);
16251626
COPY_NODE_FIELD(intoColNames);
1627+
COPY_SCALAR_FIELD(intoHasOids);
16261628
COPY_NODE_FIELD(targetList);
16271629
COPY_NODE_FIELD(fromClause);
16281630
COPY_NODE_FIELD(whereClause);

src/backend/nodes/equalfuncs.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
2020
* IDENTIFICATION
21-
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.213 2004/01/06 23:55:18 tgl Exp $
21+
* $PostgreSQL: pgsql/src/backend/nodes/equalfuncs.c,v 1.214 2004/01/10 23:28:45 neilc Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -604,6 +604,7 @@ _equalQuery(Query *a, Query *b)
604604
COMPARE_NODE_FIELD(utilityStmt);
605605
COMPARE_SCALAR_FIELD(resultRelation);
606606
COMPARE_NODE_FIELD(into);
607+
COMPARE_SCALAR_FIELD(intoHasOids);
607608
COMPARE_SCALAR_FIELD(hasAggs);
608609
COMPARE_SCALAR_FIELD(hasSubLinks);
609610
COMPARE_NODE_FIELD(rtable);
@@ -667,6 +668,7 @@ _equalSelectStmt(SelectStmt *a, SelectStmt *b)
667668
COMPARE_NODE_FIELD(distinctClause);
668669
COMPARE_NODE_FIELD(into);
669670
COMPARE_NODE_FIELD(intoColNames);
671+
COMPARE_SCALAR_FIELD(intoHasOids);
670672
COMPARE_NODE_FIELD(targetList);
671673
COMPARE_NODE_FIELD(fromClause);
672674
COMPARE_NODE_FIELD(whereClause);

src/backend/parser/analyze.c

+17-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.293 2004/01/05 20:58:58 neilc Exp $
9+
* $PostgreSQL: pgsql/src/backend/parser/analyze.c,v 1.294 2004/01/10 23:28:45 neilc Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -41,6 +41,7 @@
4141
#include "utils/acl.h"
4242
#include "utils/builtins.h"
4343
#include "utils/fmgroids.h"
44+
#include "utils/guc.h"
4445
#include "utils/lsyscache.h"
4546
#include "utils/relcache.h"
4647
#include "utils/syscache.h"
@@ -1974,6 +1975,21 @@ transformSelectStmt(ParseState *pstate, SelectStmt *stmt)
19741975
if (stmt->intoColNames)
19751976
applyColumnNames(qry->targetList, stmt->intoColNames);
19761977

1978+
switch (stmt->intoHasOids)
1979+
{
1980+
case MUST_HAVE_OIDS:
1981+
qry->intoHasOids = true;
1982+
break;
1983+
1984+
case MUST_NOT_HAVE_OIDS:
1985+
qry->intoHasOids = false;
1986+
break;
1987+
1988+
case DEFAULT_OIDS:
1989+
qry->intoHasOids = default_with_oids;
1990+
break;
1991+
}
1992+
19771993
/* mark column origins */
19781994
markTargetListOrigins(pstate, qry->targetList);
19791995

0 commit comments

Comments
 (0)