Exemplo n.º 1
0
Datum __cmsketch_merge(PG_FUNCTION_ARGS)
{
    bytea *     counterblob1 = PG_GETARG_BYTEA_P(0);
    bytea *     counterblob2 = PG_GETARG_BYTEA_P(1);
    cmtransval *transval1 = (cmtransval *)VARDATA(counterblob1);
    cmtransval *transval2 = (cmtransval *)VARDATA(counterblob2);
    cmtransval *newtrans;
    countmin *  sketches2 = (countmin *)
                            ((cmtransval *)(VARDATA(counterblob2)))->sketches;
    bytea *     newblob;
    countmin *  newsketches;
    uint32      i, j, k;
    int         sz;

    /* make sure they're initialized! */
    if (!CM_TRANSVAL_INITIALIZED(counterblob1)
        && !CM_TRANSVAL_INITIALIZED(counterblob2))
        /* if both are empty can return one of them */
        PG_RETURN_DATUM(PointerGetDatum(counterblob1));
    else if (!CM_TRANSVAL_INITIALIZED(counterblob1)) {
        counterblob1 = cmsketch_init_transval(transval2->typOid);
        transval1 = (cmtransval *)VARDATA(counterblob1);
    }
    else if (!CM_TRANSVAL_INITIALIZED(counterblob2)) {
        counterblob2 = cmsketch_init_transval(transval1->typOid);
        transval2 = (cmtransval *)VARDATA(counterblob2);
    }

    sz = VARSIZE(counterblob1);
    /* allocate a new transval as a copy of counterblob1 */
    newblob = (bytea *)palloc(sz);
    memcpy(newblob, counterblob1, sz);
    newtrans = (cmtransval *)(VARDATA(newblob));
    newsketches = (countmin *)(newtrans)->sketches;

    /* add in values from counterblob2 */
    for (i = 0; i < RANGES; i++)
        for (j = 0; j < DEPTH; j++)
            for (k = 0; k < NUMCOUNTERS; k++)
                newsketches[i][j][k] += sketches2[i][j][k];

    if (newtrans->nargs == -1) {
        /* transfer in the args from the other input */
        newtrans->nargs = transval2->nargs;
        for (i = 0; (int)i < transval2->nargs; i++)
            newtrans->args[i] = transval2->args[i];
    }

    PG_RETURN_DATUM(PointerGetDatum(newblob));
}
Exemplo n.º 2
0
Datum
pg_decrypt_iv(PG_FUNCTION_ARGS)
{
	int			err;
	bytea	   *data,
			   *key,
			   *iv,
			   *res;
	text	   *type;
	PX_Combo   *c;
	unsigned	dlen,
				klen,
				rlen,
				ivlen;

	type = PG_GETARG_TEXT_P(3);
	c = find_provider(type, (PFN) px_find_combo, "Cipher", 0);

	data = PG_GETARG_BYTEA_P(0);
	key = PG_GETARG_BYTEA_P(1);
	iv = PG_GETARG_BYTEA_P(2);
	dlen = VARSIZE(data) - VARHDRSZ;
	klen = VARSIZE(key) - VARHDRSZ;
	ivlen = VARSIZE(iv) - VARHDRSZ;

	rlen = px_combo_decrypt_len(c, dlen);
	res = palloc(VARHDRSZ + rlen);

	err = px_combo_init(c, (uint8 *) VARDATA(key), klen,
						(uint8 *) VARDATA(iv), ivlen);
	if (!err)
		px_combo_decrypt(c, (uint8 *) VARDATA(data), dlen,
						 (uint8 *) VARDATA(res), &rlen);

	px_combo_free(c);

	if (err)
		ereport(ERROR,
				(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
				 errmsg("decrypt_iv error: %s", px_strerror(err))));

	SET_VARSIZE(res, VARHDRSZ + rlen);

	PG_FREE_IF_COPY(data, 0);
	PG_FREE_IF_COPY(key, 1);
	PG_FREE_IF_COPY(iv, 2);
	PG_FREE_IF_COPY(type, 3);

	PG_RETURN_BYTEA_P(res);
}
Exemplo n.º 3
0
Datum
pg_encrypt(PG_FUNCTION_ARGS)
{
	int			err;
	bytea	   *data,
			   *key,
			   *res;
	text	   *type;
	PX_Combo   *c;
	unsigned	dlen,
				klen,
				rlen;

	if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
		PG_RETURN_NULL();

	type = PG_GETARG_TEXT_P(2);
	c = find_provider(type, (PFN) px_find_combo, "Cipher", 0);

	data = PG_GETARG_BYTEA_P(0);
	key = PG_GETARG_BYTEA_P(1);
	dlen = VARSIZE(data) - VARHDRSZ;
	klen = VARSIZE(key) - VARHDRSZ;

	rlen = px_combo_encrypt_len(c, dlen);
	res = palloc(VARHDRSZ + rlen);

	err = px_combo_init(c, (uint8 *) VARDATA(key), klen, NULL, 0);
	if (!err)
		err = px_combo_encrypt(c, (uint8 *) VARDATA(data), dlen,
							   (uint8 *) VARDATA(res), &rlen);
	px_combo_free(c);

	PG_FREE_IF_COPY(data, 0);
	PG_FREE_IF_COPY(key, 1);
	PG_FREE_IF_COPY(type, 2);

	if (err)
	{
		pfree(res);
		ereport(ERROR,
				(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
				 errmsg("encrypt error: %s", px_strerror(err))));
	}

	VARATT_SIZEP(res) = VARHDRSZ + rlen;
	PG_RETURN_BYTEA_P(res);
}
Exemplo n.º 4
0
Datum
adaptive_counter_update(PG_FUNCTION_ARGS)
{
    
    bytea * data;
    AdaptiveCounter ac;
  
    /* is the counter created (if not, create it - error 1%, 10mil items) */
    if (PG_ARGISNULL(0)) {
      ac = as_create(0.01, 4);
      data = (bytea*)palloc(ac->length + VARHDRSZ);
      SET_VARSIZE(data, ac->length);
      memcpy(VARDATA(data), ac, ac->length);
      elog(NOTICE, "Adaptive Counter: %d bytes", ac->length);
    } else {
      data = PG_GETARG_BYTEA_P(0);
    }
    
    ac = (AdaptiveCounter)VARDATA(data);
      
    /* get the new item */
    text * item = PG_GETARG_TEXT_P(1);
    
    /* in-place update works only if executed as aggregate */
    as_add_element(ac, VARDATA(item), VARSIZE(item) - VARHDRSZ);
    
    /* return the updated bytea */
    PG_RETURN_BYTEA_P(data);
    
}
Exemplo n.º 5
0
Datum
lowrite(PG_FUNCTION_ARGS)
{
	int32		fd = PG_GETARG_INT32(0);
	bytea	   *wbuf = PG_GETARG_BYTEA_P(1);
	int			bytestowrite;
	int			totalwritten;

#ifdef PGXC
#ifdef XCP
	ereport(ERROR,
			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
			 errmsg("Postgres-XL does not yet support large objects"),
			 errdetail("The feature is not currently supported")));
#else
	ereport(ERROR,
			(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
			 errmsg("Postgres-XC does not support large object yet"),
			 errdetail("The feature is not currently supported")));
#endif
#endif

	bytestowrite = VARSIZE(wbuf) - VARHDRSZ;
	totalwritten = lo_write(fd, VARDATA(wbuf), bytestowrite);
	PG_RETURN_INT32(totalwritten);
}
Exemplo n.º 6
0
Datum
brin_page_type(PG_FUNCTION_ARGS)
{
	bytea	   *raw_page = PG_GETARG_BYTEA_P(0);
	Page		page = VARDATA(raw_page);
	BrinSpecialSpace *special;
	char *type;

	special = (BrinSpecialSpace *) PageGetSpecialPointer(page);

	switch (special->type)
	{
		case BRIN_PAGETYPE_META:
			type = "meta";
			break;
		case BRIN_PAGETYPE_REVMAP:
			type = "revmap";
			break;
		case BRIN_PAGETYPE_REGULAR:
			type = "regular";
			break;
		default:
			type = psprintf("unknown (%02x)", special->type);
			break;
	}

	PG_RETURN_TEXT_P(cstring_to_text(type));
}
Exemplo n.º 7
0
Datum
brin_page_type(PG_FUNCTION_ARGS)
{
    bytea	   *raw_page = PG_GETARG_BYTEA_P(0);
    Page		page = VARDATA(raw_page);
    char	   *type;

    switch (BrinPageType(page))
    {
    case BRIN_PAGETYPE_META:
        type = "meta";
        break;
    case BRIN_PAGETYPE_REVMAP:
        type = "revmap";
        break;
    case BRIN_PAGETYPE_REGULAR:
        type = "regular";
        break;
    default:
        type = psprintf("unknown (%02x)", BrinPageType(page));
        break;
    }

    PG_RETURN_TEXT_P(cstring_to_text(type));
}
Exemplo n.º 8
0
/*
 * This is the UDF interface.  It does sanity checks and preps values
 * for the interesting logic in countmin_dyadic_trans_c
 */
Datum __cmsketch_int8_trans(PG_FUNCTION_ARGS)
{
    bytea *     transblob = NULL;
    cmtransval *transval;

    /*
     * This function makes destructive updates to its arguments.
     * Make sure it's being called in an agg context.
     */

    if (!(fcinfo->context &&
          (IsA(fcinfo->context, AggState)
    #ifdef NOTGP
           || IsA(fcinfo->context, WindowAggState)
    #endif
          )))
        elog(ERROR,
             "destructive pass by reference outside agg");

    /* get the provided element, being careful in case it's NULL */
    if (!PG_ARGISNULL(1)) {
        transblob = cmsketch_check_transval(fcinfo, true);
        transval = (cmtransval *)(VARDATA(transblob));

        /* the following line modifies the contents of transval, and hence transblob */
        countmin_dyadic_trans_c(transval, PG_GETARG_DATUM(1));
        PG_RETURN_DATUM(PointerGetDatum(transblob));
    }
    else PG_RETURN_DATUM(PointerGetDatum(PG_GETARG_BYTEA_P(0)));
}
Exemplo n.º 9
0
Datum
pg_armor(PG_FUNCTION_ARGS)
{
	bytea	   *data;
	text	   *res;
	int			data_len,
				res_len,
				guess_len;

	data = PG_GETARG_BYTEA_P(0);
	data_len = VARSIZE(data) - VARHDRSZ;

	guess_len = pgp_armor_enc_len(data_len);
	res = palloc(VARHDRSZ + guess_len);

	res_len = pgp_armor_encode((uint8 *) VARDATA(data), data_len,
							   (uint8 *) VARDATA(res));
	if (res_len > guess_len)
		ereport(ERROR,
				(errcode(ERRCODE_EXTERNAL_ROUTINE_INVOCATION_EXCEPTION),
				 errmsg("Overflow - encode estimate too small")));
	SET_VARSIZE(res, VARHDRSZ + res_len);

	PG_FREE_IF_COPY(data, 0);
	PG_RETURN_TEXT_P(res);
}
Exemplo n.º 10
0
/*
 * bytea_size
 *
 * Return the size of a bytea field, this data is useful to pass for
 * to a function doing decompression like decompress_data() above.
 */
Datum
bytea_size(PG_FUNCTION_ARGS)
{
	bytea  *data = PG_GETARG_BYTEA_P(0);

	PG_RETURN_INT32(VARSIZE(data) - VARHDRSZ);
}
Exemplo n.º 11
0
Datum HASHAPI_Hash_1_Bytea(PG_FUNCTION_ARGS)
{
    int32 num_segs;            /* number of segments  */
	bytea    *val1;        	   /* bytea value		  */
    unsigned int targetbucket; /* 0-based             */
    int16 algorithm;  /* hashing algorithm   */
	Datum d1;
	Oid oid;

	/* Get number of segments */
    num_segs = PG_GETARG_INT32(0);
	
	/* Get hashing algoriithm */
	algorithm = PG_GETARG_INT16(1);
	
	/* Get the value to hash */
	val1 = PG_GETARG_BYTEA_P(2);
	
	d1 = PointerGetDatum(val1);
	
	/* create a CdbHash for this hash test. */
	h = makeCdbHash(num_segs, algorithm);
	
	cdbhashinit(h);
	oid = BYTEAOID;
	cdbhash(h, d1, oid);
	
	/* reduce the result hash value */
	targetbucket = cdbhashreduce(h);	
	
	/* Avoid leaking memory for toasted inputs */
	PG_FREE_IF_COPY(val1, 2);
	
	PG_RETURN_INT32(targetbucket); /* return target bucket (segID) */
}
Exemplo n.º 12
0
Datum
pg_digest(PG_FUNCTION_ARGS)
{
	bytea	   *arg;
	text	   *name;
	unsigned	len,
				hlen;
	PX_MD	   *md;
	bytea	   *res;

	name = PG_GETARG_TEXT_P(1);

	/* will give error if fails */
	md = find_provider(name, (PFN) px_find_digest, "Digest", 0);

	hlen = px_md_result_size(md);

	res = (text *) palloc(hlen + VARHDRSZ);
	SET_VARSIZE(res, hlen + VARHDRSZ);

	arg = PG_GETARG_BYTEA_P(0);
	len = VARSIZE(arg) - VARHDRSZ;

	px_md_update(md, (uint8 *) VARDATA(arg), len);
	px_md_finish(md, (uint8 *) VARDATA(res));
	px_md_free(md);

	PG_FREE_IF_COPY(arg, 0);
	PG_FREE_IF_COPY(name, 1);

	PG_RETURN_BYTEA_P(res);
}
Exemplo n.º 13
0
Datum
brin_metapage_info(PG_FUNCTION_ARGS)
{
    bytea	   *raw_page = PG_GETARG_BYTEA_P(0);
    Page		page;
    BrinMetaPageData *meta;
    TupleDesc	tupdesc;
    Datum		values[4];
    bool		nulls[4];
    HeapTuple	htup;

    page = verify_brin_page(raw_page, BRIN_PAGETYPE_META, "metapage");

    /* Build a tuple descriptor for our result type */
    if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
        elog(ERROR, "return type must be a row type");
    tupdesc = BlessTupleDesc(tupdesc);

    /* Extract values from the metapage */
    meta = (BrinMetaPageData *) PageGetContents(page);
    MemSet(nulls, 0, sizeof(nulls));
    values[0] = CStringGetTextDatum(psprintf("0x%08X", meta->brinMagic));
    values[1] = Int32GetDatum(meta->brinVersion);
    values[2] = Int32GetDatum(meta->pagesPerRange);
    values[3] = Int64GetDatum(meta->lastRevmapPage);

    htup = heap_form_tuple(tupdesc, values, nulls);

    PG_RETURN_DATUM(HeapTupleGetDatum(htup));
}
Exemplo n.º 14
0
Datum
binary_encode(PG_FUNCTION_ARGS)
{
	bytea	   *data = PG_GETARG_BYTEA_P(0);
	Datum		name = PG_GETARG_DATUM(1);
	text	   *result;
	char	   *namebuf;
	int			datalen,
				resultlen,
				res;
	const struct pg_encoding *enc;

	datalen = VARSIZE(data) - VARHDRSZ;

	namebuf = TextDatumGetCString(name);

	enc = pg_find_encoding(namebuf);
	if (enc == NULL)
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
				 errmsg("unrecognized encoding: \"%s\"", namebuf)));

	resultlen = enc->encode_len(VARDATA(data), datalen);
	result = palloc(VARHDRSZ + resultlen);

	res = enc->encode(VARDATA(data), datalen, VARDATA(result));

	/* Make this FATAL 'cause we've trodden on memory ... */
	if (res > resultlen)
		elog(FATAL, "overflow - encode estimate too small");

	SET_VARSIZE(result, VARHDRSZ + res);

	PG_RETURN_TEXT_P(result);
}
Exemplo n.º 15
0
Datum x509_get_serial_number(PG_FUNCTION_ARGS) {
	bytea *raw;
	bytea *result;
	BIGNUM *bn;
	X509 *cert;

	// check for null value.
	raw = PG_GETARG_BYTEA_P(0);
	if (raw == NULL || VARSIZE(raw) == VARHDRSZ) {
		PG_RETURN_NULL();
	}

	cert = x509_from_bytea(raw);
	if (cert == NULL) {
		ereport(ERROR,
				(errcode(ERRCODE_DATA_CORRUPTED), errmsg(
						"unable to decode X509 record")));
	}

	bn = ASN1_INTEGER_to_BN(X509_get_serialNumber(cert), NULL);
	result = bn_to_bytea(bn);
	BN_free(bn);
	X509_free(cert);

	PG_RETURN_BYTEA_P(result);
}
Exemplo n.º 16
0
Datum
fsm_page_contents(PG_FUNCTION_ARGS)
{
	bytea	   *raw_page = PG_GETARG_BYTEA_P(0);
	StringInfoData sinfo;
	FSMPage		fsmpage;
	int			i;

	if (!superuser())
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
				 (errmsg("must be superuser to use raw page functions"))));

	fsmpage = (FSMPage) PageGetContents(VARDATA(raw_page));

	initStringInfo(&sinfo);

	for (i = 0; i < NodesPerPage; i++)
	{
		if (fsmpage->fp_nodes[i] != 0)
			appendStringInfo(&sinfo, "%d: %d\n", i, fsmpage->fp_nodes[i]);
	}
	appendStringInfo(&sinfo, "fp_next_slot: %d\n", fsmpage->fp_next_slot);

	PG_RETURN_TEXT_P(cstring_to_text_with_len(sinfo.data, sinfo.len));
}
Exemplo n.º 17
0
Datum LWGEOM_from_WKB(PG_FUNCTION_ARGS)
{
	bytea *bytea_wkb = (bytea*)PG_GETARG_BYTEA_P(0);
	int32 srid = 0;
	GSERIALIZED *geom;
	LWGEOM *lwgeom;
	uint8_t *wkb = (uint8_t*)VARDATA(bytea_wkb);
	
	lwgeom = lwgeom_from_wkb(wkb, VARSIZE(bytea_wkb)-VARHDRSZ, LW_PARSER_CHECK_ALL);
	
	if ( lwgeom_needs_bbox(lwgeom) )
		lwgeom_add_bbox(lwgeom);
	
	geom = geometry_serialize(lwgeom);
	lwgeom_free(lwgeom);
	PG_FREE_IF_COPY(bytea_wkb, 0);
	
	if ( gserialized_get_srid(geom) != SRID_UNKNOWN )
	{
		elog(WARNING, "OGC WKB expected, EWKB provided - use GeometryFromEWKB() for this");
	}
	
	if ( PG_NARGS() > 1 )
	{
		srid = PG_GETARG_INT32(1);
		if ( srid != gserialized_get_srid(geom) )
			gserialized_set_srid(geom, srid);
	}

	PG_RETURN_POINTER(geom);
}
Exemplo n.º 18
0
Datum is_valid_mol_pkl(PG_FUNCTION_ARGS) {
  bytea *data = PG_GETARG_BYTEA_P(0);
  int len = VARSIZE(data) - VARHDRSZ;
  bool res = isValidMolBlob(VARDATA(data), len);
  PG_FREE_IF_COPY(data, 0);
  PG_RETURN_BOOL(res);
}
Exemplo n.º 19
0
Datum
gin_metapage_info(PG_FUNCTION_ARGS)
{
	bytea	   *raw_page = PG_GETARG_BYTEA_P(0);
	int			raw_page_size;
	TupleDesc	tupdesc;
	Page		page;
	GinPageOpaque opaq;
	GinMetaPageData *metadata;
	HeapTuple	resultTuple;
	Datum		values[10];
	bool		nulls[10];

	if (!superuser())
		ereport(ERROR,
				(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
				 (errmsg("must be superuser to use raw page functions"))));

	raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
	if (raw_page_size < BLCKSZ)
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
				 errmsg("input page too small (%d bytes)", raw_page_size)));
	page = VARDATA(raw_page);

	opaq = (GinPageOpaque) PageGetSpecialPointer(page);
	if (opaq->flags != GIN_META)
		ereport(ERROR,
				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
				 errmsg("input page is not a GIN metapage"),
				 errdetail("Flags %04X, expected %04X",
						   opaq->flags, GIN_META)));

	/* Build a tuple descriptor for our result type */
	if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
		elog(ERROR, "return type must be a row type");

	metadata = GinPageGetMeta(page);

	memset(nulls, 0, sizeof(nulls));

	values[0] = Int64GetDatum(metadata->head);
	values[1] = Int64GetDatum(metadata->tail);
	values[2] = Int32GetDatum(metadata->tailFreeSize);
	values[3] = Int64GetDatum(metadata->nPendingPages);
	values[4] = Int64GetDatum(metadata->nPendingHeapTuples);

	/* statistics, updated by VACUUM */
	values[5] = Int64GetDatum(metadata->nTotalPages);
	values[6] = Int64GetDatum(metadata->nEntryPages);
	values[7] = Int64GetDatum(metadata->nDataPages);
	values[8] = Int64GetDatum(metadata->nEntries);

	values[9] = Int32GetDatum(metadata->ginVersion);

	/* Build and return the result tuple. */
	resultTuple = heap_form_tuple(tupdesc, values, nulls);

	return HeapTupleGetDatum(resultTuple);
}
Exemplo n.º 20
0
Datum
adaptive_get_error(PG_FUNCTION_ARGS)
{

    /* return the error rate of the counter */
    PG_RETURN_FLOAT4(((AdaptiveCounter)PG_GETARG_BYTEA_P(0))->error);

}
Exemplo n.º 21
0
Datum hll_merge(PG_FUNCTION_ARGS) {
	bytea    *state;
	uint32_t *sdata;

	bytea    *value = PG_GETARG_BYTEA_P(1);
	uint32_t *vdata = (uint32_t *) VARDATA(value);

	if ( PG_ARGISNULL(0) ) {
		PG_RETURN_BYTEA_P(value);
	} else {
		state = PG_GETARG_BYTEA_P(0);
		sdata = (uint32_t *) VARDATA(state);
	}

	merge_sets(1 << sdata[0], vdata + 2, sdata + 2);
	PG_RETURN_BYTEA_P(state);
}
Exemplo n.º 22
0
Datum
tdigest_add(PG_FUNCTION_ARGS)
{
	TDigest *t = tdigest_unpack(PG_GETARG_BYTEA_P(0));
	float8 incoming = PG_GETARG_FLOAT8(1);
	TDigestAdd(t, incoming, 1);
	fcinfo->nargs = 1;
	fcinfo->arg[0] = PointerGetDatum(t);
	return tdigest_send(fcinfo);
}
Exemplo n.º 23
0
Datum
adaptive_get_ndistinct(PG_FUNCTION_ARGS)
{

    AdaptiveCounter ac = (AdaptiveCounter)PG_GETARG_BYTEA_P(0);

    /* return the updated bytea */
    PG_RETURN_INT32(ac->ndistinct);

}
Datum
probabilistic_merge_simple(PG_FUNCTION_ARGS)
{

    ProbabilisticCounter counter1 = (ProbabilisticCounter)PG_GETARG_BYTEA_P(0);
    ProbabilisticCounter counter2 = (ProbabilisticCounter)PG_GETARG_BYTEA_P(1);

    /* is the counter created (if not, create it - error 1%, 10mil items) */
    if (PG_ARGISNULL(0) && PG_ARGISNULL(1)) {
        PG_RETURN_NULL();
    } else if (PG_ARGISNULL(0)) {
        PG_RETURN_BYTEA_P(pc_copy(counter2));
    } else if (PG_ARGISNULL(1)) {
        PG_RETURN_BYTEA_P(pc_copy(counter1));
    } else {
        PG_RETURN_BYTEA_P(pc_merge(counter1, counter2, false));
    }

}
Exemplo n.º 25
0
Datum pgbloomfun_add(PG_FUNCTION_ARGS)
{
  bytea *newbloomba, *bloomba = PG_GETARG_BYTEA_P(0);
  text *key = PG_GETARG_TEXT_P(1);
  pgbloom_t *pgbloom = get_pgbloom(bloomba);
  bloom_t newbloom, *bloom = NULL;
  size_t newbloom_size;
  int space_left, i;

  space_left = (pgbloom->last_capacity > pgbloom->last_entries) ||
               (pgbloom->growth_factor == 0);
  for (i=0; i<pgbloom->filters; i++)
    {
      bloom = next_bloom(bloomba, bloom);
      if (bloom == NULL)
        {
          elog(ERROR, "pgbloomfun: missing filter in bloom object");
        }
      if (i == pgbloom->filters - 1 && space_left)
        {
          if (bloom_add(bloom, VARDATA(key), VARSIZE(key) - VARHDRSZ) == 0)
            {
              pgbloom->total_entries ++;
              pgbloom->last_entries ++;
            }
          PG_RETURN_BYTEA_P(bloomba);
        }
      else if (bloom_check(bloom, VARDATA(key), VARSIZE(key) - VARHDRSZ))
        {
          PG_RETURN_BYTEA_P(bloomba);  /* key already exists */
        }
    }

  /* create a new filter */
  pgbloom->filters += 1;
  pgbloom->total_entries += 1;
  pgbloom->last_entries = 1;
  pgbloom->last_capacity *= pgbloom->growth_factor;
  pgbloom->total_capacity += pgbloom->last_capacity;

  /* calculate and allocate space */
  bloom_init(&newbloom, pgbloom->last_capacity, pgbloom->error_rate);
  newbloom_size = sizeof(newbloom) + newbloom.bits / 8;
  newbloomba = palloc(VARSIZE(bloomba) + newbloom_size);
  memcpy(newbloomba, bloomba, VARSIZE(bloomba));
  SET_VARSIZE(newbloomba, VARSIZE(bloomba) + newbloom_size);

  /* initialize the new bloom filter and add the new key to it */
  bloom = (bloom_t *) (((unsigned char *) newbloomba) + VARSIZE(bloomba));
  memset(bloom, 0, newbloom_size);
  memcpy(bloom, &newbloom, sizeof(newbloom));
  bloom_add(bloom, VARDATA(key), VARSIZE(key) - VARHDRSZ);

  PG_RETURN_BYTEA_P(newbloomba);
}
Exemplo n.º 26
0
Datum
tdigest_out(PG_FUNCTION_ARGS)
{
	StringInfoData buf;
	TDigest *t = tdigest_unpack(PG_GETARG_BYTEA_P(0));

	initStringInfo(&buf);
	appendStringInfo(&buf, "{ count = %ld, k = %d, centroids: %d }", t->total_weight, (int) t->compression, t->num_centroids);

	PG_RETURN_CSTRING(buf.data);
}
Exemplo n.º 27
0
Arquivo: pipe.c Projeto: orafce/orafce
Datum
dbms_pipe_pack_message_bytea(PG_FUNCTION_ARGS)
{
	bytea *data = PG_GETARG_BYTEA_P(0);

	output_buffer = check_buffer(output_buffer, LOCALMSGSZ);
	pack_field(output_buffer, IT_BYTEA,
		VARSIZE_ANY_EXHDR(data), VARDATA_ANY(data), InvalidOid);

	PG_RETURN_VOID();
}
Exemplo n.º 28
0
Datum
pg_hmac(PG_FUNCTION_ARGS)
{
	bytea	   *arg;
	bytea	   *key;
	text	   *name;
	unsigned	len,
				hlen,
				klen;
	PX_HMAC    *h;
	bytea	   *res;

	if (PG_ARGISNULL(0) || PG_ARGISNULL(1) || PG_ARGISNULL(2))
		PG_RETURN_NULL();

	name = PG_GETARG_TEXT_P(2);

	/* will give error if fails */
	h = find_provider(name, (PFN) px_find_hmac, "HMAC", 0);

	hlen = px_hmac_result_size(h);

	res = (text *) palloc(hlen + VARHDRSZ);
	VARATT_SIZEP(res) = hlen + VARHDRSZ;

	arg = PG_GETARG_BYTEA_P(0);
	key = PG_GETARG_BYTEA_P(1);
	len = VARSIZE(arg) - VARHDRSZ;
	klen = VARSIZE(key) - VARHDRSZ;

	px_hmac_init(h, (uint8 *) VARDATA(key), klen);
	px_hmac_update(h, (uint8 *) VARDATA(arg), len);
	px_hmac_finish(h, (uint8 *) VARDATA(res));
	px_hmac_free(h);

	PG_FREE_IF_COPY(arg, 0);
	PG_FREE_IF_COPY(key, 1);
	PG_FREE_IF_COPY(name, 2);

	PG_RETURN_BYTEA_P(res);
}
Exemplo n.º 29
0
Datum
pgp_pub_encrypt_text(PG_FUNCTION_ARGS)
{
	bytea	   *data,
			   *key;
	text	   *arg = NULL;
	text	   *res;

	data = PG_GETARG_BYTEA_P(0);
	key = PG_GETARG_BYTEA_P(1);
	if (PG_NARGS() > 2)
		arg = PG_GETARG_BYTEA_P(2);

	res = encrypt_internal(1, 1, data, key, arg);

	PG_FREE_IF_COPY(data, 0);
	PG_FREE_IF_COPY(key, 1);
	if (PG_NARGS() > 2)
		PG_FREE_IF_COPY(arg, 2);
	PG_RETURN_TEXT_P(res);
}
Exemplo n.º 30
0
Datum
lowrite(PG_FUNCTION_ARGS)
{
	int32		fd = PG_GETARG_INT32(0);
	bytea	   *wbuf = PG_GETARG_BYTEA_P(1);
	int			bytestowrite;
	int			totalwritten;

	bytestowrite = VARSIZE(wbuf) - VARHDRSZ;
	totalwritten = lo_write(fd, VARDATA(wbuf), bytestowrite);
	PG_RETURN_INT32(totalwritten);
}