From d28a571efdb2e5aeb6efecefeda8b7febe36377c Mon Sep 17 00:00:00 2001 From: openGaussDev Date: Fri, 4 Mar 2022 15:57:38 +0800 Subject: [PATCH] submit code to openGauss-server Offering: openGaussDev More detail: fix owner for fdw Signed-off-by:chenbobo Match-id-0cdd43e2bf4f91363b42dec95cd648e902b88bd5 --- contrib/postgres_fdw/deparse.cpp | 48 +++++++++++++++++--------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/contrib/postgres_fdw/deparse.cpp b/contrib/postgres_fdw/deparse.cpp index d4cc79a29..12fd99853 100644 --- a/contrib/postgres_fdw/deparse.cpp +++ b/contrib/postgres_fdw/deparse.cpp @@ -1151,7 +1151,6 @@ static void deparseRelation(StringInfo buf, Relation rel) const char *nspname = NULL; const char *relname = NULL; ListCell *lc; - char parttype = PARTTYPE_NON_PARTITIONED_RELATION; /* obtain additional catalog information. */ ForeignTable* table = GetForeignTable(RelationGetRelid(rel)); @@ -1180,31 +1179,34 @@ static void deparseRelation(StringInfo buf, Relation rel) relname = RelationGetRelationName(rel); } - /* foreign table could not be built from a partitioned table */ - UserMapping* user = GetUserMapping(rel->rd_rel->relowner, table->serverid); - ForeignServer *server = GetForeignServer(table->serverid); - PGconn* conn = GetConnection(server, user, false); + /* In current version, there are some unpredictable operations (delete/update, etc.) of foreign table built on + * partitioned table. We forbid all operations in this condition by default. */ + if (!ENABLE_SQL_BETA_FEATURE(PARTITION_FDW_ON)) { + char parttype = PARTTYPE_NON_PARTITIONED_RELATION; + UserMapping* user = GetUserMapping(GetUserId(), table->serverid); + ForeignServer *server = GetForeignServer(table->serverid); + PGconn* conn = GetConnection(server, user, false); - PQExpBuffer query = createPQExpBuffer(); - appendPQExpBuffer(query, - "SELECT c.parttype FROM pg_class c, pg_namespace n " - "WHERE c.relname = '%s' and c.relnamespace = n.oid and n.nspname = '%s'", - quote_identifier(relname), quote_identifier(nspname)); + PQExpBuffer query = createPQExpBuffer(); + appendPQExpBuffer(query, + "SELECT c.parttype FROM pg_class c, pg_namespace n " + "WHERE c.relname = '%s' and c.relnamespace = n.oid and n.nspname = '%s'", + quote_identifier(relname), quote_identifier(nspname)); - PGresult* res = pgfdw_exec_query(conn, query->data); - if (PQresultStatus(res) != PGRES_TUPLES_OK) { - pgfdw_report_error(ERROR, res, conn, true, query->data); - } - /* res may be empty as the relname/nspname validation is not checked */ - if (PQntuples(res) > 0) { - parttype = *PQgetvalue(res, 0, 0); - } - PQclear(res); - destroyPQExpBuffer(query); + PGresult* res = pgfdw_exec_query(conn, query->data); + if (PQresultStatus(res) != PGRES_TUPLES_OK) { + pgfdw_report_error(ERROR, res, conn, true, query->data); + } + /* res may be empty as the relname/nspname validation is not checked */ + if (PQntuples(res) > 0) { + parttype = *PQgetvalue(res, 0, 0); + } + PQclear(res); + destroyPQExpBuffer(query); - if (!ENABLE_SQL_BETA_FEATURE(PARTITION_FDW_ON) && - (parttype == PARTTYPE_PARTITIONED_RELATION || parttype == PARTTYPE_SUBPARTITIONED_RELATION)) { - ereport(ERROR, (errmsg("could not operate foreign table on partitioned table"))); + if ((parttype == PARTTYPE_PARTITIONED_RELATION || parttype == PARTTYPE_SUBPARTITIONED_RELATION)) { + ereport(ERROR, (errmsg("could not operate foreign table on partitioned table"))); + } } appendStringInfo(buf, "%s.%s", quote_identifier(nspname), quote_identifier(relname));