static void createJoinCondition (Query *query, SublinkInfo *info, bool isTargetRewrite) { JoinExpr *join; Node *condition; Node *Csub; Node *CsubPlus; if (info->sublink->subLinkType == ANY_SUBLINK || info->sublink->subLinkType == ALL_SUBLINK) { /* generate Csub and CsubPlus from sublink condition */ if (info->targetVar) { Csub = copyObject(info->targetVar); } else { Csub = generateCsub (info); } CsubPlus = generateCsubPlus (info, list_length(query->rtable) - 1); /* create condition */ if (info->sublink->subLinkType == ANY_SUBLINK) { /* C_sub' OR NOT C_sub */ condition = (Node *) makeBoolExpr(NOT_EXPR, list_make1(Csub)); condition = (Node *) makeBoolExpr(OR_EXPR, list_make2(CsubPlus, condition)); } if (info->sublink->subLinkType == ALL_SUBLINK) { /* C_sub OR NOT C_sub' */ condition = (Node *) makeBoolExpr(NOT_EXPR, list_make1(CsubPlus)); condition = (Node *) makeBoolExpr(OR_EXPR, list_make2(Csub, condition)); } } else { condition = makeBoolConst(true, false); } if (list_length(query->rtable) > 1) { join = (JoinExpr *) linitial(query->jointree->fromlist); join->quals = condition; } else { query->jointree->quals = condition; } }
/* * Output an objname/objargs representation for the procedure with the * given OID. If it doesn't exist, an error is thrown. * * This can be used to feed get_object_address. */ void format_procedure_parts(Oid procedure_oid, List **objnames, List **objargs) { HeapTuple proctup; Form_pg_proc procform; int nargs; int i; proctup = SearchSysCache1(PROCOID, ObjectIdGetDatum(procedure_oid)); if (!HeapTupleIsValid(proctup)) elog(ERROR, "cache lookup failed for procedure with OID %u", procedure_oid); procform = (Form_pg_proc) GETSTRUCT(proctup); nargs = procform->pronargs; *objnames = list_make2(get_namespace_name_or_temp(procform->pronamespace), pstrdup(NameStr(procform->proname))); *objargs = NIL; for (i = 0; i < nargs; i++) { Oid thisargtype = procform->proargtypes.values[i]; *objargs = lappend(*objargs, format_type_be_qualified(thisargtype)); } ReleaseSysCache(proctup); }
/* * GUC array: one invalid guc + non-userset guc * return ArrayType contain non-userset guc, ignore invalid guc */ void test__GUCArrayReset__invalid_guc(void **state) { ArrayType *in; ArrayType *out; Datum d; List *guc_list; int elems; build_guc_variables(); will_return(superuser, false); /* construct text array */ elems = 2; guc_list = list_make2("invalid_guc=true", "gp_log_format=text"); in = create_guc_array(guc_list, elems); out = GUCArrayReset(in); assert_not_null(out); assert_int_equal(ARR_DIMS(out)[0], 1); d = PointerGetDatum(ARR_DATA_PTR(out)); assert_int_equal(strlen("gp_log_format=text"), VARLEN(d)); assert_memory_equal(VARDATA(d), "gp_log_format=text", VARLEN(d)); list_free(guc_list); pfree(in); pfree(out); }
static Oid GetFuncOid(char *s) { FuncCandidateList fcl=FuncnameGetCandidates( list_make2(makeString("provsql"),makeString(s)),-1,NIL,false,false,false); if(fcl) return fcl->oid; else return 0; }
/* * Return permissive policies to be added */ List * test_rls_hooks_permissive(CmdType cmdtype, Relation relation) { List *policies = NIL; RowSecurityPolicy *policy = palloc0(sizeof(RowSecurityPolicy)); Datum role; FuncCall *n; Node *e; ColumnRef *c; ParseState *qual_pstate; RangeTblEntry *rte; if (strcmp(RelationGetRelationName(relation), "rls_test_permissive") && strcmp(RelationGetRelationName(relation), "rls_test_both")) return NIL; qual_pstate = make_parsestate(NULL); rte = addRangeTableEntryForRelation(qual_pstate, relation, NULL, false, false); addRTEtoQuery(qual_pstate, rte, false, true, true); role = ObjectIdGetDatum(ACL_ID_PUBLIC); policy->policy_name = pstrdup("extension policy"); policy->policy_id = InvalidOid; policy->polcmd = '*'; policy->roles = construct_array(&role, 1, OIDOID, sizeof(Oid), true, 'i'); /* * policy->qual = (Expr *) makeConst(BOOLOID, -1, InvalidOid, * sizeof(bool), BoolGetDatum(true), false, true); */ n = makeFuncCall(list_make2(makeString("pg_catalog"), makeString("current_user")), NIL, 0); c = makeNode(ColumnRef); c->fields = list_make1(makeString("username")); c->location = 0; e = (Node *) makeSimpleA_Expr(AEXPR_OP, "=", (Node *) n, (Node *) c, 0); policy->qual = (Expr *) transformWhereClause(qual_pstate, copyObject(e), EXPR_KIND_POLICY, "POLICY"); policy->with_check_qual = copyObject(policy->qual); policy->hassublinks = false; policies = list_make1(policy); return policies; }
/* * prune_using_either_value returns the shards for the specified distributed * table after pruning using either of two values provided by the caller (OR). */ Datum prune_using_either_value(PG_FUNCTION_ARGS) { Oid distributedTableId = PG_GETARG_OID(0); text *firstValue = PG_GETARG_TEXT_P(1); text *secondValue = PG_GETARG_TEXT_P(2); Expr *firstQual = MakeTextPartitionExpression(distributedTableId, firstValue); Expr *secondQual = MakeTextPartitionExpression(distributedTableId, secondValue); Expr *orClause = make_orclause(list_make2(firstQual, secondQual)); List *whereClauseList = list_make1(orClause); ArrayType *shardIdArrayType = PrunedShardIdsForTable(distributedTableId, whereClauseList); PG_RETURN_ARRAYTYPE_P(shardIdArrayType); }
void format_operator_parts(Oid operator_oid, List **objnames, List **objargs) { HeapTuple opertup; Form_pg_operator oprForm; opertup = SearchSysCache1(OPEROID, ObjectIdGetDatum(operator_oid)); if (!HeapTupleIsValid(opertup)) elog(ERROR, "cache lookup failed for operator with OID %u", operator_oid); oprForm = (Form_pg_operator) GETSTRUCT(opertup); *objnames = list_make2(get_namespace_name_or_temp(oprForm->oprnamespace), pstrdup(NameStr(oprForm->oprname))); *objargs = NIL; if (oprForm->oprleft) *objargs = lappend(*objargs, format_type_be_qualified(oprForm->oprleft)); if (oprForm->oprright) *objargs = lappend(*objargs, format_type_be_qualified(oprForm->oprright)); ReleaseSysCache(opertup); }
/* * make_opr() * Operator expression construction. * * Transform operator expression ensuring type compatibility. * This is where some type conversion happens. * * As with coerce_type, pstate may be NULL if no special unknown-Param * processing is wanted. */ expr_n* make_opr(parse_state_s *pstate, struct list *opname, node_n *ltree, node_n *rtree, int location) { oid_t ltypeId; oid_t rtypeId; Operator tup; Form_pg_operator opform; oid_t actual_arg_types[2]; oid_t declared_arg_types[2]; int nargs; struct list* args; oid_t rettype; opr_xp* result; /* Select the operator */ if (rtree == NULL) { /* right operator */ ltypeId = expr_type(ltree); rtypeId = INVALID_OID; tup = right_opr(pstate, opname, ltypeId, false, location); } else if (ltree == NULL) { /* left operator */ rtypeId = expr_type(rtree); ltypeId = INVALID_OID; tup = left_opr(pstate, opname, rtypeId, false, location); } else { /* otherwise, binary operator */ ltypeId = expr_type(ltree); rtypeId = expr_type(rtree); tup = oper(pstate, opname, ltypeId, rtypeId, false, location); } opform = (Form_pg_operator) GET_STRUCT(tup); /* Check it's not a shell */ if (!REGPROC_VALID(opform->oprcode)) ereport(ERROR, ( errcode(E_UNDEFINED_FUNCTION), errmsg("operator is only a shell: %s", opr_signature_string(opname, opform->oprkind, opform->oprleft, opform->oprright)), parser_errpos(pstate, location))); /* Do typecasting and build the expression tree */ if (rtree == NULL) { /* right operator */ args = list_make1(ltree); actual_arg_types[0] = ltypeId; declared_arg_types[0] = opform->oprleft; nargs = 1; } else if (ltree == NULL) { /* left operator */ args = list_make1(rtree); actual_arg_types[0] = rtypeId; declared_arg_types[0] = opform->oprright; nargs = 1; } else { /* otherwise, binary operator */ args = list_make2(ltree, rtree); actual_arg_types[0] = ltypeId; actual_arg_types[1] = rtypeId; declared_arg_types[0] = opform->oprleft; declared_arg_types[1] = opform->oprright; nargs = 2; } /* * enforce consistency with polymorphic argument and return types, * possibly adjusting return type or declared_arg_types (which will be * used as the cast destination by make_fn_arguments) */ rettype = enforce_generic_type_consistency(actual_arg_types, declared_arg_types, nargs, opform->oprresult, false); /* perform the necessary typecasting of arguments */ make_fn_arguments(pstate, args, actual_arg_types, declared_arg_types); /* and build the expression node */ result = MK_N(OpExpr,opr_xp); result->opno = opr_id(tup); result->opfuncid = opform->oprcode; result->opresulttype = rettype; result->opretset = get_func_retset(opform->oprcode); /* opcollid and inputcollid will be set by parse_collate.c */ result->args = args; result->location = location; release_syscache(tup); return (expr_n *) result; }
void test_build_http_headers(void **state) { /* setup mock data and expectations */ PxfInputData *input = (PxfInputData *) palloc0(sizeof(PxfInputData)); CHURL_HEADERS headers = (CHURL_HEADERS) palloc0(sizeof(CHURL_HEADERS)); GPHDUri *gphd_uri = (GPHDUri *) palloc0(sizeof(GPHDUri)); Relation rel = (Relation) palloc0(sizeof(RelationData)); ExtTableEntry ext_tbl; struct tupleDesc tuple; input->headers = headers; input->gphduri = gphd_uri; input->rel = rel; gphd_uri->host = "testhost"; gphd_uri->port = "101"; gphd_uri->data = "this is test data"; gphd_uri->uri = "testuri"; OptionData *option_data1 = (OptionData *) palloc0(sizeof(OptionData)); option_data1->key = "option1-key"; option_data1->value = "option1-value"; OptionData *option_data2 = (OptionData *) palloc0(sizeof(OptionData)); option_data2->key = "option2-key"; option_data2->value = "option2-value"; gphd_uri->options = list_make2(option_data1, option_data2); tuple.natts = 0; ext_tbl.fmtcode = 'c'; rel->rd_id = 56; rel->rd_att = &tuple; expect_value(GetExtTableEntry, relid, rel->rd_id); will_return(GetExtTableEntry, &ext_tbl); expect_headers_append(headers, "X-GP-FORMAT", TextFormatName); expect_headers_append(headers, "X-GP-ATTRS", "0"); expect_any(external_set_env_vars, extvar); expect_string(external_set_env_vars, uri, gphd_uri->uri); expect_value(external_set_env_vars, csv, false); expect_value(external_set_env_vars, escape, NULL); expect_value(external_set_env_vars, quote, NULL); expect_value(external_set_env_vars, header, false); expect_value(external_set_env_vars, scancounter, 0); struct extvar_t mock_extvar; mock_extvar.GP_USER = "******"; snprintf(mock_extvar.GP_SEGMENT_ID, sizeof(mock_extvar.GP_SEGMENT_ID), "SegId"); snprintf(mock_extvar.GP_SEGMENT_COUNT, sizeof(mock_extvar.GP_SEGMENT_COUNT), "10"); snprintf(mock_extvar.GP_XID, sizeof(mock_extvar.GP_XID), "20"); will_assign_memory(external_set_env_vars, extvar, &mock_extvar, sizeof(extvar_t)); will_be_called(external_set_env_vars); expect_headers_append(headers, "X-GP-USER", "user"); expect_headers_append(headers, "X-GP-SEGMENT-ID", "SegId"); expect_headers_append(headers, "X-GP-SEGMENT-COUNT", "10"); expect_headers_append(headers, "X-GP-XID", "20"); char alignment[3]; pg_ltoa(sizeof(char *), alignment); expect_headers_append(headers, "X-GP-ALIGNMENT", alignment); expect_headers_append(headers, "X-GP-URL-HOST", gphd_uri->host); expect_headers_append(headers, "X-GP-URL-PORT", gphd_uri->port); expect_headers_append(headers, "X-GP-DATA-DIR", gphd_uri->data); expect_string(normalize_key_name, key, "option1-key"); will_return(normalize_key_name, pstrdup("X-GP-OPTION1-KEY")); expect_headers_append(headers, "X-GP-OPTION1-KEY", "option1-value"); expect_string(normalize_key_name, key, "option2-key"); will_return(normalize_key_name, pstrdup("X-GP-OPTION2-KEY")); expect_headers_append(headers, "X-GP-OPTION2-KEY", "option2-value"); expect_headers_append(headers, "X-GP-URI", gphd_uri->uri); expect_headers_append(headers, "X-GP-HAS-FILTER", "0"); /* call function under test */ build_http_headers(input); /* no asserts as the function just calls to set headers */ /* cleanup */ pfree(rel); pfree(gphd_uri); pfree(headers); pfree(input); }
/* MPP-6923: */ static List * AlterResqueueCapabilityEntry( List *stmtOptIdList, Oid queueid, ListCell *initcell, bool bCreate) { ListCell *lc; List *elems = NIL; List *dropelems = NIL; List *dupcheck = NIL; HeapTuple tuple; cqContext *pcqCtx; cqContext cqc; Relation rel = NULL; bool bWithout = false; TupleDesc tupdesc = NULL; #ifdef USE_ASSERT_CHECKING { DefElem *defel = (DefElem *) lfirst(initcell); Assert(0 == strcmp(defel->defname, "withliststart")); } #endif initcell = lnext(initcell); /* walk the original list and build a list of valid entries */ for_each_cell(lc, initcell) { DefElem *defel = (DefElem *) lfirst(lc); Oid resTypeOid = InvalidOid; int resTypeInt = 0; List *pentry = NIL; Value *pKeyVal = NULL; Value *pStrVal = NULL; if (!bWithout && (strcmp(defel->defname, "withoutliststart") == 0)) { bWithout = true; rel = heap_open(ResourceTypeRelationId, RowExclusiveLock); tupdesc = RelationGetDescr(rel); goto L_loop_cont; } /* ignore the basic threshold entries -- should already be processed */ if (strcmp(defel->defname, "active_statements") == 0) goto L_loop_cont; if (strcmp(defel->defname, "max_cost") == 0) goto L_loop_cont; if (strcmp(defel->defname, "cost_overcommit") == 0) goto L_loop_cont; if (strcmp(defel->defname, "min_cost") == 0) goto L_loop_cont; if (!GetResourceTypeByName(defel->defname, &resTypeInt, &resTypeOid)) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("option \"%s\" is not a valid resource type", defel->defname))); pKeyVal = makeString(defel->defname); /* WITHOUT clause value determined in pg_resourcetype */ if (!bWithout) pStrVal = makeString(defGetString(defel)); else { pStrVal = NULL; /* if NULL, delete entry from * pg_resqueuecapability */ pcqCtx = caql_beginscan( caql_addrel(cqclr(&cqc), rel), cql("SELECT * FROM pg_resourcetype" " WHERE restypid = :1 FOR UPDATE", Int16GetDatum(resTypeInt))); while (HeapTupleIsValid(tuple = caql_getnext(pcqCtx))) { text *shutoff_text = NULL; char *shutoff_str = NULL; Datum shutoff_datum; bool isnull = false; Form_pg_resourcetype rtyp = (Form_pg_resourcetype)GETSTRUCT(tuple); if (!rtyp->reshasdisable) { ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("option \"%s\" cannot be disabled", defel->defname))); } /* required type must have a default value if it can * be disabled */ if (!rtyp->reshasdefault) { if (!rtyp->resrequired) /* optional resource without a default is * turned off by removing entry from * pg_resqueuecapability */ break; else { /* XXX XXX */ Assert(0); ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("required option \"%s\" cannot be disabled", defel->defname))); } } /* get the shutoff string */ shutoff_datum = heap_getattr(tuple, Anum_pg_resourcetype_resdisabledsetting, tupdesc, &isnull); Assert(!isnull); shutoff_text = DatumGetTextP(shutoff_datum); shutoff_str = DatumGetCString( DirectFunctionCall1( textout, PointerGetDatum(shutoff_text))); pStrVal = makeString(shutoff_str); break; } /* end while heaptuple is valid */ caql_endscan(pcqCtx); } /* check for duplicate key specifications */ if (list_member(dupcheck, pKeyVal)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("conflicting or redundant option for \"%s\"", defel->defname))); dupcheck = lappend(dupcheck, pKeyVal); pentry = list_make2( makeInteger(resTypeInt), pStrVal); /* list of lists - (resource type, resource setting) */ if (bWithout) { /* if the "without" entry has an "off" value, then treat * it as a regular "with" item and update it in * pg_resqueuecapability, else remove its entry */ if (!pStrVal) dropelems = lappend(dropelems, pentry); else elems = lappend(elems, pentry); } else elems = lappend(elems, pentry); L_loop_cont: resTypeInt = 0; /* make compiler happy */ }
/* * create_toast_table --- internal workhorse * * rel is already opened and locked * toastOid and toastIndexOid are normally InvalidOid, but during * bootstrap they can be nonzero to specify hand-assigned OIDs */ static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptions, LOCKMODE lockmode, bool check) { Oid relOid = RelationGetRelid(rel); HeapTuple reltup; TupleDesc tupdesc; bool shared_relation; bool mapped_relation; Relation toast_rel; Relation class_rel; Oid toast_relid; Oid toast_typid = InvalidOid; Oid namespaceid; char toast_relname[NAMEDATALEN]; char toast_idxname[NAMEDATALEN]; IndexInfo *indexInfo; Oid collationObjectId[2]; Oid classObjectId[2]; int16 coloptions[2]; ObjectAddress baseobject, toastobject; /* * Toast table is shared if and only if its parent is. * * We cannot allow toasting a shared relation after initdb (because * there's no way to mark it toasted in other databases' pg_class). */ shared_relation = rel->rd_rel->relisshared; if (shared_relation && !IsBootstrapProcessingMode()) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("shared tables cannot be toasted after initdb"))); /* It's mapped if and only if its parent is, too */ mapped_relation = RelationIsMapped(rel); /* * Is it already toasted? */ if (rel->rd_rel->reltoastrelid != InvalidOid) return false; /* * Check to see whether the table actually needs a TOAST table. */ if (!IsBinaryUpgrade) { /* Normal mode, normal check */ if (!needs_toast_table(rel)) return false; } else { /* * In binary-upgrade mode, create a TOAST table if and only if * pg_upgrade told us to (ie, a TOAST table OID has been provided). * * This indicates that the old cluster had a TOAST table for the * current table. We must create a TOAST table to receive the old * TOAST file, even if the table seems not to need one. * * Contrariwise, if the old cluster did not have a TOAST table, we * should be able to get along without one even if the new version's * needs_toast_table rules suggest we should have one. There is a lot * of daylight between where we will create a TOAST table and where * one is really necessary to avoid failures, so small cross-version * differences in the when-to-create heuristic shouldn't be a problem. * If we tried to create a TOAST table anyway, we would have the * problem that it might take up an OID that will conflict with some * old-cluster table we haven't seen yet. */ if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid) || !OidIsValid(binary_upgrade_next_toast_pg_type_oid)) return false; } /* * If requested check lockmode is sufficient. This is a cross check in * case of errors or conflicting decisions in earlier code. */ if (check && lockmode != AccessExclusiveLock) elog(ERROR, "AccessExclusiveLock required to add toast table."); /* * Create the toast table and its index */ snprintf(toast_relname, sizeof(toast_relname), "pg_toast_%u", relOid); snprintf(toast_idxname, sizeof(toast_idxname), "pg_toast_%u_index", relOid); /* this is pretty painful... need a tuple descriptor */ tupdesc = CreateTemplateTupleDesc(3); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "chunk_id", OIDOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 2, "chunk_seq", INT4OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 3, "chunk_data", BYTEAOID, -1, 0); /* * Ensure that the toast table doesn't itself get toasted, or we'll be * toast :-(. This is essential for chunk_data because type bytea is * toastable; hit the other two just to be sure. */ TupleDescAttr(tupdesc, 0)->attstorage = 'p'; TupleDescAttr(tupdesc, 1)->attstorage = 'p'; TupleDescAttr(tupdesc, 2)->attstorage = 'p'; /* * Toast tables for regular relations go in pg_toast; those for temp * relations go into the per-backend temp-toast-table namespace. */ if (isTempOrTempToastNamespace(rel->rd_rel->relnamespace)) namespaceid = GetTempToastNamespace(); else namespaceid = PG_TOAST_NAMESPACE; /* * Use binary-upgrade override for pg_type.oid, if supplied. We might be * in the post-schema-restore phase where we are doing ALTER TABLE to * create TOAST tables that didn't exist in the old cluster. */ if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_toast_pg_type_oid)) { toast_typid = binary_upgrade_next_toast_pg_type_oid; binary_upgrade_next_toast_pg_type_oid = InvalidOid; } toast_relid = heap_create_with_catalog(toast_relname, namespaceid, rel->rd_rel->reltablespace, toastOid, toast_typid, InvalidOid, rel->rd_rel->relowner, tupdesc, NIL, RELKIND_TOASTVALUE, rel->rd_rel->relpersistence, shared_relation, mapped_relation, ONCOMMIT_NOOP, reloptions, false, true, true, InvalidOid, NULL); Assert(toast_relid != InvalidOid); /* make the toast relation visible, else heap_open will fail */ CommandCounterIncrement(); /* ShareLock is not really needed here, but take it anyway */ toast_rel = heap_open(toast_relid, ShareLock); /* * Create unique index on chunk_id, chunk_seq. * * NOTE: the normal TOAST access routines could actually function with a * single-column index on chunk_id only. However, the slice access * routines use both columns for faster access to an individual chunk. In * addition, we want it to be unique as a check against the possibility of * duplicate TOAST chunk OIDs. The index might also be a little more * efficient this way, since btree isn't all that happy with large numbers * of equal keys. */ indexInfo = makeNode(IndexInfo); indexInfo->ii_NumIndexAttrs = 2; indexInfo->ii_NumIndexKeyAttrs = 2; indexInfo->ii_IndexAttrNumbers[0] = 1; indexInfo->ii_IndexAttrNumbers[1] = 2; indexInfo->ii_Expressions = NIL; indexInfo->ii_ExpressionsState = NIL; indexInfo->ii_Predicate = NIL; indexInfo->ii_PredicateState = NULL; indexInfo->ii_ExclusionOps = NULL; indexInfo->ii_ExclusionProcs = NULL; indexInfo->ii_ExclusionStrats = NULL; indexInfo->ii_Unique = true; indexInfo->ii_ReadyForInserts = true; indexInfo->ii_Concurrent = false; indexInfo->ii_BrokenHotChain = false; indexInfo->ii_ParallelWorkers = 0; indexInfo->ii_Am = BTREE_AM_OID; indexInfo->ii_AmCache = NULL; indexInfo->ii_Context = CurrentMemoryContext; collationObjectId[0] = InvalidOid; collationObjectId[1] = InvalidOid; classObjectId[0] = OID_BTREE_OPS_OID; classObjectId[1] = INT4_BTREE_OPS_OID; coloptions[0] = 0; coloptions[1] = 0; index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid, InvalidOid, InvalidOid, indexInfo, list_make2("chunk_id", "chunk_seq"), BTREE_AM_OID, rel->rd_rel->reltablespace, collationObjectId, classObjectId, coloptions, (Datum) 0, INDEX_CREATE_IS_PRIMARY, 0, true, true, NULL); heap_close(toast_rel, NoLock); /* * Store the toast table's OID in the parent relation's pg_class row */ class_rel = heap_open(RelationRelationId, RowExclusiveLock); reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid)); if (!HeapTupleIsValid(reltup)) elog(ERROR, "cache lookup failed for relation %u", relOid); ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid; if (!IsBootstrapProcessingMode()) { /* normal case, use a transactional update */ CatalogTupleUpdate(class_rel, &reltup->t_self, reltup); } else { /* While bootstrapping, we cannot UPDATE, so overwrite in-place */ heap_inplace_update(class_rel, reltup); } heap_freetuple(reltup); heap_close(class_rel, RowExclusiveLock); /* * Register dependency from the toast table to the master, so that the * toast table will be deleted if the master is. Skip this in bootstrap * mode. */ if (!IsBootstrapProcessingMode()) { baseobject.classId = RelationRelationId; baseobject.objectId = relOid; baseobject.objectSubId = 0; toastobject.classId = RelationRelationId; toastobject.objectId = toast_relid; toastobject.objectSubId = 0; recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL); } /* * Make changes visible */ CommandCounterIncrement(); return true; }
/* * make_op_expr() * Build operator expression using an already-looked-up operator. * * As with coerce_type, pstate may be NULL if no special unknown-Param * processing is wanted. */ static Expr * make_op_expr(ParseState *pstate, Operator op, Node *ltree, Node *rtree, Oid ltypeId, Oid rtypeId) { Form_pg_operator opform = (Form_pg_operator) GETSTRUCT(op); Oid actual_arg_types[2]; Oid declared_arg_types[2]; int nargs; List *args; Oid rettype; OpExpr *result; if (rtree == NULL) { /* right operator */ args = list_make1(ltree); actual_arg_types[0] = ltypeId; declared_arg_types[0] = opform->oprleft; nargs = 1; } else if (ltree == NULL) { /* left operator */ args = list_make1(rtree); actual_arg_types[0] = rtypeId; declared_arg_types[0] = opform->oprright; nargs = 1; } else { /* otherwise, binary operator */ args = list_make2(ltree, rtree); actual_arg_types[0] = ltypeId; actual_arg_types[1] = rtypeId; declared_arg_types[0] = opform->oprleft; declared_arg_types[1] = opform->oprright; nargs = 2; } /* * enforce consistency with ANYARRAY and ANYELEMENT argument and return * types, possibly adjusting return type or declared_arg_types (which will * be used as the cast destination by make_fn_arguments) */ rettype = enforce_generic_type_consistency(actual_arg_types, declared_arg_types, nargs, opform->oprresult); /* perform the necessary typecasting of arguments */ make_fn_arguments(pstate, args, actual_arg_types, declared_arg_types); /* and build the expression node */ result = makeNode(OpExpr); result->opno = oprid(op); result->opfuncid = InvalidOid; result->opresulttype = rettype; result->opretset = get_func_retset(opform->oprcode); result->args = args; return (Expr *) result; }
/* * make_scalar_array_op() * Build expression tree for "scalar op ANY/ALL (array)" construct. */ Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location) { Oid ltypeId, rtypeId, atypeId, res_atypeId; Operator tup; Form_pg_operator opform; Oid actual_arg_types[2]; Oid declared_arg_types[2]; List *args; Oid rettype; ScalarArrayOpExpr *result; ltypeId = exprType(ltree); atypeId = exprType(rtree); /* * The right-hand input of the operator will be the element type of the * array. However, if we currently have just an untyped literal on the * right, stay with that and hope we can resolve the operator. */ if (atypeId == UNKNOWNOID) rtypeId = UNKNOWNOID; else { rtypeId = get_element_type(atypeId); if (!OidIsValid(rtypeId)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("op ANY/ALL (array) requires array on right side"), errOmitLocation(true), parser_errposition(pstate, location))); } /* Now resolve the operator */ tup = oper(pstate, opname, ltypeId, rtypeId, false, location); opform = (Form_pg_operator) GETSTRUCT(tup); args = list_make2(ltree, rtree); actual_arg_types[0] = ltypeId; actual_arg_types[1] = rtypeId; declared_arg_types[0] = opform->oprleft; declared_arg_types[1] = opform->oprright; /* * enforce consistency with ANYARRAY and ANYELEMENT argument and return * types, possibly adjusting return type or declared_arg_types (which will * be used as the cast destination by make_fn_arguments) */ rettype = enforce_generic_type_consistency(actual_arg_types, declared_arg_types, 2, opform->oprresult); /* * Check that operator result is boolean */ if (rettype != BOOLOID) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("op ANY/ALL (array) requires operator to yield boolean"), errOmitLocation(true), parser_errposition(pstate, location))); if (get_func_retset(opform->oprcode)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("op ANY/ALL (array) requires operator not to return a set"), errOmitLocation(true), parser_errposition(pstate, location))); /* * Now switch back to the array type on the right, arranging for any * needed cast to be applied. */ res_atypeId = get_array_type(declared_arg_types[1]); if (!OidIsValid(res_atypeId)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("could not find array type for data type %s", format_type_be(declared_arg_types[1])), errOmitLocation(true), parser_errposition(pstate, location))); actual_arg_types[1] = atypeId; declared_arg_types[1] = res_atypeId; /* perform the necessary typecasting of arguments */ make_fn_arguments(pstate, args, actual_arg_types, declared_arg_types); /* and build the expression node */ result = makeNode(ScalarArrayOpExpr); result->opno = oprid(tup); result->opfuncid = InvalidOid; result->useOr = useOr; result->args = args; ReleaseOperator(tup); return (Expr *) result; }
/* * create_toast_table --- internal workhorse * * rel is already opened and locked * toastOid and toastIndexOid are normally InvalidOid, but during * bootstrap they can be nonzero to specify hand-assigned OIDs */ static bool create_toast_table(Relation rel, Oid toastOid, Oid toastIndexOid, Datum reloptions, LOCKMODE lockmode, bool check) { Oid relOid = RelationGetRelid(rel); HeapTuple reltup; TupleDesc tupdesc; bool shared_relation; bool mapped_relation; Relation toast_rel; Relation class_rel; Oid toast_relid; Oid toast_typid = InvalidOid; Oid namespaceid; char toast_relname[NAMEDATALEN]; char toast_idxname[NAMEDATALEN]; IndexInfo *indexInfo; Oid collationObjectId[2]; Oid classObjectId[2]; int16 coloptions[2]; ObjectAddress baseobject, toastobject; /* * Toast table is shared if and only if its parent is. * * We cannot allow toasting a shared relation after initdb (because * there's no way to mark it toasted in other databases' pg_class). */ shared_relation = rel->rd_rel->relisshared; if (shared_relation && !IsBootstrapProcessingMode()) ereport(ERROR, (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE), errmsg("shared tables cannot be toasted after initdb"))); /* It's mapped if and only if its parent is, too */ mapped_relation = RelationIsMapped(rel); /* * Is it already toasted? */ if (rel->rd_rel->reltoastrelid != InvalidOid) return false; if (!IsBinaryUpgrade) { if (!needs_toast_table(rel)) return false; } else { /* * Check to see whether the table needs a TOAST table. * * If an update-in-place TOAST relfilenode is specified, force TOAST file * creation even if it seems not to need one. This handles the case * where the old cluster needed a TOAST table but the new cluster * would not normally create one. */ /* * If a TOAST oid is not specified, skip TOAST creation as we will do * it later so we don't create a TOAST table whose OID later conflicts * with a user-supplied OID. This handles cases where the old cluster * didn't need a TOAST table, but the new cluster does. */ if (!OidIsValid(binary_upgrade_next_toast_pg_class_oid)) return false; /* * If a special TOAST value has been passed in, it means we are in * cleanup mode --- we are creating needed TOAST tables after all user * tables with specified OIDs have been created. We let the system * assign a TOAST oid for us. The tables are empty so the missing * TOAST tables were not a problem. */ if (binary_upgrade_next_toast_pg_class_oid == OPTIONALLY_CREATE_TOAST_OID) { /* clear as it is not to be used; it is just a flag */ binary_upgrade_next_toast_pg_class_oid = InvalidOid; if (!needs_toast_table(rel)) return false; } /* both should be set, or not set */ Assert(OidIsValid(binary_upgrade_next_toast_pg_class_oid) == OidIsValid(binary_upgrade_next_toast_pg_type_oid)); } /* * If requested check lockmode is sufficient. This is a cross check in * case of errors or conflicting decisions in earlier code. */ if (check && lockmode != AccessExclusiveLock) elog(ERROR, "AccessExclusiveLock required to add toast table."); /* * Create the toast table and its index */ snprintf(toast_relname, sizeof(toast_relname), "pg_toast_%u", relOid); snprintf(toast_idxname, sizeof(toast_idxname), "pg_toast_%u_index", relOid); /* this is pretty painful... need a tuple descriptor */ tupdesc = CreateTemplateTupleDesc(3, false); TupleDescInitEntry(tupdesc, (AttrNumber) 1, "chunk_id", OIDOID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 2, "chunk_seq", INT4OID, -1, 0); TupleDescInitEntry(tupdesc, (AttrNumber) 3, "chunk_data", BYTEAOID, -1, 0); /* * Ensure that the toast table doesn't itself get toasted, or we'll be * toast :-(. This is essential for chunk_data because type bytea is * toastable; hit the other two just to be sure. */ tupdesc->attrs[0]->attstorage = 'p'; tupdesc->attrs[1]->attstorage = 'p'; tupdesc->attrs[2]->attstorage = 'p'; /* * Toast tables for regular relations go in pg_toast; those for temp * relations go into the per-backend temp-toast-table namespace. */ if (isTempOrToastNamespace(rel->rd_rel->relnamespace)) namespaceid = GetTempToastNamespace(); else namespaceid = PG_TOAST_NAMESPACE; /* Use binary-upgrade override for pg_type.oid, if supplied. */ if (IsBinaryUpgrade && OidIsValid(binary_upgrade_next_toast_pg_type_oid)) { toast_typid = binary_upgrade_next_toast_pg_type_oid; binary_upgrade_next_toast_pg_type_oid = InvalidOid; } toast_relid = heap_create_with_catalog(toast_relname, namespaceid, rel->rd_rel->reltablespace, toastOid, toast_typid, InvalidOid, rel->rd_rel->relowner, tupdesc, NIL, RELKIND_TOASTVALUE, rel->rd_rel->relpersistence, shared_relation, mapped_relation, true, 0, ONCOMMIT_NOOP, reloptions, false, true, true); Assert(toast_relid != InvalidOid); /* make the toast relation visible, else heap_open will fail */ CommandCounterIncrement(); /* ShareLock is not really needed here, but take it anyway */ toast_rel = heap_open(toast_relid, ShareLock); /* * Create unique index on chunk_id, chunk_seq. * * NOTE: the normal TOAST access routines could actually function with a * single-column index on chunk_id only. However, the slice access * routines use both columns for faster access to an individual chunk. In * addition, we want it to be unique as a check against the possibility of * duplicate TOAST chunk OIDs. The index might also be a little more * efficient this way, since btree isn't all that happy with large numbers * of equal keys. */ indexInfo = makeNode(IndexInfo); indexInfo->ii_NumIndexAttrs = 2; indexInfo->ii_KeyAttrNumbers[0] = 1; indexInfo->ii_KeyAttrNumbers[1] = 2; indexInfo->ii_Expressions = NIL; indexInfo->ii_ExpressionsState = NIL; indexInfo->ii_Predicate = NIL; indexInfo->ii_PredicateState = NIL; indexInfo->ii_ExclusionOps = NULL; indexInfo->ii_ExclusionProcs = NULL; indexInfo->ii_ExclusionStrats = NULL; indexInfo->ii_Unique = true; indexInfo->ii_ReadyForInserts = true; indexInfo->ii_Concurrent = false; indexInfo->ii_BrokenHotChain = false; collationObjectId[0] = InvalidOid; collationObjectId[1] = InvalidOid; classObjectId[0] = OID_BTREE_OPS_OID; classObjectId[1] = INT4_BTREE_OPS_OID; coloptions[0] = 0; coloptions[1] = 0; index_create(toast_rel, toast_idxname, toastIndexOid, InvalidOid, indexInfo, list_make2("chunk_id", "chunk_seq"), BTREE_AM_OID, rel->rd_rel->reltablespace, collationObjectId, classObjectId, coloptions, (Datum) 0, true, false, false, false, true, false, false, true); heap_close(toast_rel, NoLock); /* * Store the toast table's OID in the parent relation's pg_class row */ class_rel = heap_open(RelationRelationId, RowExclusiveLock); reltup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relOid)); if (!HeapTupleIsValid(reltup)) elog(ERROR, "cache lookup failed for relation %u", relOid); ((Form_pg_class) GETSTRUCT(reltup))->reltoastrelid = toast_relid; if (!IsBootstrapProcessingMode()) { /* normal case, use a transactional update */ simple_heap_update(class_rel, &reltup->t_self, reltup); /* Keep catalog indexes current */ CatalogUpdateIndexes(class_rel, reltup); } else { /* While bootstrapping, we cannot UPDATE, so overwrite in-place */ heap_inplace_update(class_rel, reltup); } heap_freetuple(reltup); heap_close(class_rel, RowExclusiveLock); /* * Register dependency from the toast table to the master, so that the * toast table will be deleted if the master is. Skip this in bootstrap * mode. */ if (!IsBootstrapProcessingMode()) { baseobject.classId = RelationRelationId; baseobject.objectId = relOid; baseobject.objectSubId = 0; toastobject.classId = RelationRelationId; toastobject.objectId = toast_relid; toastobject.objectSubId = 0; recordDependencyOn(&toastobject, &baseobject, DEPENDENCY_INTERNAL); } /* * Make changes visible */ CommandCounterIncrement(); return true; }
/* * make_op() * Operator expression construction. * * Transform operator expression ensuring type compatibility. * This is where some type conversion happens. * * As with coerce_type, pstate may be NULL if no special unknown-Param * processing is wanted. */ Expr * make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, int location) { Oid ltypeId, rtypeId; Operator tup; Form_pg_operator opform; Oid actual_arg_types[2]; Oid declared_arg_types[2]; int nargs; List *args; Oid rettype; OpExpr *result; /* Select the operator */ if (rtree == NULL) { /* right operator */ ltypeId = exprType(ltree); rtypeId = InvalidOid; tup = right_oper(pstate, opname, ltypeId, false, location); } else if (ltree == NULL) { /* left operator */ rtypeId = exprType(rtree); ltypeId = InvalidOid; tup = left_oper(pstate, opname, rtypeId, false, location); } else { /* otherwise, binary operator */ ltypeId = exprType(ltree); rtypeId = exprType(rtree); tup = oper(pstate, opname, ltypeId, rtypeId, false, location); } opform = (Form_pg_operator) GETSTRUCT(tup); /* Check it's not a shell */ if (!RegProcedureIsValid(opform->oprcode)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("operator is only a shell: %s", op_signature_string(opname, opform->oprkind, opform->oprleft, opform->oprright)), parser_errposition(pstate, location))); /* Do typecasting and build the expression tree */ if (rtree == NULL) { /* right operator */ args = list_make1(ltree); actual_arg_types[0] = ltypeId; declared_arg_types[0] = opform->oprleft; nargs = 1; } else if (ltree == NULL) { /* left operator */ args = list_make1(rtree); actual_arg_types[0] = rtypeId; declared_arg_types[0] = opform->oprright; nargs = 1; } else { /* otherwise, binary operator */ args = list_make2(ltree, rtree); actual_arg_types[0] = ltypeId; actual_arg_types[1] = rtypeId; declared_arg_types[0] = opform->oprleft; declared_arg_types[1] = opform->oprright; nargs = 2; } /* * enforce consistency with polymorphic argument and return types, * possibly adjusting return type or declared_arg_types (which will be * used as the cast destination by make_fn_arguments) */ rettype = enforce_generic_type_consistency(actual_arg_types, declared_arg_types, nargs, opform->oprresult, false); /* perform the necessary typecasting of arguments */ make_fn_arguments(pstate, args, actual_arg_types, declared_arg_types); /* and build the expression node */ result = makeNode(OpExpr); result->opno = oprid(tup); result->opfuncid = opform->oprcode; result->opresulttype = rettype; result->opretset = get_func_retset(opform->oprcode); /* opcollid and inputcollid will be set by parse_collate.c */ result->args = args; result->location = location; ReleaseSysCache(tup); return (Expr *) result; }
/* * make_scalar_array_op() * Build expression tree for "scalar op ANY/ALL (array)" construct. */ Expr * make_scalar_array_op(ParseState *pstate, List *opname, bool useOr, Node *ltree, Node *rtree, int location) { Oid ltypeId, rtypeId, atypeId, res_atypeId; Operator tup; Form_pg_operator opform; Oid actual_arg_types[2]; Oid declared_arg_types[2]; List *args; Oid rettype; ScalarArrayOpExpr *result; ltypeId = exprType(ltree); atypeId = exprType(rtree); /* * The right-hand input of the operator will be the element type of the * array. However, if we currently have just an untyped literal on the * right, stay with that and hope we can resolve the operator. */ if (atypeId == UNKNOWNOID) rtypeId = UNKNOWNOID; else { rtypeId = get_base_element_type(atypeId); if (!OidIsValid(rtypeId)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("op ANY/ALL (array) requires array on right side"), parser_errposition(pstate, location))); } /* Now resolve the operator */ tup = oper(pstate, opname, ltypeId, rtypeId, false, location); opform = (Form_pg_operator) GETSTRUCT(tup); /* Check it's not a shell */ if (!RegProcedureIsValid(opform->oprcode)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_FUNCTION), errmsg("operator is only a shell: %s", op_signature_string(opname, opform->oprkind, opform->oprleft, opform->oprright)), parser_errposition(pstate, location))); args = list_make2(ltree, rtree); actual_arg_types[0] = ltypeId; actual_arg_types[1] = rtypeId; declared_arg_types[0] = opform->oprleft; declared_arg_types[1] = opform->oprright; /* * enforce consistency with polymorphic argument and return types, * possibly adjusting return type or declared_arg_types (which will be * used as the cast destination by make_fn_arguments) */ rettype = enforce_generic_type_consistency(actual_arg_types, declared_arg_types, 2, opform->oprresult, false); /* * Check that operator result is boolean */ if (rettype != BOOLOID) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("op ANY/ALL (array) requires operator to yield boolean"), parser_errposition(pstate, location))); if (get_func_retset(opform->oprcode)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("op ANY/ALL (array) requires operator not to return a set"), parser_errposition(pstate, location))); /* * Now switch back to the array type on the right, arranging for any * needed cast to be applied. Beware of polymorphic operators here; * enforce_generic_type_consistency may or may not have replaced a * polymorphic type with a real one. */ if (IsPolymorphicType(declared_arg_types[1])) { /* assume the actual array type is OK */ res_atypeId = atypeId; } else { res_atypeId = get_array_type(declared_arg_types[1]); if (!OidIsValid(res_atypeId)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("could not find array type for data type %s", format_type_be(declared_arg_types[1])), parser_errposition(pstate, location))); } actual_arg_types[1] = atypeId; declared_arg_types[1] = res_atypeId; /* perform the necessary typecasting of arguments */ make_fn_arguments(pstate, args, actual_arg_types, declared_arg_types); /* and build the expression node */ result = makeNode(ScalarArrayOpExpr); result->opno = oprid(tup); result->opfuncid = opform->oprcode; result->useOr = useOr; /* inputcollid will be set by parse_collate.c */ result->args = args; result->location = location; ReleaseSysCache(tup); return (Expr *) result; }
/* * Generate plan for an INTERSECT, INTERSECT ALL, EXCEPT, or EXCEPT ALL node */ static Plan * generate_nonunion_plan(SetOperationStmt *op, PlannerInfo *root, List *refnames_tlist, List **sortClauses) { Plan *lplan, *rplan, *plan; List *tlist, *sortList, *planlist, *child_sortclauses; SetOpCmd cmd; /* Recurse on children, ensuring their outputs are marked */ lplan = recurse_set_operations(op->larg, root, 0.0 /* all tuples needed */ , op->colTypes, false, 0, refnames_tlist, &child_sortclauses); rplan = recurse_set_operations(op->rarg, root, 0.0 /* all tuples needed */ , op->colTypes, false, 1, refnames_tlist, &child_sortclauses); planlist = list_make2(lplan, rplan); /* * Generate tlist for Append plan node. * * The tlist for an Append plan isn't important as far as the Append is * concerned, but we must make it look real anyway for the benefit of the * next plan level up. In fact, it has to be real enough that the flag * column is shown as a variable not a constant, else setrefs.c will get * confused. */ tlist = generate_append_tlist(op->colTypes, true, planlist, refnames_tlist); /* * Append the child results together. */ plan = (Plan *) make_append(planlist, false, tlist); /* * Sort the child results, then add a SetOp plan node to generate the * correct output. */ sortList = addAllTargetsToSortList(NULL, NIL, tlist, false); if (sortList == NIL) /* nothing to sort on? */ { *sortClauses = NIL; return plan; } plan = (Plan *) make_sort_from_sortclauses(root, sortList, plan); switch (op->op) { case SETOP_INTERSECT: cmd = op->all ? SETOPCMD_INTERSECT_ALL : SETOPCMD_INTERSECT; break; case SETOP_EXCEPT: cmd = op->all ? SETOPCMD_EXCEPT_ALL : SETOPCMD_EXCEPT; break; default: elog(ERROR, "unrecognized set op: %d", (int) op->op); cmd = SETOPCMD_INTERSECT; /* keep compiler quiet */ break; } plan = (Plan *) make_setop(cmd, plan, sortList, list_length(op->colTypes) + 1); *sortClauses = sortList; return plan; }