pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #5988: CTINE duplicates constraints
5+ messages / 3 participants
[nested] [flat]
* BUG #5988: CTINE duplicates constraints
@ 2011-04-20 12:51 Marko Tiikkaja <marko.tiikkaja@2ndquadrant.com>
0 siblings, 1 reply; 5+ messages in thread
From: Marko Tiikkaja @ 2011-04-20 12:51 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged online:
Bug reference: 5988
Logged by: Marko Tiikkaja
Email address: marko.tiikkaja@2ndquadrant.com
PostgreSQL version: git master
Operating system: Linux
Description: CTINE duplicates constraints
Details:
CREATE TABLE IF NOT EXISTS duplicates some constraints if the new table
isn't created:
=# create table foo(a int primary key);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "foo_pkey"
for table "foo"
CREATE TABLE
=# create table if not exists foo(a int primary key);
NOTICE: relation "foo" already exists, skipping
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "foo_pkey1"
for table "foo"
CREATE TABLE
=# \d foo
Table "public.foo"
Column | Type | Modifiers
--------+---------+-----------
a | integer | not null
Indexes:
"foo_pkey" PRIMARY KEY, btree (a)
"foo_pkey1" PRIMARY KEY, btree (a)
It seems to do that at least for PRIMARY KEY and UNIQUE constraints.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #5988: CTINE duplicates constraints
@ 2011-04-20 23:13 Tom Lane <tgl@sss.pgh.pa.us>
parent: Marko Tiikkaja <marko.tiikkaja@2ndquadrant.com>
0 siblings, 1 reply; 5+ messages in thread
From: Tom Lane @ 2011-04-20 23:13 UTC (permalink / raw)
To: Marko Tiikkaja <marko.tiikkaja@2ndquadrant.com>; +Cc: pgsql-bugs
"Marko Tiikkaja" <marko.tiikkaja@2ndquadrant.com> writes:
> CREATE TABLE IF NOT EXISTS duplicates some constraints if the new table
> isn't created:
What this means is that the feature was implemented in the wrong place,
ie the short-circuit is after rather than before parse_utilcmds.c splits
up the CREATE TABLE command into multiple primitive actions.
I have stated previously my opinion that this is a misconceived feature,
and it's now apparent that the implementation is as poorly thought
through as the definition. My recommendation is to revert that patch
altogether.
(BTW, before anyone suggests that s/continue/break/ would fix it,
I suggest experimenting with a table containing a serial column.)
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #5988: CTINE duplicates constraints
@ 2011-04-25 14:57 Robert Haas <robertmhaas@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 5+ messages in thread
From: Robert Haas @ 2011-04-25 14:57 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Marko Tiikkaja <marko.tiikkaja@2ndquadrant.com>; pgsql-bugs
On Wed, Apr 20, 2011 at 7:13 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> "Marko Tiikkaja" <marko.tiikkaja@2ndquadrant.com> writes:
>> CREATE TABLE IF NOT EXISTS duplicates some constraints if the new table
>> isn't created:
>
> What this means is that the feature was implemented in the wrong place,
> ie the short-circuit is after rather than before parse_utilcmds.c splits
> up the CREATE TABLE command into multiple primitive actions.
>
> I have stated previously my opinion that this is a misconceived feature,
> and it's now apparent that the implementation is as poorly thought
> through as the definition. My recommendation is to revert that patch
> altogether.
>
> (BTW, before anyone suggests that s/continue/break/ would fix it,
> I suggest experimenting with a table containing a serial column.)
IIRC, quite a few people voiced support for this feature, so I think
that ripping it out because you don't personally like it is not a good
solution. That is exactly the sort of thing that gives us a
reputation for parochialism. In fact, I believe that the person in
response to whose complaint I wrote this patch had exactly that
complaint.
Having said that, it's not altogether obvious to me what a good fix
for this problem would look like. The reason why the check isn't done
before transformCreateStmt() is because we don't decide on the
creation namespace until we get down to DefineRelation(). We could
duplicate that logic prior to calling transformCreateStmt(). That
would rely on getting the same answer both times, but apparently we're
relying on that anyway in the case of a table with a serial column;
else we'd be at risk of putting the sequence and table in different
schemas. To avoid information leakage, we'd also need to duplicate
the permissions-checking logic that immediately follows, which is a
little ugly... but maybe not too ugly, if we encapsulate the whole
thing in a function that gets called from multiple places, rather than
duplicating the code.
In fact, it looks like we already have a bit of information leakage here:
rhaas=> create table secret.foo (a serial);
NOTICE: CREATE TABLE will create implicit sequence "foo_a_seq2" for
serial column "foo.a"
ERROR: permission denied for schema secret
We can infer the existence of foo_a_seq and foo_a_seq1.
It seems like a better solution here would be to derive the creation
namespace before we break up the statement into subcommands, and pass
the OID around after that. Then we wouldn't be doing it multiple
times and hoping to get the same answer, and the permissions checking
would be happening at the beginning of the process instead of at some
point in the middle, which is really the root cause of the above
information leak.
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #5988: CTINE duplicates constraints
@ 2011-04-25 15:06 Tom Lane <tgl@sss.pgh.pa.us>
parent: Robert Haas <robertmhaas@gmail.com>
0 siblings, 1 reply; 5+ messages in thread
From: Tom Lane @ 2011-04-25 15:06 UTC (permalink / raw)
To: Robert Haas <robertmhaas@gmail.com>; +Cc: Marko Tiikkaja <marko.tiikkaja@2ndquadrant.com>; pgsql-bugs
Robert Haas <robertmhaas@gmail.com> writes:
> On Wed, Apr 20, 2011 at 7:13 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> I have stated previously my opinion that this is a misconceived feature,
>> and it's now apparent that the implementation is as poorly thought
>> through as the definition. My recommendation is to revert that patch
>> altogether.
> IIRC, quite a few people voiced support for this feature, so I think
> that ripping it out because you don't personally like it is not a good
> solution.
I will not stand in the way of someone else coming up with a less broken
implementation. But as you've noted, that seems to be a somewhat less
than trivial project. And time grows short. I don't think it's
unreasonable at all to pull this feature from 9.1 and let someone who
cares about it submit a rewritten patch for 9.2.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #5988: CTINE duplicates constraints
@ 2011-04-25 17:18 Robert Haas <robertmhaas@gmail.com>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 0 replies; 5+ messages in thread
From: Robert Haas @ 2011-04-25 17:18 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: Marko Tiikkaja <marko.tiikkaja@2ndquadrant.com>; pgsql-bugs
On Mon, Apr 25, 2011 at 11:06 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
> Robert Haas <robertmhaas@gmail.com> writes:
>> On Wed, Apr 20, 2011 at 7:13 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>>> I have stated previously my opinion that this is a misconceived feature,
>>> and it's now apparent that the implementation is as poorly thought
>>> through as the definition. My recommendation is to revert that patch
>>> altogether.
>
>> IIRC, quite a few people voiced support for this feature, so I think
>> that ripping it out because you don't personally like it is not a good
>> solution.
>
> I will not stand in the way of someone else coming up with a less broken
> implementation. But as you've noted, that seems to be a somewhat less
> than trivial project. And time grows short. I don't think it's
> unreasonable at all to pull this feature from 9.1 and let someone who
> cares about it submit a rewritten patch for 9.2.
Well, I'd like to make at least some minimal effort to see whether we
can fix it before we give up on it completely. A little poking around
suggests that this actually isn't that hard. Attached please find a
proposed patch which fixes the Marko's original complaint, the related
problem with serial columns that you noted, and the information leak I
noted of in my previous reply. The guts of the change are in
transformCreateStmt(). It might eventually be worth doing some more
extensive refactoring in this area, but this seems good enough for
now.
--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
Attachments:
fix-ctine.patch (application/octet-stream, 12.6K) download
*** a/src/backend/bootstrap/bootparse.y
--- b/src/backend/bootstrap/bootparse.y
***************
*** 247,254 **** Boot_CreateStmt:
ONCOMMIT_NOOP,
(Datum) 0,
false,
! true,
! false);
elog(DEBUG4, "relation created with oid %u", id);
}
do_end();
--- 247,253 ----
ONCOMMIT_NOOP,
(Datum) 0,
false,
! true);
elog(DEBUG4, "relation created with oid %u", id);
}
do_end();
*** a/src/backend/catalog/heap.c
--- b/src/backend/catalog/heap.c
***************
*** 973,980 **** heap_create_with_catalog(const char *relname,
OnCommitAction oncommit,
Datum reloptions,
bool use_user_acl,
! bool allow_system_table_mods,
! bool if_not_exists)
{
Relation pg_class_desc;
Relation new_rel_desc;
--- 973,979 ----
OnCommitAction oncommit,
Datum reloptions,
bool use_user_acl,
! bool allow_system_table_mods)
{
Relation pg_class_desc;
Relation new_rel_desc;
***************
*** 994,1019 **** heap_create_with_catalog(const char *relname,
CheckAttributeNamesTypes(tupdesc, relkind, allow_system_table_mods);
/*
! * If the relation already exists, it's an error, unless the user
! * specifies "IF NOT EXISTS". In that case, we just print a notice and do
! * nothing further.
*/
existing_relid = get_relname_relid(relname, relnamespace);
if (existing_relid != InvalidOid)
- {
- if (if_not_exists)
- {
- ereport(NOTICE,
- (errcode(ERRCODE_DUPLICATE_TABLE),
- errmsg("relation \"%s\" already exists, skipping",
- relname)));
- heap_close(pg_class_desc, RowExclusiveLock);
- return InvalidOid;
- }
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_TABLE),
errmsg("relation \"%s\" already exists", relname)));
- }
/*
* Since we are going to create a rowtype as well, also check for
--- 993,1006 ----
CheckAttributeNamesTypes(tupdesc, relkind, allow_system_table_mods);
/*
! * This would fail later on anyway, if the relation already exists. But
! * by catching it here we can emit a nicer error message.
*/
existing_relid = get_relname_relid(relname, relnamespace);
if (existing_relid != InvalidOid)
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_TABLE),
errmsg("relation \"%s\" already exists", relname)));
/*
* Since we are going to create a rowtype as well, also check for
*** a/src/backend/catalog/namespace.c
--- b/src/backend/catalog/namespace.c
***************
*** 361,366 **** RangeVarGetCreationNamespace(const RangeVar *newRelation)
--- 361,395 ----
}
/*
+ * RangeVarGetAndCheckCreationNamespace
+ * As RangeVarGetCreationNamespace, but with a permissions check.
+ */
+ Oid
+ RangeVarGetAndCheckCreationNamespace(const RangeVar *newRelation)
+ {
+ Oid namespaceId;
+
+ namespaceId = RangeVarGetCreationNamespace(newRelation);
+
+ /*
+ * Check we have permission to create there. Skip check if bootstrapping,
+ * since permissions machinery may not be working yet.
+ */
+ if (!IsBootstrapProcessingMode())
+ {
+ AclResult aclresult;
+
+ aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
+ ACL_CREATE);
+ if (aclresult != ACLCHECK_OK)
+ aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
+ get_namespace_name(namespaceId));
+ }
+
+ return namespaceId;
+ }
+
+ /*
* RelnameGetRelid
* Try to resolve an unqualified relation name.
* Returns OID if relation found in search path, else InvalidOid.
*** a/src/backend/catalog/toasting.c
--- b/src/backend/catalog/toasting.c
***************
*** 226,233 **** create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptio
ONCOMMIT_NOOP,
reloptions,
false,
! true,
! false);
Assert(toast_relid != InvalidOid);
/* make the toast relation visible, else heap_open will fail */
--- 226,232 ----
ONCOMMIT_NOOP,
reloptions,
false,
! true);
Assert(toast_relid != InvalidOid);
/* make the toast relation visible, else heap_open will fail */
*** a/src/backend/commands/cluster.c
--- b/src/backend/commands/cluster.c
***************
*** 646,653 **** make_new_heap(Oid OIDOldHeap, Oid NewTableSpace)
ONCOMMIT_NOOP,
reloptions,
false,
! true,
! false);
Assert(OIDNewHeap != InvalidOid);
ReleaseSysCache(tuple);
--- 646,652 ----
ONCOMMIT_NOOP,
reloptions,
false,
! true);
Assert(OIDNewHeap != InvalidOid);
ReleaseSysCache(tuple);
*** a/src/backend/commands/tablecmds.c
--- b/src/backend/commands/tablecmds.c
***************
*** 439,460 **** DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId)
errmsg("cannot create temporary table within security-restricted operation")));
/*
! * Look up the namespace in which we are supposed to create the relation.
! * Check we have permission to create there. Skip check if bootstrapping,
! * since permissions machinery may not be working yet.
*/
! namespaceId = RangeVarGetCreationNamespace(stmt->relation);
!
! if (!IsBootstrapProcessingMode())
! {
! AclResult aclresult;
!
! aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
! ACL_CREATE);
! if (aclresult != ACLCHECK_OK)
! aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
! get_namespace_name(namespaceId));
! }
/*
* Select tablespace to use. If not specified, use default tablespace
--- 439,448 ----
errmsg("cannot create temporary table within security-restricted operation")));
/*
! * Look up the namespace in which we are supposed to create the relation,
! * and check we have permission to create there.
*/
! namespaceId = RangeVarGetAndCheckCreationNamespace(stmt->relation);
/*
* Select tablespace to use. If not specified, use default tablespace
***************
*** 602,617 **** DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId)
stmt->oncommit,
reloptions,
true,
! allowSystemTableMods,
! stmt->if_not_exists);
!
! /*
! * If heap_create_with_catalog returns InvalidOid, it means that the user
! * specified "IF NOT EXISTS" and the relation already exists. In that
! * case we do nothing further.
! */
! if (relationId == InvalidOid)
! return InvalidOid;
/* Store inheritance information for new rel. */
StoreCatalogInheritance(relationId, inheritOids);
--- 590,596 ----
stmt->oncommit,
reloptions,
true,
! allowSystemTableMods);
/* Store inheritance information for new rel. */
StoreCatalogInheritance(relationId, inheritOids);
*** a/src/backend/executor/execMain.c
--- b/src/backend/executor/execMain.c
***************
*** 2341,2347 **** OpenIntoRel(QueryDesc *queryDesc)
Oid namespaceId;
Oid tablespaceId;
Datum reloptions;
- AclResult aclresult;
Oid intoRelationId;
TupleDesc tupdesc;
DR_intorel *myState;
--- 2341,2346 ----
***************
*** 2378,2390 **** OpenIntoRel(QueryDesc *queryDesc)
* Find namespace to create in, check its permissions
*/
intoName = into->rel->relname;
! namespaceId = RangeVarGetCreationNamespace(into->rel);
!
! aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(),
! ACL_CREATE);
! if (aclresult != ACLCHECK_OK)
! aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
! get_namespace_name(namespaceId));
/*
* Select tablespace to use. If not specified, use default tablespace
--- 2377,2383 ----
* Find namespace to create in, check its permissions
*/
intoName = into->rel->relname;
! namespaceId = RangeVarGetAndCheckCreationNamespace(into->rel);
/*
* Select tablespace to use. If not specified, use default tablespace
***************
*** 2444,2451 **** OpenIntoRel(QueryDesc *queryDesc)
into->onCommit,
reloptions,
true,
! allowSystemTableMods,
! false);
Assert(intoRelationId != InvalidOid);
FreeTupleDesc(tupdesc);
--- 2437,2443 ----
into->onCommit,
reloptions,
true,
! allowSystemTableMods);
Assert(intoRelationId != InvalidOid);
FreeTupleDesc(tupdesc);
*** a/src/backend/parser/parse_utilcmd.c
--- b/src/backend/parser/parse_utilcmd.c
***************
*** 148,153 **** transformCreateStmt(CreateStmt *stmt, const char *queryString)
--- 148,154 ----
List *result;
List *save_alist;
ListCell *elements;
+ Oid namespaceid;
/*
* We must not scribble on the passed-in CreateStmt, so copy it. (This is
***************
*** 156,161 **** transformCreateStmt(CreateStmt *stmt, const char *queryString)
--- 157,189 ----
stmt = (CreateStmt *) copyObject(stmt);
/*
+ * Look up the creation namespace. This also checks permissions on the
+ * target namespace, so that we throw any permissions error as early as
+ * possible.
+ */
+ namespaceid = RangeVarGetAndCheckCreationNamespace(stmt->relation);
+
+ /*
+ * If the relation already exists and the user specified "IF NOT EXISTS",
+ * bail out with a NOTICE.
+ */
+ if (stmt->if_not_exists)
+ {
+ Oid existing_relid;
+
+ existing_relid = get_relname_relid(stmt->relation->relname,
+ namespaceid);
+ if (existing_relid != InvalidOid)
+ {
+ ereport(NOTICE,
+ (errcode(ERRCODE_DUPLICATE_TABLE),
+ errmsg("relation \"%s\" already exists, skipping",
+ stmt->relation->relname)));
+ return NIL;
+ }
+ }
+
+ /*
* If the target relation name isn't schema-qualified, make it so. This
* prevents some corner cases in which added-on rewritten commands might
* think they should apply to other relations that have the same name and
***************
*** 164,174 **** transformCreateStmt(CreateStmt *stmt, const char *queryString)
*/
if (stmt->relation->schemaname == NULL
&& stmt->relation->relpersistence != RELPERSISTENCE_TEMP)
- {
- Oid namespaceid = RangeVarGetCreationNamespace(stmt->relation);
-
stmt->relation->schemaname = get_namespace_name(namespaceid);
- }
/* Set up pstate and CreateStmtContext */
pstate = make_parsestate(NULL);
--- 192,198 ----
*** a/src/backend/tcop/utility.c
--- b/src/backend/tcop/utility.c
***************
*** 530,542 **** standard_ProcessUtility(Node *parsetree,
InvalidOid);
/*
- * If "IF NOT EXISTS" was specified and the relation
- * already exists, do nothing further.
- */
- if (relOid == InvalidOid)
- continue;
-
- /*
* Let AlterTableCreateToastTable decide if this one
* needs a secondary relation too.
*/
--- 530,535 ----
***************
*** 559,573 **** standard_ProcessUtility(Node *parsetree,
relOid = DefineRelation((CreateStmt *) stmt,
RELKIND_FOREIGN_TABLE,
InvalidOid);
!
! /*
! * Unless "IF NOT EXISTS" was specified and the
! * relation already exists, create the
! * pg_foreign_table entry.
! */
! if (relOid != InvalidOid)
! CreateForeignTable((CreateForeignTableStmt *) stmt,
! relOid);
}
else
{
--- 552,559 ----
relOid = DefineRelation((CreateStmt *) stmt,
RELKIND_FOREIGN_TABLE,
InvalidOid);
! CreateForeignTable((CreateForeignTableStmt *) stmt,
! relOid);
}
else
{
*** a/src/include/catalog/heap.h
--- b/src/include/catalog/heap.h
***************
*** 63,70 **** extern Oid heap_create_with_catalog(const char *relname,
OnCommitAction oncommit,
Datum reloptions,
bool use_user_acl,
! bool allow_system_table_mods,
! bool if_not_exists);
extern void heap_drop_with_catalog(Oid relid);
--- 63,69 ----
OnCommitAction oncommit,
Datum reloptions,
bool use_user_acl,
! bool allow_system_table_mods);
extern void heap_drop_with_catalog(Oid relid);
*** a/src/include/catalog/namespace.h
--- b/src/include/catalog/namespace.h
***************
*** 49,54 **** typedef struct OverrideSearchPath
--- 49,55 ----
extern Oid RangeVarGetRelid(const RangeVar *relation, bool failOK);
extern Oid RangeVarGetCreationNamespace(const RangeVar *newRelation);
+ extern Oid RangeVarGetAndCheckCreationNamespace(const RangeVar *newRelation);
extern Oid RelnameGetRelid(const char *relname);
extern bool RelationIsVisible(Oid relid);
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2011-04-25 17:18 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-04-20 12:51 BUG #5988: CTINE duplicates constraints Marko Tiikkaja <marko.tiikkaja@2ndquadrant.com>
2011-04-20 23:13 ` Tom Lane <tgl@sss.pgh.pa.us>
2011-04-25 14:57 ` Robert Haas <robertmhaas@gmail.com>
2011-04-25 15:06 ` Tom Lane <tgl@sss.pgh.pa.us>
2011-04-25 17:18 ` Robert Haas <robertmhaas@gmail.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