Github user kavinderd commented on a diff in the pull request: https://github.com/apache/incubator-hawq/pull/1002#discussion_r88318669 --- Diff: src/backend/access/transam/varsup.c --- @@ -474,74 +483,128 @@ ResetExternalObjectId(void) /* * master_highest_used_oid - * Query the database to find the highest used Oid by + * Uses CAQL and SPI to find the highest used Oid among user and catalog tables + * + * Uses CAQL to query catalog tables + * Uses SPI to query user tables, because CAQL supports tables from CatCoreRelation array only * 1) Find all the relations that has Oids * 2) Find max oid from those relations */ Oid master_highest_used_oid(void) { + Oid oidMaxCatalog = InvalidOid; + Oid oidMaxUser = InvalidOid; Oid oidMax = InvalidOid; + Oid currentOid; + Form_pg_class classForm; + cqContext *pcqOuterCtx; + cqContext *pcqInnerCtx; + HeapTuple outerTuple; + HeapTuple innerTuple; + /* number of user tables having oids*/ + int userTablesNum = 0; + int ret; + + pcqOuterCtx = caql_beginscan(NULL, cql("SELECT * FROM pg_class WHERE relhasoids = :1", BoolGetDatum(true))); - if (SPI_OK_CONNECT != SPI_connect()) + outerTuple = caql_getnext(pcqOuterCtx); + + if (!HeapTupleIsValid(outerTuple)) { - ereport(ERROR, (errcode(ERRCODE_CDB_INTERNAL_ERROR), - errmsg("Unable to connect to execute internal query for HCatalog."))); + caql_endscan(pcqOuterCtx); + elog(DEBUG1, "Unable to get list of tables having oids"); + return oidMax; } - int ret = SPI_execute("SELECT relname FROM pg_class where relhasoids=true", true, 0); + /* construct query to get max oid from all tables with oids */ + StringInfo sqlstrCatalog = makeStringInfo(); + StringInfo sqlstrUser = makeStringInfo(); + appendStringInfo(sqlstrUser, "SELECT max(oid) FROM ("); + while (HeapTupleIsValid(outerTuple)) + { + classForm = (Form_pg_class) GETSTRUCT(outerTuple); - int rows = SPI_processed; + /* use CAQL for accessing catalog tables*/ + if (classForm->relnamespace == PG_CATALOG_NAMESPACE) + { + appendStringInfo(sqlstrCatalog, + "SELECT oid FROM %s WHERE oid > :1 ORDER BY oid", + classForm->relname.data); - char *tableNames[rows]; + pcqInnerCtx = caql_beginscan(NULL, + cql1(sqlstrCatalog->data, __FILE__, __LINE__, + ObjectIdGetDatum(oidMaxCatalog))); - if (rows == 0 || ret <= 0 || NULL == SPI_tuptable) - { - SPI_finish(); - return oidMax; - } + innerTuple = caql_getnext(pcqInnerCtx); - TupleDesc tupdesc = SPI_tuptable->tupdesc; - SPITupleTable *tuptable = SPI_tuptable; + currentOid = InvalidOid; - for (int i = 0; i < rows; i++) - { - HeapTuple tuple = tuptable->vals[i]; - tableNames[i] = SPI_getvalue(tuple, tupdesc, 1); - } + while (HeapTupleIsValid(innerTuple)) + { + currentOid = HeapTupleGetOid(innerTuple); + innerTuple = caql_getnext(pcqInnerCtx); --- End diff -- I see, that sucks
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---