Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b8gLX-00018X-Gg for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Jun 2016 04:04:31 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1b8gLX-0003cH-2a for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Jun 2016 04:04:31 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b8gKA-00026v-0Z for pgsql-hackers@postgresql.org; Fri, 03 Jun 2016 04:03:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1b8gK7-0003uC-1F for pgsql-hackers@postgresql.org; Fri, 03 Jun 2016 04:03:04 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u53430Js023768; Fri, 3 Jun 2016 00:03:00 -0400 From: Tom Lane To: Jeff Janes , Teodor Sigaev cc: pgsql-hackers Subject: Re: [BUGS] BUG #14155: bloom index error with unlogged table In-reply-to: <24270.1464184933@sss.pgh.pa.us> References: <20160524144146.22598.42558@wrigleys.postgresql.org> <22311.1464134907@sss.pgh.pa.us> <24270.1464184933@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Wed, 25 May 2016 10:02:13 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <23719.1464926508.0@sss.pgh.pa.us> Date: Fri, 03 Jun 2016 00:03:00 -0400 Message-ID: <23767.1464926580@sss.pgh.pa.us> X-Pg-Spam-Score: -3.3 (---) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <23719.1464926508.1@sss.pgh.pa.us> I wrote: > Jeff Janes writes: >> My biggest gripe with it at the moment is that the signature size should be >> expressed in bits, and then internally rounded up to a multiple of 16, >> rather than having it be expressed in 'uint16'. >> If that were done it would be easier to fix the documentation to be more >> understandable. > +1 ... that sort of definition seems much more future-proof, too. > IMO it's not too late to change this. (We probably don't want to change > the on-disk representation of the reloptions, but we could convert from > bits to words in bloptions().) There were no objections to this, but also no action. Attached is a draft patch ... any complaints? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="bloom-express-length-in-bits.patch"; charset="us-ascii" Content-ID: <23719.1464926508.2@sss.pgh.pa.us> Content-Description: bloom-express-length-in-bits.patch Content-Transfer-Encoding: quoted-printable diff --git a/contrib/bloom/bloom.h b/contrib/bloom/bloom.h index c21eebf..a9daf63 100644 *** a/contrib/bloom/bloom.h --- b/contrib/bloom/bloom.h *************** typedef BloomPageOpaqueData *BloomPageOp *** 79,96 **** #define BLOOM_HEAD_BLKNO (1) /* first data page */ =20=20 /* ! * Maximum of bloom signature length in uint16. Actual value ! * is 512 bytes */ ! #define MAX_BLOOM_LENGTH (256) =20=20 /* Bloom index options */ typedef struct BloomOptions { int32 vl_len_; /* varlena header (do not touch directly!) */ ! int bloomLength; /* length of signature in uint16 */ ! int bitSize[INDEX_MAX_KEYS]; /* signature bits per index ! * key */ } BloomOptions; =20=20 /* --- 79,108 ---- #define BLOOM_HEAD_BLKNO (1) /* first data page */ =20=20 /* ! * We store Bloom signatures as arrays of uint16 words. */ ! typedef uint16 BloomSignatureWord; !=20 ! #define SIGNWORDBITS ((int) (BITS_PER_BYTE * sizeof(BloomSignatureWord))) !=20 ! /* ! * Default and maximum Bloom signature length in bits. ! */ ! #define DEFAULT_BLOOM_LENGTH (5 * SIGNWORDBITS) ! #define MAX_BLOOM_LENGTH (256 * SIGNWORDBITS) !=20 ! /* ! * Default and maximum signature bits generated per index key. ! */ ! #define DEFAULT_BLOOM_BITS 2 ! #define MAX_BLOOM_BITS (MAX_BLOOM_LENGTH - 1) =20=20 /* Bloom index options */ typedef struct BloomOptions { int32 vl_len_; /* varlena header (do not touch directly!) */ ! int bloomLength; /* length of signature in words (not bits!) */ ! int bitSize[INDEX_MAX_KEYS]; /* signature bits per index key */ } BloomOptions; =20=20 /* *************** typedef struct BloomState *** 143,154 **** /* * Tuples are very different from all other relations */ - typedef uint16 SignType; -=20 typedef struct BloomTuple { ItemPointerData heapPtr; ! SignType sign[FLEXIBLE_ARRAY_MEMBER]; } BloomTuple; =20=20 #define BLOOMTUPLEHDRSZ offsetof(BloomTuple, sign) --- 155,164 ---- /* * Tuples are very different from all other relations */ typedef struct BloomTuple { ItemPointerData heapPtr; ! BloomSignatureWord sign[FLEXIBLE_ARRAY_MEMBER]; } BloomTuple; =20=20 #define BLOOMTUPLEHDRSZ offsetof(BloomTuple, sign) *************** typedef struct BloomTuple *** 156,162 **** /* Opaque data structure for bloom index scan */ typedef struct BloomScanOpaqueData { ! SignType *sign; /* Scan signature */ BloomState state; } BloomScanOpaqueData; =20=20 --- 166,172 ---- /* Opaque data structure for bloom index scan */ typedef struct BloomScanOpaqueData { ! BloomSignatureWord *sign; /* Scan signature */ BloomState state; } BloomScanOpaqueData; =20=20 *************** extern void BloomFillMetapage(Relation i *** 170,176 **** extern void BloomInitMetapage(Relation index); extern void BloomInitPage(Page page, uint16 flags); extern Buffer BloomNewBuffer(Relation index); ! extern void signValue(BloomState * state, SignType * sign, Datum value, i= nt attno); extern BloomTuple *BloomFormTuple(BloomState * state, ItemPointer iptr, D= atum *values, bool *isnull); extern bool BloomPageAddItem(BloomState * state, Page page, BloomTuple * = tuple); =20=20 --- 180,186 ---- extern void BloomInitMetapage(Relation index); extern void BloomInitPage(Page page, uint16 flags); extern Buffer BloomNewBuffer(Relation index); ! extern void signValue(BloomState * state, BloomSignatureWord * sign, Datu= m value, int attno); extern BloomTuple *BloomFormTuple(BloomState * state, ItemPointer iptr, D= atum *values, bool *isnull); extern bool BloomPageAddItem(BloomState * state, Page page, BloomTuple * = tuple); =20=20 diff --git a/contrib/bloom/blscan.c b/contrib/bloom/blscan.c index aebf32a..0c954dc 100644 *** a/contrib/bloom/blscan.c --- b/contrib/bloom/blscan.c *************** blgetbitmap(IndexScanDesc scan, TIDBitma *** 93,99 **** /* New search: have to calculate search signature */ ScanKey skey =3D scan->keyData; =20=20 ! so->sign =3D palloc0(sizeof(SignType) * so->state.opts.bloomLength); =20=20 for (i =3D 0; i < scan->numberOfKeys; i++) { --- 93,99 ---- /* New search: have to calculate search signature */ ScanKey skey =3D scan->keyData; =20=20 ! so->sign =3D palloc0(sizeof(BloomSignatureWord) * so->state.opts.bloomL= ength); =20=20 for (i =3D 0; i < scan->numberOfKeys; i++) { diff --git a/contrib/bloom/blutils.c b/contrib/bloom/blutils.c index 4a5b343..8f13476 100644 *** a/contrib/bloom/blutils.c --- b/contrib/bloom/blutils.c *************** *** 27,49 **** =20=20 #include "bloom.h" =20=20 ! /* Signature dealing macros */ ! #define BITSIGNTYPE (BITS_PER_BYTE * sizeof(SignType)) ! #define GETWORD(x,i) ( *( (SignType*)(x) + (int)( (i) / BITSIGNTYPE ) ) ) ! #define CLRBIT(x,i) GETWORD(x,i) &=3D ~( 0x01 << ( (i) % BITSIGNTYPE ) ) ! #define SETBIT(x,i) GETWORD(x,i) |=3D ( 0x01 << ( (i) % BITSIGNTYPE ) ) ! #define GETBIT(x,i) ( (GETWORD(x,i) >> ( (i) % BITSIGNTYPE )) & 0x01 ) =20=20 PG_FUNCTION_INFO_V1(blhandler); =20=20 ! /* Kind of relation optioms for bloom index */ static relopt_kind bl_relopt_kind; =20=20 static int32 myRand(void); static void mySrand(uint32 seed); =20=20 /* ! * Module initialize function: initilized relation options. */ void _PG_init(void) --- 27,52 ---- =20=20 #include "bloom.h" =20=20 ! /* Signature dealing macros - note i is assumed to be of type int */ ! #define GETWORD(x,i) ( *( (BloomSignatureWord *)(x) + ( (i) / SIGNWORDBIT= S ) ) ) ! #define CLRBIT(x,i) GETWORD(x,i) &=3D ~( 0x01 << ( (i) % SIGNWORDBITS )= ) ! #define SETBIT(x,i) GETWORD(x,i) |=3D ( 0x01 << ( (i) % SIGNWORDBITS )= ) ! #define GETBIT(x,i) ( (GETWORD(x,i) >> ( (i) % SIGNWORDBITS )) & 0x01 ) =20=20 PG_FUNCTION_INFO_V1(blhandler); =20=20 ! /* Kind of relation options for bloom index */ static relopt_kind bl_relopt_kind; + /* parse table for fillRelOptions */ + static relopt_parse_elt bl_relopt_tab[INDEX_MAX_KEYS + 1]; =20=20 static int32 myRand(void); static void mySrand(uint32 seed); =20=20 /* ! * Module initialize function: initialize info about Bloom relation optio= ns. ! * ! * Note: keep this in sync with makeDefaultBloomOptions(). */ void _PG_init(void) *************** _PG_init(void) *** 53,70 **** =20=20 bl_relopt_kind =3D add_reloption_kind(); =20=20 add_int_reloption(bl_relopt_kind, "length", ! "Length of signature in uint16 type", 5, 1, 256); =20=20 for (i =3D 0; i < INDEX_MAX_KEYS; i++) { ! snprintf(buf, 16, "col%d", i + 1); add_int_reloption(bl_relopt_kind, buf, ! "Number of bits for corresponding column", 2, 1, 2048); } } =20=20 /* * Bloom handler function: return IndexAmRoutine with access method param= eters * and callbacks. */ --- 56,101 ---- =20=20 bl_relopt_kind =3D add_reloption_kind(); =20=20 + /* Option for length of signature */ add_int_reloption(bl_relopt_kind, "length", ! "Length of signature in bits", ! DEFAULT_BLOOM_LENGTH, 1, MAX_BLOOM_LENGTH); ! bl_relopt_tab[0].optname =3D "length"; ! bl_relopt_tab[0].opttype =3D RELOPT_TYPE_INT; ! bl_relopt_tab[0].offset =3D offsetof(BloomOptions, bloomLength); =20=20 + /* Number of bits for each possible index column: col1, col2, ... */ for (i =3D 0; i < INDEX_MAX_KEYS; i++) { ! snprintf(buf, sizeof(buf), "col%d", i + 1); add_int_reloption(bl_relopt_kind, buf, ! "Number of bits for corresponding column", ! DEFAULT_BLOOM_BITS, 1, MAX_BLOOM_BITS); ! bl_relopt_tab[i + 1].optname =3D MemoryContextStrdup(TopMemoryContext, ! buf); ! bl_relopt_tab[i + 1].opttype =3D RELOPT_TYPE_INT; ! bl_relopt_tab[i + 1].offset =3D offsetof(BloomOptions, bitSize[i]); } } =20=20 /* + * Construct a default set of Bloom options. + */ + static BloomOptions * + makeDefaultBloomOptions(void) + { + BloomOptions *opts; + int i; +=20 + opts =3D (BloomOptions *) palloc0(sizeof(BloomOptions)); + opts->bloomLength =3D (DEFAULT_BLOOM_LENGTH + SIGNWORDBITS - 1) / SIGNWO= RDBITS; + for (i =3D 0; i < INDEX_MAX_KEYS; i++) + opts->bitSize[i] =3D DEFAULT_BLOOM_BITS; + SET_VARSIZE(opts, sizeof(BloomOptions)); + return opts; + } +=20 + /* * Bloom handler function: return IndexAmRoutine with access method param= eters * and callbacks. */ *************** initBloomState(BloomState *state, Relati *** 157,163 **** =20=20 memcpy(&state->opts, index->rd_amcache, sizeof(state->opts)); state->sizeOfBloomTuple =3D BLOOMTUPLEHDRSZ + ! sizeof(SignType) * state->opts.bloomLength; } =20=20 /* --- 188,194 ---- =20=20 memcpy(&state->opts, index->rd_amcache, sizeof(state->opts)); state->sizeOfBloomTuple =3D BLOOMTUPLEHDRSZ + ! sizeof(BloomSignatureWord) * state->opts.bloomLength; } =20=20 /* *************** mySrand(uint32 seed) *** 208,214 **** * Add bits of given value to the signature. */ void ! signValue(BloomState *state, SignType *sign, Datum value, int attno) { uint32 hashVal; int nBit, --- 239,245 ---- * Add bits of given value to the signature. */ void ! signValue(BloomState *state, BloomSignatureWord *sign, Datum value, int a= ttno) { uint32 hashVal; int nBit, *************** signValue(BloomState *state, SignType *s *** 231,238 **** =20=20 for (j =3D 0; j < state->opts.bitSize[attno]; j++) { ! /* prevent mutiple evaluation */ ! nBit =3D myRand() % (state->opts.bloomLength * BITSIGNTYPE); SETBIT(sign, nBit); } } --- 262,269 ---- =20=20 for (j =3D 0; j < state->opts.bitSize[attno]; j++) { ! /* prevent multiple evaluation in SETBIT macro */ ! nBit =3D myRand() % (state->opts.bloomLength * SIGNWORDBITS); SETBIT(sign, nBit); } } *************** BloomInitPage(Page page, uint16 flags) *** 362,400 **** } =20=20 /* - * Adjust options of bloom index. - * - * This must produce default options when *opts is initially all-zero. - */ - static void - adjustBloomOptions(BloomOptions *opts) - { - int i; -=20 - /* Default length of bloom filter is 5 of 16-bit integers */ - if (opts->bloomLength <=3D 0) - opts->bloomLength =3D 5; - else if (opts->bloomLength > MAX_BLOOM_LENGTH) - ereport(ERROR, - (errcode(ERRCODE_INVALID_PARAMETER_VALUE), - errmsg("length of bloom signature (%d) is greater than maximum %d", - opts->bloomLength, MAX_BLOOM_LENGTH))); -=20 - /* Check signature length */ - for (i =3D 0; i < INDEX_MAX_KEYS; i++) - { - /* - * Zero and negative number of bits is meaningless. Also setting - * more bits than signature have seems useless. Replace both cases - * with 2 bits default. - */ - if (opts->bitSize[i] <=3D 0 - || opts->bitSize[i] >=3D opts->bloomLength * sizeof(SignType) * BITS_P= ER_BYTE) - opts->bitSize[i] =3D 2; - } - } -=20 - /* * Fill in metapage for bloom index. */ void --- 393,398 ---- *************** BloomFillMetapage(Relation index, Page m *** 405,418 **** =20=20 /* * Choose the index's options. If reloptions have been assigned, use ! * those, otherwise create default options by applying adjustBloomOptions ! * to a zeroed chunk of memory. We apply adjustBloomOptions to existing ! * reloptions too, just out of paranoia; they should be valid already. */ opts =3D (BloomOptions *) index->rd_options; if (!opts) ! opts =3D (BloomOptions *) palloc0(sizeof(BloomOptions)); ! adjustBloomOptions(opts); =20=20 /* * Initialize contents of meta page, including a copy of the options, --- 403,413 ---- =20=20 /* * Choose the index's options. If reloptions have been assigned, use ! * those, otherwise create default options. */ opts =3D (BloomOptions *) index->rd_options; if (!opts) ! opts =3D makeDefaultBloomOptions(); =20=20 /* * Initialize contents of meta page, including a copy of the options, *************** bloptions(Datum reloptions, bool validat *** 462,491 **** relopt_value *options; int numoptions; BloomOptions *rdopts; - relopt_parse_elt tab[INDEX_MAX_KEYS + 1]; - int i; - char buf[16]; -=20 - /* Option for length of signature */ - tab[0].optname =3D "length"; - tab[0].opttype =3D RELOPT_TYPE_INT; - tab[0].offset =3D offsetof(BloomOptions, bloomLength); -=20 - /* Number of bits for each of possible columns: col1, col2, ... */ - for (i =3D 0; i < INDEX_MAX_KEYS; i++) - { - snprintf(buf, sizeof(buf), "col%d", i + 1); - tab[i + 1].optname =3D pstrdup(buf); - tab[i + 1].opttype =3D RELOPT_TYPE_INT; - tab[i + 1].offset =3D offsetof(BloomOptions, bitSize[i]); - } =20=20 options =3D parseRelOptions(reloptions, validate, bl_relopt_kind, &numop= tions); rdopts =3D allocateReloptStruct(sizeof(BloomOptions), options, numoption= s); fillRelOptions((void *) rdopts, sizeof(BloomOptions), options, numoption= s, ! validate, tab, INDEX_MAX_KEYS + 1); =20=20 ! adjustBloomOptions(rdopts); =20=20 return (bytea *) rdopts; } --- 457,471 ---- relopt_value *options; int numoptions; BloomOptions *rdopts; =20=20 + /* Parse the user-given reloptions */ options =3D parseRelOptions(reloptions, validate, bl_relopt_kind, &numop= tions); rdopts =3D allocateReloptStruct(sizeof(BloomOptions), options, numoption= s); fillRelOptions((void *) rdopts, sizeof(BloomOptions), options, numoption= s, ! validate, bl_relopt_tab, lengthof(bl_relopt_tab)); =20=20 ! /* Convert signature length to words, rounding up */ ! rdopts->bloomLength =3D (rdopts->bloomLength + SIGNWORDBITS - 1) / SIGNW= ORDBITS; =20=20 return (bytea *) rdopts; } diff --git a/doc/src/sgml/bloom.sgml b/doc/src/sgml/bloom.sgml index 49cb066..abf30e7 100644 *** a/doc/src/sgml/bloom.sgml --- b/doc/src/sgml/bloom.sgml *************** *** 8,15 **** =20=20 ! bloom is a module which implements an index access method. = It comes ! as an example of custom access methods and generic WAL records usage. = But it is also useful in itself. =20=20 --- 8,15 ---- =20=20 ! bloom is a module that implements an index access method. = It comes ! as an example of custom access methods and generic WAL record usage. B= ut it is also useful in itself. =20=20 *************** *** 22,29 **** allows fast exclusion of non-candidate tuples via signatures. Since a signature is a lossy representation of all indexed attributes, search results must be rechecked using heap information. ! The user can specify signature length (in uint16, default is 5) and the ! number of bits, which can be set per attribute (1 < colN < 2048). =20=20 --- 22,30 ---- allows fast exclusion of non-candidate tuples via signatures. Since a signature is a lossy representation of all indexed attributes, search results must be rechecked using heap information. ! The user can specify signature length in bits (default 80, maximum 409= 6) ! and the number of bits generated for each index column (default 2, ! maximum 4095). =20=20 *************** *** 51,64 **** length ! Length of signature in uint16 type values ! col1 — col16 Number of bits for corresponding column --- 52,65 ---- length ! Length of signature in bits ! col1 — col32 Number of bits for corresponding column *************** *** 77,88 **** =20=20 CREATE INDEX bloomidx ON tbloom USING bloom (i1,i2,i3) ! WITH (length=3D5, col1=3D2, col2=3D2, col3=3D4); =20=20 Here, we created a bloom index with a signature length of 80 bits, ! and attributes i1 and i2 mapped to 2 bits, and attribute i3 to 4 bits. =20=20 --- 78,89 ---- =20=20 CREATE INDEX bloomidx ON tbloom USING bloom (i1,i2,i3) ! WITH (length=3D80, col1=3D2, col2=3D2, col3=3D4); =20=20 Here, we created a bloom index with a signature length of 80 bits, ! and attributes i1 and i2 mapped to 2 bits, and attribute i3 mapped to = 4 bits. =20=20 ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers ------- =_aaaaaaaaaa0--