Github user hornn commented on a diff in the pull request:

    https://github.com/apache/incubator-hawq/pull/1002#discussion_r86451395
  
    --- Diff: src/backend/access/transam/varsup.c ---
    @@ -474,73 +479,53 @@ ResetExternalObjectId(void)
     
     /*
      * master_highest_used_oid
    - *                 Query the database to find the highest used Oid by
    + *                 Uses CAQL to find the highest used Oid by
      *                 1) Find all the relations that has Oids
      *                 2) Find max oid from those relations
      */
     Oid
     master_highest_used_oid(void)
     {
        Oid oidMax = InvalidOid;
    +   Oid currentOid;
    +   Form_pg_class classForm;
    +   int fetchCount;
     
    -   if (SPI_OK_CONNECT != SPI_connect())
    -   {
    -           ereport(ERROR, (errcode(ERRCODE_CDB_INTERNAL_ERROR),
    -                           errmsg("Unable to connect to execute internal 
query for HCatalog.")));
    -   }
    -
    -   int ret = SPI_execute("SELECT relname FROM pg_class where 
relhasoids=true", true, 0);
    +   cqContext *pcqOuterCtx = caql_beginscan(
    +                   NULL,
    +                   cql("SELECT * FROM pg_class where relhasoids = :1",
    +                                   BoolGetDatum(true)));
     
    -   int rows = SPI_processed;
    +   HeapTuple tuple = caql_getnext(pcqOuterCtx);
     
    -   char *tableNames[rows];
    -
    -   if (rows == 0 || ret <= 0 || NULL == SPI_tuptable)
    +   if (!HeapTupleIsValid(tuple))
        {
    -           SPI_finish();
    +           caql_endscan(pcqOuterCtx);
    +           elog(DEBUG1, "Unable to get list of tables having oids");
                return oidMax;
        }
     
    -   TupleDesc tupdesc = SPI_tuptable->tupdesc;
    -   SPITupleTable *tuptable = SPI_tuptable;
    -
    -   for (int i = 0; i < rows; i++)
    -   {
    -           HeapTuple tuple = tuptable->vals[i];
    -           tableNames[i] = SPI_getvalue(tuple, tupdesc, 1);
    -   }
    -
        /* construct query to get max oid from all tables with oids */
    -   StringInfoData sqlstr;
    -   initStringInfo(&sqlstr);
    -   appendStringInfo(&sqlstr, "SELECT max(oid) FROM (");
    -   for (int i = 0; i < rows; i++)
    +   StringInfo sqlstr = makeStringInfo();
    +   while (HeapTupleIsValid(tuple))
        {
    -           if (i > 0)
    -           {
    -                   appendStringInfo(&sqlstr, " UNION ALL ");
    -           }
    -           appendStringInfo(&sqlstr, "SELECT max(oid) AS oid FROM %s", 
tableNames[i]);
    -   }
    -   appendStringInfo(&sqlstr, ") AS x");
    +           classForm = (Form_pg_class) GETSTRUCT(tuple);
    +           appendStringInfo(sqlstr, "SELECT oid FROM %s WHERE oid >= :1 
ORDER BY oid", classForm->relname.data);
    --- End diff --
    
    why not use `MAX(oid)` like it was before? is it because it's not supported 
by caql?
    Also, maybe add `LIMIT 1` to the query because we only need the first row.


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to