8000 Use FLEXIBLE_ARRAY_MEMBER for HeapTupleHeaderData.t_bits[]. · postgrespro/postgres@e1a11d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit e1a11d9

Browse files
committed
Use FLEXIBLE_ARRAY_MEMBER for HeapTupleHeaderData.t_bits[].
This requires changing quite a few places that were depending on sizeof(HeapTupleHeaderData), but it seems for the best. Michael Paquier, some adjustments by me
1 parent 3d9b6f3 commit e1a11d9

File tree

19 files changed

+102
-99
lines changed
  • util
  • replication/logical
  • utils/adt
  • include
  • 19 files changed

    +102
    -99
    lines changed

    contrib/file_fdw/file_fdw.c

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -932,7 +932,7 @@ estimate_size(PlannerInfo *root, RelOptInfo *baserel,
    932932
    int tuple_width;
    933933

    934934
    tuple_width = MAXALIGN(baserel->width) +
    935-
    MAXALIGN(sizeof(HeapTupleHeaderData));
    935+
    MAXALIGN(SizeofHeapTupleHeader);
    936936
    ntuples = clamp_row_est((double) stat_buf.st_size /
    937937
    (double) tuple_width);
    938938
    }

    contrib/pageinspect/heapfuncs.c

    Lines changed: 9 additions & 8 deletions
    Original file line numberDiff line numberDiff line change
    @@ -149,7 +149,7 @@ heap_page_items(PG_FUNCTION_ARGS)
    149149
    * many other ways, but at least we won't crash.
    150150
    */
    151151
    if (ItemIdHasStorage(id) &&
    152-
    lp_len >= sizeof(HeapTupleHeader) &&
    152+
    lp_len >= MinHeapTupleSize &&
    153153
    lp_offset == MAXALIGN(lp_offset) &&
    154154
    lp_offset + lp_len <= raw_page_size)
    155155
    {
    @@ -169,18 +169,19 @@ heap_page_items(PG_FUNCTION_ARGS)
    169169
    values[10] = UInt8GetDatum(tuphdr->t_hoff);
    170170

    171171
    /*
    172-
    * We already checked that the item as is completely within the
    173-
    * raw page passed to us, with the length given in the line
    174-
    * pointer.. Let's check that t_hoff doesn't point over lp_len,
    175-
    * before using it to access t_bits and oid.
    172+
    * We already checked that the item is completely within the raw
    173+
    * page passed to us, with the length given in the line pointer.
    174+
    * Let's check that t_hoff doesn't point over lp_len, before using
    175+
    * it to access t_bits and oid.
    176176
    */
    177-
    if (tuphdr->t_hoff >= sizeof(HeapTupleHeader) &&
    178-
    tuphdr->t_hoff <= lp_len)
    177+
    if (tuphdr->t_hoff >= SizeofHeapTupleHeader &&
    178+
    tuphdr->t_hoff <= lp_len &&
    179+
    tuphdr->t_hoff == MAXALIGN(tuphdr->t_hoff))
    179180
    {
    180181
    if (tuphdr->t_infomask & HEAP_HASNULL)
    181182
    {
    182183
    bits_len = tuphdr->t_hoff -
    183-
    (((char *) tuphdr->t_bits) -((char *) tuphdr));
    184+
    offsetof(HeapTupleHeaderData, t_bits);
    184185

    185186
    values[11] = CStringGetTextDatum(
    186187
    bits_to_text(tuphdr->t_bits, bits_len * 8));

    contrib/postgres_fdw/postgres_fdw.c

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -519,7 +519,7 @@ postgresGetForeignRelSize(PlannerInfo *root,
    519519
    {
    520520
    baserel->pages = 10;
    521521
    baserel->tuples =
    522-
    (10 * BLCKSZ) / (baserel->width + sizeof(HeapTupleHeaderData));
    522+
    (10 * BLCKSZ) / (baserel->width + MAXALIGN(SizeofHeapTupleHeader));
    523523
    }
    524524

    525525
    /* Estimate baserel size as best we can with local statistics. */

    src/backend/access/common/heaptuple.c

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -1434,7 +1434,7 @@ heap_form_minimal_tuple(TupleDesc tupleDescriptor,
    14341434
    /*
    14351435
    * Determine total space needed
    14361436
    */
    1437-
    len = offsetof(MinimalTupleData, t_bits);
    1437+
    len = SizeofMinimalTupleHeader;
    14381438

    14391439
    if (hasnull)
    14401440
    len += BITMAPLEN(numberOfAttributes);

    src/backend/access/heap/heapam.c

    Lines changed: 28 additions & 28 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2186,8 +2186,8 @@ heap_insert(Relation relation, HeapTuple tup, CommandId cid,
    21862186
    XLogRegisterBufData(0, (char *) &xlhdr, SizeOfHeapHeader);
    21872187
    /* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
    21882188
    XLogRegisterBufData(0,
    2189-
    (char *) heaptup->t_data + offsetof(HeapTupleHeaderData, t_bits),
    2190-
    heaptup->t_len - offsetof(HeapTupleHeaderData, t_bits));
    2189+
    (char *) heaptup->t_data + SizeofHeapTupleHeader,
    2190+
    heaptup->t_len - SizeofHeapTupleHeader);
    21912191

    21922192
    recptr = XLogInsert(RM_HEAP_ID, info);
    21932193

    @@ -2460,9 +2460,9 @@ heap_multi_insert(Relation relation, HeapTuple *tuples, int ntuples,
    24602460
    tuphdr->t_hoff = heaptup->t_data->t_hoff;
    24612461

    24622462
    /* write bitmap [+ padding] [+ oid] + data */
    2463-
    datalen = heaptup->t_len - offsetof(HeapTupleHeaderData, t_bits);
    2463+
    datalen = heaptup->t_len - SizeofHeapTupleHeader;
    24642464
    memcpy(scratchptr,
    2465-
    (char *) heaptup->t_data + offsetof(HeapTupleHeaderData, t_bits),
    2465+
    (char *) heaptup->t_data + SizeofHeapTupleHeader,
    24662466
    datalen);
    24672467
    tuphdr->datalen = datalen;
    24682468
    scratchptr += datalen;
    @@ -2904,9 +2904,9 @@ heap_delete(Relation relation, ItemPointer tid,
    29042904

    29052905
    XLogRegisterData((char *) &xlhdr, SizeOfHeapHeader);
    29062906
    XLogRegisterData((char *) old_key_tuple->t_data
    2907-
    + offsetof(HeapTupleHeaderData, t_bits),
    2907+
    + SizeofHeapTupleHeader,
    29082908
    old_key_tuple->t_len
    2909-
    - offsetof(HeapTupleHeaderData, t_bits));
    2909+
    - SizeofHeapTupleHeader);
    29102910
    }
    29112911

    29122912
    recptr = XLogInsert(RM_HEAP_ID, XLOG_HEAP_DELETE);
    @@ -6732,7 +6732,7 @@ log_heap_update(Relation reln, Buffer oldbuf,
    67326732
    xlhdr.t_infomask2 = newtup->t_data->t_infomask2;
    67336733
    xlhdr.t_infomask = newtup->t_data->t_infomask;
    67346734
    xlhdr.t_hoff = newtup->t_data->t_hoff;
    6735-
    Assert(offsetof(HeapTupleHeaderData, t_bits) + prefixlen + suffixlen <= newtup->t_len);
    6735+
    Assert(SizeofHeapTupleHeader + prefixlen + suffixlen <= newtup->t_len);
    67366736

    67376737
    /*
    67386738
    * PG73FORMAT: write bitmap [+ padding] [+ oid] + data
    @@ -6743,8 +6743,8 @@ log_heap_update(Relation reln, Buffer oldbuf,
    67436743
    if (prefixlen == 0)
    67446744
    {
    67456745
    XLogRegisterBufData(0,
    6746-
    ((char *) newtup->t_data) + offsetof(HeapTupleHeaderData, t_bits),
    6747-
    newtup->t_len - offsetof(HeapTupleHeaderData, t_bits) -suffixlen);
    6746+
    ((char *) newtup->t_data) + SizeofHeapTupleHeader,
    6747+
    newtup->t_len - SizeofHeapTupleHeader - suffixlen);
    67486748
    }
    67496749
    else
    67506750
    {
    @@ -6753,11 +6753,11 @@ log_heap_update(Relation reln, Buffer oldbuf,
    67536753
    * two separate rdata entries.
    67546754
    */
    67556755
    /* bitmap [+ padding] [+ oid] */
    6756-
    if (newtup->t_data->t_hoff - offsetof(HeapTupleHeaderData, t_bits) >0)
    6756+
    if (newtup->t_data->t_hoff - SizeofHeapTupleHeader > 0)
    67576757
    {
    67586758
    XLogRegisterBufData(0,
    6759-
    ((char *) newtup->t_data) + offsetof(HeapTupleHeaderData, t_bits),
    6760-
    newtup->t_data->t_hoff - offsetof(HeapTupleHeaderData, t_bits));
    6759+
    ((char *) newtup->t_data) + SizeofHeapTupleHeader,
    6760+
    newtup->t_data->t_hoff - SizeofHeapTupleHeader);
    67616761
    }
    67626762

    67636763
    /* data after common prefix */
    @@ -6777,8 +6777,8 @@ log_heap_update(Relation reln, Buffer oldbuf,
    67776777
    XLogRegisterData((char *) &xlhdr_idx, SizeOfHeapHeader);
    67786778

    67796779
    /* PG73FORMAT: write bitmap [+ padding] [+ oid] + data */
    6780-
    XLogRegisterData((char *) old_key_tuple->t_data + offsetof(HeapTupleHeaderData, t_bits),
    6781-
    old_key_tuple->t_len - offsetof(HeapTupleHeaderData, t_bits));
    6780+
    XLogRegisterData((char *) old_key_tuple->t_data + SizeofHeapTupleHeader,
    6781+
    old_key_tuple->t_len - SizeofHeapTupleHeader);
    67826782
    }
    67836783

    67846784
    recptr = XLogInsert(RM_HEAP_ID, info);
    @@ -7351,7 +7351,7 @@ heap_xlog_insert(XLogReaderState *record)
    73517351
    xl_heap_insert *xlrec = (xl_heap_insert *) XLogRecGetData(record);
    73527352
    Buffer buffer;
    73537353
    Page page;
    7354-
    struct
    7354+
    union
    73557355
    {
    73567356
    HeapTupleHeaderData hdr;
    73577357
    char data[MaxHeapTupleSize];
    @@ -7415,12 +7415,12 @@ heap_xlog_insert(XLogReaderState *record)
    74157415
    data += SizeOfHeapHeader;
    74167416

    74177417
    htup = &tbuf.hdr;
    7418-
    MemSet((char *) htup, 0, sizeof(HeapTupleHeaderData));
    7418+
    MemSet((char *) htup, 0, SizeofHeapTupleHeader);
    74197419
    /* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
    7420-
    memcpy((char *) htup + offsetof(HeapTupleHeaderData, t_bits),
    7420+
    memcpy((char *) htup + SizeofHeapTupleHeader,
    74217421
    data,
    74227422
    newlen);
    7423-
    newlen += offsetof(HeapTupleHeaderData, t_bits);
    7423+
    newlen += SizeofHeapTupleHeader;
    74247424
    htup->t_infomask2 = xlhdr.t_infomask2;
    74257425
    htup->t_infomask = xlhdr.t_infomask;
    74267426
    htup->t_hoff = xlhdr.t_hoff;
    @@ -7469,7 +7469,7 @@ heap_xlog_multi_insert(XLogReaderState *record)
    74697469
    BlockNumber blkno;
    74707470
    Buffer buffer;
    74717471
    Page page;
    7472-
    struct
    7472+
    union
    74737473
    {
    74747474
    HeapTupleHeaderData hdr;
    74757475
    char data[MaxHeapTupleSize];
    @@ -7548,14 +7548,14 @@ heap_xlog_multi_insert(XLogReaderState *record)
    75487548
    newlen = xlhdr->datalen;
    75497549
    Assert(newlen <= MaxHeapTupleSize);
    75507550
    htup = &tbuf.hdr;
    7551-
    MemSet((char *) htup, 0, sizeof(HeapTupleHeaderData));
    7551+
    MemSet((char *) htup, 0, SizeofHeapTupleHeader);
    75527552
    /* PG73FORMAT: get bitmap [+ padding] [+ oid] + data */
    7553-
    memcpy((char *) htup + offsetof(HeapTupleHeaderData, t_bits),
    7553+
    memcpy((char *) htup + SizeofHeapTupleHeader,
    75547554
    (char *) tupdata,
    75557555
    newlen);
    75567556
    tupdata += newlen;
    75577557

    7558-
    newlen += offsetof(HeapTupleHeaderData, t_bits);
    7558+
    newlen += SizeofHeapTupleHeader;
    75597559
    htup->t_infomask2 = xlhdr->t_infomask2;
    75607560
    htup->t_infomask = xlhdr->t_infomask;
    75617561
    htup->t_hoff = xlhdr->t_hoff;
    @@ -7618,7 +7618,7 @@ heap_xlog_update(XLogReaderState *record, bool hot_update)
    76187618
    uint16 prefixlen = 0,
    76197619
    suffixlen = 0;
    76207620
    char *newp;
    7621-
    struct
    7621+
    union
    76227622
    {
    76237623
    HeapTupleHeaderData hdr;
    76247624
    char data[MaxHeapTupleSize];
    @@ -7780,19 +7780,19 @@ heap_xlog_update(XLogReaderState *record, bool hot_update)
    77807780
    Assert(tuplen <= MaxHeapTupleSize);
    77817781

    77827782
    htup = &tbuf.hdr;
    7783-
    MemSet((char *) htup, 0, sizeof(HeapTupleHeaderData));
    7783+
    MemSet((char *) htup, 0, SizeofHeapTupleHeader);
    77847784

    77857785
    /*
    77867786
    * Reconstruct the new tuple using the prefix and/or suffix from the
    77877787
    * old tuple, and the data stored in the WAL record.
    77887788
    */
    7789-
    newp = (char *) htup + offsetof(HeapTupleHeaderData, t_bits);
    7789+
    newp = (char *) htup + SizeofHeapTupleHeader;
    77907790
    if (prefixlen > 0)
    77917791
    {
    77927792
    int len;
    77937793

    77947794
    /* copy bitmap [+ padding] [+ oid] from WAL record */
    7795-
    len = xlhdr.t_hoff - offsetof(HeapTupleHeaderData, t_bits);
    7795+
    len = xlhdr.t_hoff - SizeofHeapTupleHeader;
    77967796
    memcpy(newp, recdata, len);
    77977797
    recdata += len;
    77987798
    newp += len;
    @@ -7802,7 +7802,7 @@ heap_xlog_update(XLogReaderState *record, bool hot_update)
    78027802
    newp += prefixlen;
    78037803

    78047804
    /* copy new tuple data from WAL record */
    7805-
    len = tuplen - (xlhdr.t_hoff - offsetof(HeapTupleHeaderData, t_bits));
    7805+
    len = tuplen - (xlhdr.t_hoff - SizeofHeapTupleHeader);
    78067806
    memcpy(newp, recdata, len);
    78077807
    recdata += len;
    78087808
    newp += len;
    @@ -7823,7 +7823,7 @@ heap_xlog_update(XLogReaderState *record, bool hot_update)
    78237823
    if (suffixlen > 0)
    78247824
    memcpy(newp, (char *) oldtup.t_data + oldtup.t_len - suffixlen, suffixlen);
    78257825

    7826-
    newlen = offsetof(HeapTupleHeaderData, t_bits) + tuplen + prefixlen + suffixlen;
    7826+
    newlen = SizeofHeapTupleHeader + tuplen + prefixlen + suffixlen;
    78277827
    htup->t_infomask2 = xlhdr.t_infomask2;
    78287828
    htup->t_infomask = xlhdr.t_infomask;
    78297829
    htup->t_hoff = xlhdr.t_hoff;

    src/backend/access/heap/tuptoaster.c

    Lines changed: 5 additions & 5 deletions
    Original file line numberDiff line numberDiff line change
    @@ -677,7 +677,7 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup,
    677677
    */
    678678

    679679
    /* compute header overhead --- this should match heap_form_tuple() */
    680-
    hoff = offsetof(HeapTupleHeaderData, t_bits);
    680+
    hoff = SizeofHeapTupleHeader;
    681681
    if (has_nulls)
    682682
    hoff += BITMAPLEN(numAttrs);
    683683
    if (newtup->t_data->t_infomask & HEAP_HASOID)
    @@ -963,7 +963,7 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup,
    963963
    * different conclusion about the size of the null bitmap, or even
    964964
    * whether there needs to be one at all.
    965965
    */
    966-
    new_header_len = offsetof(HeapTupleHeaderData, t_bits);
    966+
    new_header_len = SizeofHeapTupleHeader;
    967967
    if (has_nulls)
    968968
    new_header_len += BITMAPLEN(numAttrs);
    969969
    if (olddata->t_infomask & HEAP_HASOID)
    @@ -986,7 +986,7 @@ toast_insert_or_update(Relation rel, HeapTuple newtup, HeapTuple oldtup,
    986986
    /*
    987987
    * Copy the existing tuple header, but adjust natts and t_hoff.
    988988
    */
    989-
    memcpy(new_data, olddata, offsetof(HeapTupleHeaderData, t_bits));
    989+
    memcpy(new_data, olddata, SizeofHeapTupleHeader);
    990990
    HeapTupleHeaderSetNatts(new_data, numAttrs);
    991991
    new_data->t_hoff = new_header_len;
    992992
    if (olddata->t_infomask & HEAP_HASOID)
    @@ -1196,7 +1196,7 @@ toast_flatten_tuple_to_datum(HeapTupleHeader tup,
    11961196
    *
    11971197
    * This should match the reconstruction code in toast_insert_or_update.
    11981198
    */
    1199-
    new_header_len = offsetof(HeapTupleHeaderData, t_bits);
    1199+
    new_header_len = SizeofHeapTupleHeader;
    12001200
    if (has_nulls)
    12011201
    new_header_len += BITMAPLEN(numAttrs);
    12021202
    if (tup->t_infomask & HEAP_HASOID)
    @@ -1211,7 +1211,7 @@ toast_flatten_tuple_to_datum(HeapTupleHeader tup,
    12111211
    /*
    12121212
    * Copy the existing tuple header, but adjust natts and t_hoff.
    12131213
    */
    1214-
    memcpy(new_data, tup, offsetof(HeapTupleHeaderData, t_bits));
    1214+
    memcpy(new_data, tup, SizeofHeapTupleHeader);
    12151215
    HeapTupleHeaderSetNatts(new_data, numAttrs);
    12161216
    new_data->t_hoff = new_header_len;
    12171217
    if (tup->t_infomask & HEAP_HASOID)

    src/backend/catalog/toasting.c

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -447,7 +447,7 @@ needs_toast_table(Relation rel)
    447447
    return false; /* nothing to toast? */
    448448
    if (maxlength_unknown)
    449449
    return true; /* any unlimited-length attrs? */
    450-
    tuple_length = MAXALIGN(offsetof(HeapTupleHeaderData, t_bits) +
    450+
    tuple_length = MAXALIGN(SizeofHeapTupleHeader +
    451451
    BITMAPLEN(tupdesc->natts)) +
    452452
    MAXALIGN(data_length);
    453453
    return (tuple_length > TOAST_TUPLE_THRESHOLD);

    src/backend/executor/nodeHash.c

    Lines changed: 1 addition & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -439,7 +439,7 @@ ExecChooseHashTableSize(double ntuples, int tupwidth, bool useskew,
    439439
    * don't count palloc overhead either.
    440440
    */
    441441
    tupsize = HJTUPLE_OVERHEAD +
    442-
    MAXALIGN(sizeof(MinimalTupleData)) +
    442+
    MAXALIGN(SizeofMinimalTupleHeader) +
    443443
    MAXALIGN(tupwidth);
    444444
    inner_rel_bytes = ntuples * tupsize;
    445445

    src/backend/optimizer/path/costsize.c

    Lines changed: 3 additions & 3 deletions
    Original file line numberDiff line numberDiff line change
    @@ -4036,11 +4036,11 @@ set_rel_width(PlannerInfo *root, RelOptInfo *rel)
    40364036

    40374037
    /*
    40384038
    * If we have a whole-row reference, estimate its width as the sum of
    4039-
    * per-column widths plus sizeof(HeapTupleHeaderData).
    4039+
    * per-column widths plus heap tuple header overhead.
    40404040
    */
    40414041
    if (have_wholerow_var)
    40424042
    {
    4043-
    int32 wholerow_width = sizeof(HeapTupleHeaderData);
    4043+
    int32 wholerow_width = MAXALIGN(SizeofHeapTupleHeader);
    40444044

    40454045
    if (reloid != InvalidOid)
    40464046
    {
    @@ -4078,7 +4078,7 @@ set_rel_width(PlannerInfo *root, RelOptInfo *rel)
    40784078
    static double
    40794079
    relation_byte_size(double tuples, int width)
    40804080
    {
    4081-
    return tuples * (MAXALIGN(width) + MAXALIGN(sizeof(HeapTupleHeaderData)));
    4081+
    return tuples * (MAXALIGN(width) + MAXALIGN(SizeofHeapTupleHeader));
    40824082
    }
    40834083

    40844084
    /*

    src/backend/optimizer/plan/planner.c

    Lines changed: 2 additions & 2 deletions
    Original file line numberDiff line numberDiff line change
    @@ -2755,7 +2755,7 @@ choose_hashed_grouping(PlannerInfo *root,
    27552755
    */
    27562756

    27572757
    /* Estimate per-hash-entry space at tuple width... */
    2758-
    hashentrysize = MAXALIGN(path_width) + MAXALIGN(sizeof(MinimalTupleData));
    2758+
    hashentrysize = MAXALIGN(path_width) + MAXALIGN(SizeofMinimalTupleHeader);
    27592759
    /* plus space for pass-by-ref transition values... */
    27602760
    hashentrysize += agg_costs->transitionSpace;
    27612761
    /* plus the per-hash-entry overhead */
    @@ -2923,7 +2923,7 @@ choose_hashed_distinct(PlannerInfo *root,
    29232923
    */
    29242924

    29252925
    /* Estimate per-hash-entry space at tuple width... */
    2926-
    hashentrysize = MAXALIGN(path_width) + MAXALIGN(sizeof(MinimalTupleData));
    2926+
    hashentrysize = MAXALIGN(path_width) + MAXALIGN(SizeofMinimalTupleHeader);
    29272927
    /* plus the per-hash-entry overhead */
    29282928
    hashentrysize += hash_agg_entry_size(0);
    29292929

    0 commit comments

    Comments
     (0)
    0