andr-sokolov commented on code in PR #1842:
URL: https://github.com/apache/cloudberry/pull/1842#discussion_r3528533057
##########
src/backend/commands/foreigncmds.c:
##########
@@ -1026,6 +1028,266 @@ CreateForeignServer(CreateForeignServerStmt *stmt)
}
+/*
+ * Create a foreign catalog
+ */
+ObjectAddress
+CreateForeignCatalog(CreateForeignCatalogStmt *stmt)
+{
+ Relation rel;
+ Datum catalogoptions;
+ Datum values[Natts_pg_foreign_catalog];
+ bool nulls[Natts_pg_foreign_catalog];
+ HeapTuple tuple;
+ Oid catalogId;
+ Oid ownerId;
+ AclResult aclresult;
+ ObjectAddress myself;
+ ObjectAddress referenced;
+ ForeignServer *server;
+
+ rel = table_open(ForeignCatalogRelationId, RowExclusiveLock);
+
+ /* For now the owner cannot be specified on create. Use effective user
ID. */
+ ownerId = GetUserId();
+
+ /*
+ * Check that there is no other foreign catalog by this name. Catalog
+ * names are global (like server names): every reference syntax (DROP
+ * CATALOG, the CATALOG clause of CREATE ICEBERG TABLE, GUCs) identifies
+ * a catalog by bare name, so the name alone must be unique. If there
is
+ * one, do nothing if IF NOT EXISTS was specified.
+ */
+ catalogId = get_foreign_catalog_oid(stmt->catalogname, true);
+ if (OidIsValid(catalogId))
+ {
+ if (stmt->if_not_exists)
+ {
+ /*
+ * If we are in an extension script, insist that the
pre-existing
+ * object be a member of the extension, to avoid
security risks.
+ */
+ ObjectAddressSet(myself, ForeignCatalogRelationId,
catalogId);
+ checkMembershipInCurrentExtension(&myself);
+
+ /* OK to skip */
+ ereport(NOTICE,
+ (errcode(ERRCODE_DUPLICATE_OBJECT),
+ errmsg("foreign catalog \"%s\" already
exists, skipping",
+ stmt->catalogname)));
+ table_close(rel, RowExclusiveLock);
+ return InvalidObjectAddress;
+ }
+ else
+ ereport(ERROR,
+ (errcode(ERRCODE_DUPLICATE_OBJECT),
+ errmsg("foreign catalog \"%s\" already
exists",
+ stmt->catalogname)));
Review Comment:
I suggest to invert the condition to reduce indentation and get rid of
`else`. Here and in `CreateForeignVolume` too.
```suggestion
if (!stmt->if_not_exists)
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg("foreign catalog \"%s\" already
exists",
stmt->catalogname)));
/*
* If we are in an extension script, insist that the
pre-existing
* object be a member of the extension, to avoid security risks.
*/
ObjectAddressSet(myself, ForeignCatalogRelationId, catalogId);
checkMembershipInCurrentExtension(&myself);
/* OK to skip */
ereport(NOTICE,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg("foreign catalog \"%s\" already exists,
skipping",
stmt->catalogname)));
table_close(rel, RowExclusiveLock);
return InvalidObjectAddress;
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]