Datum btnamecmp(PG_FUNCTION_ARGS) { Name arg1 = PG_GETARG_NAME(0); Name arg2 = PG_GETARG_NAME(1); PG_RETURN_INT32(namecmp(arg1, arg2, PG_GET_COLLATION())); }
Datum namegt(PG_FUNCTION_ARGS) { Name arg1 = PG_GETARG_NAME(0); Name arg2 = PG_GETARG_NAME(1); PG_RETURN_BOOL(strncmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) > 0); }
Datum name_pattern_ge(PG_FUNCTION_ARGS) { Name arg1 = PG_GETARG_NAME(0); Name arg2 = PG_GETARG_NAME(1); PG_RETURN_BOOL(memcmp(NameStr(*arg1), NameStr(*arg2), NAMEDATALEN) >= 0); }
Datum btnamecmp(PG_FUNCTION_ARGS) { Name a = PG_GETARG_NAME(0); Name b = PG_GETARG_NAME(1); PG_RETURN_INT32(strncmp(NameStr(*a), NameStr(*b), NAMEDATALEN)); }
Datum namege(PG_FUNCTION_ARGS) { Name arg1 = PG_GETARG_NAME(0); Name arg2 = PG_GETARG_NAME(1); PG_RETURN_BOOL(namecmp(arg1, arg2, PG_GET_COLLATION()) >= 0); }
Datum bmname_pattern_cmp(PG_FUNCTION_ARGS) { Name a = PG_GETARG_NAME(0); Name b = PG_GETARG_NAME(1); PG_RETURN_INT32(memcmp(NameStr(*a), NameStr(*b), NAMEDATALEN)); }
/* * SQL function for creating a new logical replication slot. */ Datum pg_create_logical_replication_slot(PG_FUNCTION_ARGS) { Name name = PG_GETARG_NAME(0); Name plugin = PG_GETARG_NAME(1); LogicalDecodingContext *ctx = NULL; TupleDesc tupdesc; HeapTuple tuple; Datum result; Datum values[2]; bool nulls[2]; Assert(!MyReplicationSlot); if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) elog(ERROR, "return type must be a row type"); check_permissions(); CheckLogicalDecodingRequirements(); /* * Acquire a logical decoding slot, this will check for conflicting names. * Initially create it as ephemeral - that allows us to nicely handle * errors during initialization because it'll get dropped if this * transaction fails. We'll make it persistent at the end. */ ReplicationSlotCreate(NameStr(*name), true, RS_EPHEMERAL); /* * Create logical decoding context, to build the initial snapshot. */ ctx = CreateInitDecodingContext( NameStr(*plugin), NIL, logical_read_local_xlog_page, NULL, NULL); /* build initial snapshot, might take a while */ DecodingContextFindStartpoint(ctx); values[0] = CStringGetTextDatum(NameStr(MyReplicationSlot->data.name)); values[1] = LSNGetDatum(MyReplicationSlot->data.confirmed_flush); /* don't need the decoding context anymore */ FreeDecodingContext(ctx); memset(nulls, 0, sizeof(nulls)); tuple = heap_form_tuple(tupdesc, values, nulls); result = HeapTupleGetDatum(tuple); /* ok, slot is now fully created, mark it as persistent */ ReplicationSlotPersist(); ReplicationSlotRelease(); PG_RETURN_DATUM(result); }
/* * Convert string using encoding_nanme. * * TEXT convert2(TEXT string, NAME src_encoding_name, NAME dest_encoding_name) */ Datum pg_convert2(PG_FUNCTION_ARGS) { text *string = PG_GETARG_TEXT_P(0); char *src_encoding_name = NameStr(*PG_GETARG_NAME(1)); int src_encoding = pg_char_to_encoding(src_encoding_name); char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2)); int dest_encoding = pg_char_to_encoding(dest_encoding_name); unsigned char *result; text *retval; unsigned char *str; int len; if (src_encoding < 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid source encoding name \"%s\"", src_encoding_name))); if (dest_encoding < 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid destination encoding name \"%s\"", dest_encoding_name))); /* make sure that source string is null terminated */ len = VARSIZE(string) - VARHDRSZ; str = palloc(len + 1); memcpy(str, VARDATA(string), len); *(str + len) = '\0'; result = pg_do_encoding_conversion(str, len, src_encoding, dest_encoding); if (result == NULL) elog(ERROR, "encoding conversion failed"); /* * build text data type structure. we cannot use textin() here, since * textin assumes that input string encoding is same as database * encoding. */ len = strlen(result) + VARHDRSZ; retval = palloc(len); VARATT_SIZEP(retval) = len; memcpy(VARDATA(retval), result, len - VARHDRSZ); if (result != str) pfree(result); pfree(str); /* free memory if allocated by the toaster */ PG_FREE_IF_COPY(string, 0); PG_RETURN_TEXT_P(retval); }
/* * Convert string using encoding_names. * * BYTEA convert(BYTEA string, NAME src_encoding_name, NAME dest_encoding_name) */ Datum pg_convert(PG_FUNCTION_ARGS) { bytea *string = PG_GETARG_BYTEA_P(0); char *src_encoding_name = NameStr(*PG_GETARG_NAME(1)); int src_encoding = pg_char_to_encoding(src_encoding_name); char *dest_encoding_name = NameStr(*PG_GETARG_NAME(2)); int dest_encoding = pg_char_to_encoding(dest_encoding_name); unsigned char *result; bytea *retval; unsigned char *str; int len; if (src_encoding < 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid source encoding name \"%s\"", src_encoding_name))); if (dest_encoding < 0) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("invalid destination encoding name \"%s\"", dest_encoding_name))); /* make sure that source string is valid and null terminated */ len = VARSIZE(string) - VARHDRSZ; pg_verify_mbstr(src_encoding, VARDATA(string), len, false); str = palloc(len + 1); memcpy(str, VARDATA(string), len); *(str + len) = '\0'; result = pg_do_encoding_conversion(str, len, src_encoding, dest_encoding); /* * build bytea data type structure. */ len = strlen((char *) result) + VARHDRSZ; retval = palloc(len); SET_VARSIZE(retval, len); memcpy(VARDATA(retval), result, len - VARHDRSZ); if (result != str) pfree(result); pfree(str); /* free memory if allocated by the toaster */ PG_FREE_IF_COPY(string, 0); PG_RETURN_BYTEA_P(retval); }
/* * nameout - converts internal representation to "..." */ Datum nameout(PG_FUNCTION_ARGS) { Name s = PG_GETARG_NAME(0); PG_RETURN_CSTRING(pstrdup(NameStr(*s))); }
Datum pg_database_size_name(PG_FUNCTION_ARGS) { int64 size = 0; Name dbName = PG_GETARG_NAME(0); Oid dbOid = get_database_oid(NameStr(*dbName)); if (!OidIsValid(dbOid)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database \"%s\" does not exist", NameStr(*dbName)))); size = calculate_database_size(dbOid); if (Gp_role == GP_ROLE_DISPATCH) { StringInfoData buffer; initStringInfo(&buffer); appendStringInfo(&buffer, "select sum(pg_database_size('%s'))::int8 from gp_dist_random('gp_id');", NameStr(*dbName)); size += get_size_from_segDBs(buffer.data); } PG_RETURN_INT64(size); }
Datum acl_check_access_text_name(PG_FUNCTION_ARGS) { ArrayType *acl; text *mask; Name rolename; bool implicit_allow; Oid who; if (!check_access_text_mask_extract_args(fcinfo, &acl, &mask, NULL, &implicit_allow, false, true)) PG_RETURN_NULL(); if (PG_ARGISNULL(2)) PG_RETURN_NULL(); rolename = PG_GETARG_NAME(2); who = get_role_oid(NameStr(*rolename), false); PG_RETURN_TEXT_P(check_access_text_mask(acl, ACL_TYPE_LENGTH, ACL_TYPE_ALIGNMENT, extract_acl_entry_base, mask, (intptr_t) who, who_matches, implicit_allow)); }
/* * SQL function for creating a new physical (streaming replication) * replication slot. */ Datum pg_create_physical_replication_slot(PG_FUNCTION_ARGS) { Name name = PG_GETARG_NAME(0); Datum values[2]; bool nulls[2]; TupleDesc tupdesc; HeapTuple tuple; Datum result; Assert(!MyReplicationSlot); if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) elog(ERROR, "return type must be a row type"); check_permissions(); CheckSlotRequirements(); /* acquire replication slot, this will check for conflicting names */ ReplicationSlotCreate(NameStr(*name), false, RS_PERSISTENT); values[0] = NameGetDatum(&MyReplicationSlot->data.name); nulls[0] = false; nulls[1] = true; tuple = heap_form_tuple(tupdesc, values, nulls); result = HeapTupleGetDatum(tuple); ReplicationSlotRelease(); PG_RETURN_DATUM(result); }
Datum pg_tablespace_size_name(PG_FUNCTION_ARGS) { int64 size = 0; Name tblspcName = PG_GETARG_NAME(0); Oid tblspcOid = get_tablespace_oid(NameStr(*tblspcName)); if (!OidIsValid(tblspcOid)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("tablespace \"%s\" does not exist", NameStr(*tblspcName)))); size = calculate_tablespace_size(tblspcOid); if (Gp_role == GP_ROLE_DISPATCH) { StringInfoData buffer; initStringInfo(&buffer); appendStringInfo(&buffer, "select sum(pg_tablespace_size('%s'))::int8 from gp_dist_random('gp_version_at_initdb');", NameStr(*tblspcName)); size += get_size_from_segDBs(buffer.data); } PG_RETURN_INT64(size); }
Datum pg_database_size_name(PG_FUNCTION_ARGS) { Name dbName = PG_GETARG_NAME(0); Oid dbOid = get_database_oid(NameStr(*dbName), false); PG_RETURN_INT64(calculate_database_size(dbOid)); }
Datum pg_tablespace_size_name(PG_FUNCTION_ARGS) { Name tblspcName = PG_GETARG_NAME(0); Oid tblspcOid = get_tablespace_oid(NameStr(*tblspcName), false); PG_RETURN_INT64(calculate_tablespace_size(tblspcOid)); }
Datum database_size(PG_FUNCTION_ARGS) { Name dbname = PG_GETARG_NAME(0); Oid dbid; char *dbpath; DIR *dirdesc; struct dirent *direntry; int64 totalsize; dbid = get_database_oid(NameStr(*dbname)); if (!OidIsValid(dbid)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database \"%s\" does not exist", NameStr(*dbname)))); dbpath = GetDatabasePath(dbid); dirdesc = AllocateDir(dbpath); if (!dirdesc) ereport(ERROR, (errcode_for_file_access(), errmsg("could not open directory \"%s\": %m", dbpath))); totalsize = 0; for (;;) { char *fullname; struct stat statbuf; errno = 0; direntry = readdir(dirdesc); if (!direntry) { if (errno) ereport(ERROR, (errcode_for_file_access(), errmsg("error reading directory: %m"))); else break; } fullname = psnprintf(strlen(dbpath) + 1 + strlen(direntry->d_name) + 1, "%s/%s", dbpath, direntry->d_name); if (stat(fullname, &statbuf) == -1) ereport(ERROR, (errcode_for_file_access(), errmsg("could not stat \"%s\": %m", fullname))); totalsize += statbuf.st_size; pfree(fullname); } FreeDir(dirdesc); PG_RETURN_INT64(totalsize); }
/* * namesend - converts name to binary format */ Datum namesend(PG_FUNCTION_ARGS) { Name s = PG_GETARG_NAME(0); StringInfoData buf; pq_begintypsend(&buf); pq_sendtext(&buf, NameStr(*s), strlen(NameStr(*s))); PG_RETURN_BYTEA_P(pq_endtypsend(&buf)); }
Datum hashname(PG_FUNCTION_ARGS) { char *key = NameStr(*PG_GETARG_NAME(0)); int keylen = strlen(key); Assert(keylen < NAMEDATALEN); /* else it's not truncated * correctly */ return hash_any((unsigned char *) key, keylen); }
Datum set_timetravel(PG_FUNCTION_ARGS) { Name relname = PG_GETARG_NAME(0); int32 on = PG_GETARG_INT32(1); char *rname; char *d; char *s; int32 ret; TTOffList *p, *pp; for (pp = (p = &TTOff)->next; pp; pp = (p = pp)->next) { if (namestrcmp(relname, pp->name) == 0) break; } if (pp) { /* OFF currently */ if (on != 0) { /* turn ON */ p->next = pp->next; free(pp); } ret = 0; } else { /* ON currently */ if (on == 0) { /* turn OFF */ s = rname = DatumGetCString(DirectFunctionCall1(nameout, NameGetDatum(relname))); if (s) { pp = malloc(sizeof(TTOffList) + strlen(rname)); if (pp) { pp->next = NULL; p->next = pp; d = pp->name; while (*s) *d++ = tolower((unsigned char) *s++); *d = '\0'; } pfree(rname); } } ret = 1; } PG_RETURN_INT32(ret); }
Datum add_pg_enum_label(PG_FUNCTION_ARGS) { Oid enumoid = PG_GETARG_OID(0); Oid typoid = PG_GETARG_OID(1); Name label = PG_GETARG_NAME(2); EnumValuesCreate(typoid, list_make1(makeString(NameStr(*label))), enumoid); PG_RETURN_VOID(); }
Datum nameicregexne(PG_FUNCTION_ARGS) { Name n = PG_GETARG_NAME(0); text *p = PG_GETARG_TEXT_P(1); PG_RETURN_BOOL(!RE_compile_and_execute(p, NameStr(*n), strlen(NameStr(*n)), regex_flavor | REG_ICASE, 0, NULL)); }
Datum nameicregexeq(PG_FUNCTION_ARGS) { Name n = PG_GETARG_NAME(0); text *p = PG_GETARG_TEXT_PP(1); PG_RETURN_BOOL(RE_compile_and_execute(p, NameStr(*n), strlen(NameStr(*n)), REG_ADVANCED | REG_ICASE, 0, NULL)); }
Datum get_timetravel(PG_FUNCTION_ARGS) { Name relname = PG_GETARG_NAME(0); TTOffList *pp; for (pp = TTOff.next; pp; pp = pp->next) { if (namestrcmp(relname, pp->name) == 0) PG_RETURN_INT32(0); } PG_RETURN_INT32(1); }
/* * SQL function for dropping a replication slot. */ Datum pg_drop_replication_slot(PG_FUNCTION_ARGS) { Name name = PG_GETARG_NAME(0); check_permissions(); CheckSlotRequirements(); ReplicationSlotDrop(NameStr(*name)); PG_RETURN_VOID(); }
Datum nameregexne(PG_FUNCTION_ARGS) { Name n = PG_GETARG_NAME(0); text *p = PG_GETARG_TEXT_PP(1); PG_RETURN_BOOL(!RE_compile_and_execute(p, NameStr(*n), strlen(NameStr(*n)), REG_ADVANCED, PG_GET_COLLATION(), 0, NULL)); }
/* * SQL function for creating a new physical (streaming replication) * replication slot. */ Datum pg_create_physical_replication_slot(PG_FUNCTION_ARGS) { Name name = PG_GETARG_NAME(0); bool immediately_reserve = PG_GETARG_BOOL(1); Datum values[2]; bool nulls[2]; TupleDesc tupdesc; HeapTuple tuple; Datum result; Assert(!MyReplicationSlot); if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE) elog(ERROR, "return type must be a row type"); check_permissions(); CheckSlotRequirements(); /* acquire replication slot, this will check for conflicting names */ ReplicationSlotCreate(NameStr(*name), false, RS_PERSISTENT); values[0] = NameGetDatum(&MyReplicationSlot->data.name); nulls[0] = false; if (immediately_reserve) { /* Reserve WAL as the user asked for it */ ReplicationSlotReserveWal(); /* Write this slot to disk */ ReplicationSlotMarkDirty(); ReplicationSlotSave(); values[1] = LSNGetDatum(MyReplicationSlot->data.restart_lsn); nulls[1] = false; } else { values[0] = NameGetDatum(&MyReplicationSlot->data.name); nulls[1] = true; } tuple = heap_form_tuple(tupdesc, values, nulls); result = HeapTupleGetDatum(tuple); ReplicationSlotRelease(); PG_RETURN_DATUM(result); }
/* ---------- * convert to ASCII - enc is set as 'name' arg. * ---------- */ Datum to_ascii_encname(PG_FUNCTION_ARGS) { text *data = PG_GETARG_TEXT_P_COPY(0); char *encname = NameStr(*PG_GETARG_NAME(1)); int enc = pg_char_to_encoding(encname); if (enc < 0) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("%s is not a valid encoding name", encname))); PG_RETURN_TEXT_P(encode_to_ascii(data, enc)); }
Datum pg_tablespace_size_name(PG_FUNCTION_ARGS) { Name tblspcName = PG_GETARG_NAME(0); Oid tblspcOid = get_tablespace_oid(NameStr(*tblspcName), false); int64 size; size = calculate_tablespace_size(tblspcOid); if (size < 0) PG_RETURN_NULL(); PG_RETURN_INT64(size); }
Datum pg_database_size_name(PG_FUNCTION_ARGS) { Name dbName = PG_GETARG_NAME(0); Oid dbOid = get_database_oid(NameStr(*dbName), false); int64 size; size = calculate_database_size(dbOid); if (size == 0) PG_RETURN_NULL(); PG_RETURN_INT64(size); }