Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
/*-------------------------------------------------------------------------
 *
 * test_aio.c
 *      Helpers to write tests for AIO
 *
 * This module provides interface functions for C functionality to SQL, to
 * make it possible to test AIO related behavior in a targeted way from SQL.
 * It'd not generally be safe to export these functions to SQL, but for a test
 * that's fine.
 *
 * Copyright (c) 2020-2026, PostgreSQL Global Development Group
 *
 * IDENTIFICATION
 *    src/test/modules/test_aio/test_aio.c
 *
 *-------------------------------------------------------------------------
 */
 
#include "postgres.h"
 
#include "access/relation.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#include "funcapi.h"
#include "storage/aio.h"
#include "storage/aio_internal.h"
#include "storage/buf_internals.h"
#include "storage/bufmgr.h"
#include "storage/checksum.h"
#include "storage/condition_variable.h"
#include "storage/lwlock.h"
#include "storage/proc.h"
#include "storage/procnumber.h"
#include "storage/read_stream.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/injection_point.h"
#include "utils/rel.h"
#include "utils/tuplestore.h"
#include "utils/wait_event.h"
 
 
PG_MODULE_MAGIC;
 
 
/* In shared memory */
typedef struct InjIoErrorState
{
    ConditionVariable cv;
 
    bool        enabled_short_read;
    bool        enabled_reopen;
 
    bool        enabled_completion_wait;
    Oid         completion_wait_relfilenode;
    BlockNumber completion_wait_blockno;
    pid_t       completion_wait_pid;
    uint32      completion_wait_event;
 
    bool        short_read_result_set;
    Oid         short_read_relfilenode;
    pid_t       short_read_pid;
    int         short_read_result;
} InjIoErrorState;
 
typedef struct BlocksReadStreamData
{
    int         nblocks;
    int         curblock;
    uint32     *blocks;
} BlocksReadStreamData;
 
 
static InjIoErrorState *inj_io_error_state;
 
/* Shared memory init callbacks */
static void test_aio_shmem_request(void *arg);
static void test_aio_shmem_init(void *arg);
static void test_aio_shmem_attach(void *arg);
 
static const ShmemCallbacks inj_io_shmem_callbacks = {
    .request_fn = test_aio_shmem_request,
    .init_fn = test_aio_shmem_init,
    .attach_fn = test_aio_shmem_attach,
};
 
 
static PgAioHandle *last_handle;
 
 
 
static void
test_aio_shmem_request(void *arg)
{
    ShmemRequestStruct(.name = "test_aio injection points",
                       .size = sizeof(InjIoErrorState),
                       .ptr = (void **) &inj_io_error_state,
        );
}
 
static void
test_aio_shmem_init(void *arg)
{
    /* First time through, initialize */
    inj_io_error_state->enabled_short_read = false;
    inj_io_error_state->enabled_reopen = false;
    inj_io_error_state->enabled_completion_wait = false;
 
    ConditionVariableInit(&inj_io_error_state->cv);
    inj_io_error_state->completion_wait_event = WaitEventInjectionPointNew("completion_wait");
 
#ifdef USE_INJECTION_POINTS
    InjectionPointAttach("aio-process-completion-before-shared",
                         "test_aio",
                         "inj_io_completion_hook",
                         NULL,
                         0);
    InjectionPointLoad("aio-process-completion-before-shared");
 
    InjectionPointAttach("aio-worker-after-reopen",
                         "test_aio",
                         "inj_io_reopen",
                         NULL,
                         0);
    InjectionPointLoad("aio-worker-after-reopen");
 
#endif
}
 
static void
test_aio_shmem_attach(void *arg)
{
    /*
     * Pre-load the injection points now, so we can call them in a critical
     * section.
     */
#ifdef USE_INJECTION_POINTS
    InjectionPointLoad("aio-process-completion-before-shared");
    InjectionPointLoad("aio-worker-after-reopen");
    elog(LOG, "injection point loaded");
#endif
}
 
void
_PG_init(void)
{
    if (!process_shared_preload_libraries_in_progress)
        return;
 
    RegisterShmemCallbacks(&inj_io_shmem_callbacks);
}
 
 
PG_FUNCTION_INFO_V1(errno_from_string);
Datum
errno_from_string(PG_FUNCTION_ARGS)
{
    const char *sym = text_to_cstring(PG_GETARG_TEXT_PP(0));
 
    if (strcmp(sym, "EIO") == 0)
        PG_RETURN_INT32(EIO);
    else if (strcmp(sym, "EAGAIN") == 0)
        PG_RETURN_INT32(EAGAIN);
    else if (strcmp(sym, "EINTR") == 0)
        PG_RETURN_INT32(EINTR);
    else if (strcmp(sym, "ENOSPC") == 0)
        PG_RETURN_INT32(ENOSPC);
    else if (strcmp(sym, "EROFS") == 0)
        PG_RETURN_INT32(EROFS);
 
    ereport(ERROR,
            errcode(ERRCODE_INVALID_PARAMETER_VALUE),
            errmsg_internal("%s is not a supported errno value", sym));
    PG_RETURN_INT32(0);
}
 
PG_FUNCTION_INFO_V1(grow_rel);
Datum
grow_rel(PG_FUNCTION_ARGS)
{
    Oid         relid = PG_GETARG_OID(0);
    uint32      nblocks = PG_GETARG_UINT32(1);
    Relation    rel;
#define MAX_BUFFERS_TO_EXTEND_BY 64
    Buffer      victim_buffers[MAX_BUFFERS_TO_EXTEND_BY];
 
    rel = relation_open(relid, AccessExclusiveLock);
 
    while (nblocks > 0)
    {
        uint32      extend_by_pages;
 
        extend_by_pages = Min(nblocks, MAX_BUFFERS_TO_EXTEND_BY);
 
        ExtendBufferedRelBy(BMR_REL(rel),
                            MAIN_FORKNUM,
                            NULL,
                            0,
                            extend_by_pages,
                            victim_buffers,
                            &extend_by_pages);
 
        nblocks -= extend_by_pages;
 
        for (uint32 i = 0; i < extend_by_pages; i++)
        {
            ReleaseBuffer(victim_buffers[i]);
        }
    }
 
    relation_close(rel, NoLock);
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(modify_rel_block);
Datum
modify_rel_block(PG_FUNCTION_ARGS)
{
    Oid         relid = PG_GETARG_OID(0);
    BlockNumber blkno = PG_GETARG_UINT32(1);
    bool        zero = PG_GETARG_BOOL(2);
    bool        corrupt_header = PG_GETARG_BOOL(3);
    bool        corrupt_checksum = PG_GETARG_BOOL(4);
    Page        page = palloc_aligned(BLCKSZ, PG_IO_ALIGN_SIZE, 0);
    bool        flushed;
    Relation    rel;
    Buffer      buf;
    PageHeader  ph;
 
    rel = relation_open(relid, AccessExclusiveLock);
 
    buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno,
                             RBM_ZERO_ON_ERROR, NULL);
 
    LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
 
    /*
     * copy the page to local memory, seems nicer than to directly modify in
     * the buffer pool.
     */
    memcpy(page, BufferGetPage(buf), BLCKSZ);
 
    UnlockReleaseBuffer(buf);
 
    /*
     * Don't want to have a buffer in-memory that's marked valid where the
     * on-disk contents are invalid. Particularly not if the in-memory buffer
     * could be dirty...
     *
     * While we hold an AEL on the relation nobody else should be able to read
     * the buffer in.
     *
     * NB: This is probably racy, better don't copy this to non-test code.
     */
    if (BufferIsLocal(buf))
        InvalidateLocalBuffer(GetLocalBufferDescriptor(-buf - 1), true);
    else
        EvictUnpinnedBuffer(buf, &flushed);
 
    /*
     * Now modify the page as asked for by the caller.
     */
    if (zero)
        memset(page, 0, BufferGetPageSize(buf));
 
    if (PageIsEmpty(page) && (corrupt_header || corrupt_checksum))
        PageInit(page, BufferGetPageSize(buf), 0);
 
    ph = (PageHeader) page;
 
    if (corrupt_header)
        ph->pd_special = BLCKSZ + 1;
 
    if (corrupt_checksum)
    {
        bool        successfully_corrupted = 0;
 
        /*
         * Any single modification of the checksum could just end up being
         * valid again, due to e.g. corrupt_header changing the data in a way
         * that'd result in the "corrupted" checksum, or the checksum already
         * being invalid. Retry in that, unlikely, case.
         */
        for (int i = 0; i < 100; i++)
        {
            uint16      verify_checksum;
            uint16      old_checksum;
 
            old_checksum = ph->pd_checksum;
            ph->pd_checksum = old_checksum + 1;
 
            elog(LOG, "corrupting checksum of blk %u from %u to %u",
                 blkno, old_checksum, ph->pd_checksum);
 
            verify_checksum = pg_checksum_page(page, blkno);
            if (verify_checksum != ph->pd_checksum)
            {
                successfully_corrupted = true;
                break;
            }
        }
 
        if (!successfully_corrupted)
            elog(ERROR, "could not corrupt checksum, what's going on?");
    }
    else
    {
        PageSetChecksum(page, blkno);
    }
 
    smgrwrite(RelationGetSmgr(rel),
              MAIN_FORKNUM, blkno, page, true);
 
    relation_close(rel, NoLock);
 
    PG_RETURN_VOID();
}
 
/*
 * Ensures a buffer for rel & blkno is in shared buffers, without actually
 * caring about the buffer contents. Used to set up test scenarios.
 */
static Buffer
create_toy_buffer(Relation rel, BlockNumber blkno)
{
    Buffer      buf;
    BufferDesc *buf_hdr;
    uint64      buf_state;
    bool        was_pinned = false;
    uint64      unset_bits = 0;
 
    /* place buffer in shared buffers without erroring out */
    buf = ReadBufferExtended(rel, MAIN_FORKNUM, blkno, RBM_ZERO_AND_LOCK, NULL);
    LockBuffer(buf, BUFFER_LOCK_UNLOCK);
 
    if (RelationUsesLocalBuffers(rel))
    {
        buf_hdr = GetLocalBufferDescriptor(-buf - 1);
        buf_state = pg_atomic_read_u64(&buf_hdr->state);
    }
    else
    {
        buf_hdr = GetBufferDescriptor(buf - 1);
        buf_state = LockBufHdr(buf_hdr);
    }
 
    /*
     * We should be the only backend accessing this buffer. This is just a
     * small bit of belt-and-suspenders defense, none of this code should ever
     * run in a cluster with real data.
     */
    if (BUF_STATE_GET_REFCOUNT(buf_state) > 1)
        was_pinned = true;
    else
        unset_bits |= BM_VALID | BM_DIRTY;
 
    if (RelationUsesLocalBuffers(rel))
    {
        buf_state &= ~unset_bits;
        pg_atomic_unlocked_write_u64(&buf_hdr->state, buf_state);
    }
    else
    {
        UnlockBufHdrExt(buf_hdr, buf_state, 0, unset_bits, 0);
    }
 
    if (was_pinned)
        elog(ERROR, "toy buffer %d was already pinned",
             buf);
 
    return buf;
}
 
/*
 * A "low level" read. This does similar things to what
 * StartReadBuffers()/WaitReadBuffers() do, but provides more control (and
 * less sanity).
 */
PG_FUNCTION_INFO_V1(read_rel_block_ll);
Datum
read_rel_block_ll(PG_FUNCTION_ARGS)
{
    Oid         relid = PG_GETARG_OID(0);
    BlockNumber blkno = PG_GETARG_UINT32(1);
    int         nblocks = PG_GETARG_INT32(2);
    bool        wait_complete = PG_GETARG_BOOL(3);
    bool        batchmode_enter = PG_GETARG_BOOL(4);
    bool        call_smgrreleaseall = PG_GETARG_BOOL(5);
    bool        batchmode_exit = PG_GETARG_BOOL(6);
    bool        zero_on_error = PG_GETARG_BOOL(7);
    Relation    rel;
    Buffer      bufs[PG_IOV_MAX];
    BufferDesc *buf_hdrs[PG_IOV_MAX];
    Page        pages[PG_IOV_MAX];
    uint8       srb_flags = 0;
    PgAioReturn ior;
    PgAioHandle *ioh;
    PgAioWaitRef iow;
    SMgrRelation smgr;
 
    if (nblocks <= 0 || nblocks > PG_IOV_MAX)
        elog(ERROR, "nblocks is out of range");
 
    rel = relation_open(relid, AccessShareLock);
 
    for (int i = 0; i < nblocks; i++)
    {
        bufs[i] = create_toy_buffer(rel, blkno + i);
        pages[i] = BufferGetBlock(bufs[i]);
        buf_hdrs[i] = BufferIsLocal(bufs[i]) ?
            GetLocalBufferDescriptor(-bufs[i] - 1) :
            GetBufferDescriptor(bufs[i] - 1);
    }
 
    smgr = RelationGetSmgr(rel);
 
    pgstat_prepare_report_checksum_failure(smgr->smgr_rlocator.locator.dbOid);
 
    ioh = pgaio_io_acquire(CurrentResourceOwner, &ior);
    pgaio_io_get_wref(ioh, &iow);
 
    if (RelationUsesLocalBuffers(rel))
    {
        for (int i = 0; i < nblocks; i++)
            StartLocalBufferIO(buf_hdrs[i], true, true, NULL);
        pgaio_io_set_flag(ioh, PGAIO_HF_REFERENCES_LOCAL);
    }
    else
    {
        for (int i = 0; i < nblocks; i++)
            StartSharedBufferIO(buf_hdrs[i], true, true, NULL);
    }
 
    pgaio_io_set_handle_data_32(ioh, (uint32 *) bufs, nblocks);
 
    if (zero_on_error | zero_damaged_pages)
        srb_flags |= READ_BUFFERS_ZERO_ON_ERROR;
    if (ignore_checksum_failure)
        srb_flags |= READ_BUFFERS_IGNORE_CHECKSUM_FAILURES;
 
    pgaio_io_register_callbacks(ioh,
                                RelationUsesLocalBuffers(rel) ?
                                PGAIO_HCB_LOCAL_BUFFER_READV :
                                PGAIO_HCB_SHARED_BUFFER_READV,
                                srb_flags);
 
    if (batchmode_enter)
        pgaio_enter_batchmode();
 
    smgrstartreadv(ioh, smgr, MAIN_FORKNUM, blkno,
                   (void *) pages, nblocks);
 
    if (call_smgrreleaseall)
        smgrreleaseall();
 
    if (batchmode_exit)
        pgaio_exit_batchmode();
 
    for (int i = 0; i < nblocks; i++)
        ReleaseBuffer(bufs[i]);
 
    if (wait_complete)
    {
        pgaio_wref_wait(&iow);
 
        if (ior.result.status != PGAIO_RS_OK)
            pgaio_result_report(ior.result,
                                &ior.target_data,
                                ior.result.status == PGAIO_RS_ERROR ?
                                ERROR : WARNING);
    }
 
    relation_close(rel, NoLock);
 
    PG_RETURN_VOID();
}
 
/* helper for invalidate_rel_block() and evict_rel() */
static void
invalidate_one_block(Relation rel, ForkNumber forknum, BlockNumber blkno)
{
    PrefetchBufferResult pr;
    Buffer      buf;
 
    /*
     * This is a gross hack, but there's no other API exposed that allows to
     * get a buffer ID without actually reading the block in.
     */
    pr = PrefetchBuffer(rel, forknum, blkno);
    buf = pr.recent_buffer;
 
    if (BufferIsValid(buf))
    {
        /* if the buffer contents aren't valid, this'll return false */
        if (ReadRecentBuffer(rel->rd_locator, forknum, blkno, buf))
        {
            BufferDesc *buf_hdr = BufferIsLocal(buf) ?
                GetLocalBufferDescriptor(-buf - 1)
                : GetBufferDescriptor(buf - 1);
            bool        flushed;
 
            LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
 
            if (pg_atomic_read_u64(&buf_hdr->state) & BM_DIRTY)
            {
                if (BufferIsLocal(buf))
                    FlushLocalBuffer(buf_hdr, NULL);
                else
                    FlushOneBuffer(buf);
            }
            UnlockReleaseBuffer(buf);
 
            if (BufferIsLocal(buf))
                InvalidateLocalBuffer(GetLocalBufferDescriptor(-buf - 1), true);
            else if (!EvictUnpinnedBuffer(buf, &flushed))
                elog(ERROR, "couldn't evict");
        }
    }
 
}
 
PG_FUNCTION_INFO_V1(invalidate_rel_block);
Datum
invalidate_rel_block(PG_FUNCTION_ARGS)
{
    Oid         relid = PG_GETARG_OID(0);
    BlockNumber blkno = PG_GETARG_UINT32(1);
    Relation    rel;
 
    rel = relation_open(relid, AccessExclusiveLock);
 
    invalidate_one_block(rel, MAIN_FORKNUM, blkno);
 
    relation_close(rel, AccessExclusiveLock);
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(evict_rel);
Datum
evict_rel(PG_FUNCTION_ARGS)
{
    Oid         relid = PG_GETARG_OID(0);
    Relation    rel;
 
    rel = relation_open(relid, AccessExclusiveLock);
 
    /*
     * EvictRelUnpinnedBuffers() doesn't support temp tables, so for temp
     * tables we have to do it the expensive way and evict every possible
     * buffer.
     */
    if (RelationUsesLocalBuffers(rel))
    {
        SMgrRelation smgr = RelationGetSmgr(rel);
 
        for (int forknum = MAIN_FORKNUM; forknum <= MAX_FORKNUM; forknum++)
        {
            BlockNumber nblocks;
 
            if (!smgrexists(smgr, forknum))
                continue;
 
            nblocks = smgrnblocks(smgr, forknum);
 
            for (int blkno = 0; blkno < nblocks; blkno++)
            {
                invalidate_one_block(rel, forknum, blkno);
            }
        }
    }
    else
    {
        int32       buffers_evicted,
                    buffers_flushed,
                    buffers_skipped;
 
        EvictRelUnpinnedBuffers(rel, &buffers_evicted, &buffers_flushed,
                                &buffers_skipped);
    }
 
    relation_close(rel, AccessExclusiveLock);
 
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(buffer_create_toy);
Datum
buffer_create_toy(PG_FUNCTION_ARGS)
{
    Oid         relid = PG_GETARG_OID(0);
    BlockNumber blkno = PG_GETARG_UINT32(1);
    Relation    rel;
    Buffer      buf;
 
    rel = relation_open(relid, AccessExclusiveLock);
 
    buf = create_toy_buffer(rel, blkno);
    ReleaseBuffer(buf);
 
    relation_close(rel, NoLock);
 
    PG_RETURN_INT32(buf);
}
 
PG_FUNCTION_INFO_V1(buffer_call_start_io);
Datum
buffer_call_start_io(PG_FUNCTION_ARGS)
{
    Buffer      buf = PG_GETARG_INT32(0);
    bool        for_input = PG_GETARG_BOOL(1);
    bool        wait = PG_GETARG_BOOL(2);
    StartBufferIOResult result;
    bool        can_start;
 
    if (BufferIsLocal(buf))
        result = StartLocalBufferIO(GetLocalBufferDescriptor(-buf - 1),
                                    for_input, wait, NULL);
    else
        result = StartSharedBufferIO(GetBufferDescriptor(buf - 1),
                                     for_input, wait, NULL);
 
    can_start = result == BUFFER_IO_READY_FOR_IO;
 
    /*
     * For tests we don't want the resowner release preventing us from
     * orchestrating odd scenarios.
     */
    if (can_start && !BufferIsLocal(buf))
        ResourceOwnerForgetBufferIO(CurrentResourceOwner,
                                    buf);
 
    ereport(LOG,
            errmsg("buffer %d after StartBufferIO: %s",
                   buf, DebugPrintBufferRefcount(buf)),
            errhidestmt(true), errhidecontext(true));
 
    PG_RETURN_BOOL(can_start);
}
 
PG_FUNCTION_INFO_V1(buffer_call_terminate_io);
Datum
buffer_call_terminate_io(PG_FUNCTION_ARGS)
{
    Buffer      buf = PG_GETARG_INT32(0);
    bool        for_input = PG_GETARG_BOOL(1);
    bool        succeed = PG_GETARG_BOOL(2);
    bool        io_error = PG_GETARG_BOOL(3);
    bool        release_aio = PG_GETARG_BOOL(4);
    bool        clear_dirty = false;
    uint64      set_flag_bits = 0;
 
    if (io_error)
        set_flag_bits |= BM_IO_ERROR;
 
    if (for_input)
    {
        clear_dirty = false;
 
        if (succeed)
            set_flag_bits |= BM_VALID;
    }
    else
    {
        if (succeed)
            clear_dirty = true;
    }
 
    ereport(LOG,
            errmsg("buffer %d before Terminate[Local]BufferIO: %s",
                   buf, DebugPrintBufferRefcount(buf)),
            errhidestmt(true), errhidecontext(true));
 
    if (BufferIsLocal(buf))
        TerminateLocalBufferIO(GetLocalBufferDescriptor(-buf - 1),
                               clear_dirty, set_flag_bits, release_aio);
    else
        TerminateBufferIO(GetBufferDescriptor(buf - 1),
                          clear_dirty, set_flag_bits, false, release_aio);
 
    ereport(LOG,
            errmsg("buffer %d after Terminate[Local]BufferIO: %s",
                   buf, DebugPrintBufferRefcount(buf)),
            errhidestmt(true), errhidecontext(true));
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(read_buffers);
/*
 * Infrastructure to test StartReadBuffers()
 */
Datum
read_buffers(PG_FUNCTION_ARGS)
{
    Oid         relid = PG_GETARG_OID(0);
    BlockNumber startblock = PG_GETARG_UINT32(1);
    int32       nblocks = PG_GETARG_INT32(2);
    ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
    Relation    rel;
    SMgrRelation smgr;
    int         nblocks_done = 0;
    int         nblocks_disp = 0;
    int         nios = 0;
    ReadBuffersOperation *operations;
    Buffer     *buffers;
    Datum      *buffers_datum;
    bool       *io_reqds;
    int        *nblocks_per_io;
 
    Assert(nblocks > 0);
 
    InitMaterializedSRF(fcinfo, 0);
 
    /* at worst each block gets its own IO */
    operations = palloc0(sizeof(ReadBuffersOperation) * nblocks);
    buffers = palloc0(sizeof(Buffer) * nblocks);
    buffers_datum = palloc0(sizeof(Datum) * nblocks);
    io_reqds = palloc0(sizeof(bool) * nblocks);
    nblocks_per_io = palloc0(sizeof(int) * nblocks);
 
    rel = relation_open(relid, AccessShareLock);
    smgr = RelationGetSmgr(rel);
 
    /*
     * Do StartReadBuffers() until IO for all the required blocks has been
     * started (if required).
     */
    while (nblocks_done < nblocks)
    {
        ReadBuffersOperation *operation = &operations[nios];
        int         nblocks_this_io =
            Min(nblocks - nblocks_done, io_combine_limit);
 
        operation->rel = rel;
        operation->smgr = smgr;
        operation->persistence = rel->rd_rel->relpersistence;
        operation->strategy = NULL;
        operation->forknum = MAIN_FORKNUM;
 
        io_reqds[nios] = StartReadBuffers(operation,
                                          &buffers[nblocks_done],
                                          startblock + nblocks_done,
                                          &nblocks_this_io,
                                          0);
        nblocks_per_io[nios] = nblocks_this_io;
        nios++;
        nblocks_done += nblocks_this_io;
    }
 
    /*
     * Now wait for all operations that required IO. This is done at the end,
     * as otherwise waiting for IO in progress in other backends could
     * influence the result for subsequent buffers / blocks.
     */
    for (int nio = 0; nio < nios; nio++)
    {
        ReadBuffersOperation *operation = &operations[nio];
 
        if (io_reqds[nio])
            WaitReadBuffers(operation);
    }
 
    /*
     * Convert what has been done into SQL SRF return value.
     */
    for (int nio = 0; nio < nios; nio++)
    {
        ReadBuffersOperation *operation = &operations[nio];
        int         nblocks_this_io = nblocks_per_io[nio];
        Datum       values[6] = {0};
        bool        nulls[6] = {0};
        ArrayType  *buffers_arr;
 
        /* convert buffer array to datum array */
        for (int i = 0; i < nblocks_this_io; i++)
        {
            Buffer      buf = buffers[nblocks_disp + i];
 
            Assert(BufferGetBlockNumber(buf) == startblock + nblocks_disp + i);
 
            buffers_datum[nblocks_disp + i] = Int32GetDatum(buf);
        }
 
        buffers_arr = construct_array_builtin(&buffers_datum[nblocks_disp],
                                              nblocks_this_io,
                                              INT4OID);
 
        /* blockoff */
        values[0] = Int32GetDatum(nblocks_disp);
        nulls[0] = false;
 
        /* blocknum */
        values[1] = UInt32GetDatum(startblock + nblocks_disp);
        nulls[1] = false;
 
        /* io_reqd */
        values[2] = BoolGetDatum(io_reqds[nio]);
        nulls[2] = false;
 
        /* foreign IO - only valid when IO was required */
        values[3] = BoolGetDatum(io_reqds[nio] ? operation->foreign_io : false);
        nulls[3] = false;
 
        /* nblocks */
        values[4] = Int32GetDatum(nblocks_this_io);
        nulls[4] = false;
 
        /* array of buffers */
        values[5] = PointerGetDatum(buffers_arr);
        nulls[5] = false;
 
        tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
 
        nblocks_disp += nblocks_this_io;
    }
 
    /* release pins on all the buffers */
    for (int i = 0; i < nblocks_done; i++)
        ReleaseBuffer(buffers[i]);
 
    /*
     * Free explicitly, to have a chance to detect potential issues with too
     * long lived references to the operation.
     */
    pfree(operations);
    pfree(buffers);
    pfree(buffers_datum);
    pfree(io_reqds);
    pfree(nblocks_per_io);
 
    relation_close(rel, NoLock);
 
    return (Datum) 0;
}
 
 
static BlockNumber
read_stream_for_blocks_cb(ReadStream *stream,
                          void *callback_private_data,
                          void *per_buffer_data)
{
    BlocksReadStreamData *stream_data = callback_private_data;
 
    if (stream_data->curblock >= stream_data->nblocks)
        return InvalidBlockNumber;
    return stream_data->blocks[stream_data->curblock++];
}
 
PG_FUNCTION_INFO_V1(read_stream_for_blocks);
Datum
read_stream_for_blocks(PG_FUNCTION_ARGS)
{
    Oid         relid = PG_GETARG_OID(0);
    ArrayType  *blocksarray = PG_GETARG_ARRAYTYPE_P(1);
    ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
    Relation    rel;
    BlocksReadStreamData stream_data;
    ReadStream *stream;
 
    InitMaterializedSRF(fcinfo, 0);
 
    /*
     * We expect the input to be an N-element int4 array; verify that. We
     * don't need to use deconstruct_array() since the array data is just
     * going to look like a C array of N int4 values.
     */
    if (ARR_NDIM(blocksarray) != 1 ||
        ARR_HASNULL(blocksarray) ||
        ARR_ELEMTYPE(blocksarray) != INT4OID)
        elog(ERROR, "expected 1 dimensional int4 array");
 
    stream_data.curblock = 0;
    stream_data.nblocks = ARR_DIMS(blocksarray)[0];
    stream_data.blocks = (uint32 *) ARR_DATA_PTR(blocksarray);
 
    rel = relation_open(relid, AccessShareLock);
 
    stream = read_stream_begin_relation(READ_STREAM_FULL,
                                        NULL,
                                        rel,
                                        MAIN_FORKNUM,
                                        read_stream_for_blocks_cb,
                                        &stream_data,
                                        0);
 
    for (int i = 0; i < stream_data.nblocks; i++)
    {
        Buffer      buf = read_stream_next_buffer(stream, NULL);
        Datum       values[3] = {0};
        bool        nulls[3] = {0};
 
        if (!BufferIsValid(buf))
            elog(ERROR, "read_stream_next_buffer() call %d is unexpectedly invalid", i);
 
        values[0] = Int32GetDatum(i);
        values[1] = UInt32GetDatum(stream_data.blocks[i]);
        values[2] = UInt32GetDatum(buf);
 
        tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, values, nulls);
 
        ReleaseBuffer(buf);
    }
 
    if (read_stream_next_buffer(stream, NULL) != InvalidBuffer)
        elog(ERROR, "read_stream_next_buffer() call %d is unexpectedly valid",
             stream_data.nblocks);
 
    read_stream_end(stream);
 
    relation_close(rel, NoLock);
 
    return (Datum) 0;
}
 
 
PG_FUNCTION_INFO_V1(handle_get);
Datum
handle_get(PG_FUNCTION_ARGS)
{
    last_handle = pgaio_io_acquire(CurrentResourceOwner, NULL);
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(handle_release_last);
Datum
handle_release_last(PG_FUNCTION_ARGS)
{
    if (!last_handle)
        elog(ERROR, "no handle");
 
    pgaio_io_release(last_handle);
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(handle_get_and_error);
Datum
handle_get_and_error(PG_FUNCTION_ARGS)
{
    pgaio_io_acquire(CurrentResourceOwner, NULL);
 
    elog(ERROR, "as you command");
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(handle_get_twice);
Datum
handle_get_twice(PG_FUNCTION_ARGS)
{
    pgaio_io_acquire(CurrentResourceOwner, NULL);
    pgaio_io_acquire(CurrentResourceOwner, NULL);
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(handle_get_release);
Datum
handle_get_release(PG_FUNCTION_ARGS)
{
    PgAioHandle *handle;
 
    handle = pgaio_io_acquire(CurrentResourceOwner, NULL);
    pgaio_io_release(handle);
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(batch_start);
Datum
batch_start(PG_FUNCTION_ARGS)
{
    pgaio_enter_batchmode();
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(batch_end);
Datum
batch_end(PG_FUNCTION_ARGS)
{
    pgaio_exit_batchmode();
    PG_RETURN_VOID();
}
 
#ifdef USE_INJECTION_POINTS
extern PGDLLEXPORT void inj_io_completion_hook(const char *name,
                                               const void *private_data,
                                               void *arg);
extern PGDLLEXPORT void inj_io_reopen(const char *name,
                                      const void *private_data,
                                      void *arg);
 
static bool
inj_io_short_read_matches(PgAioHandle *ioh)
{
    PGPROC     *io_proc;
    int32       io_pid;
    int32       inj_pid;
    PgAioTargetData *td;
 
    if (!inj_io_error_state->enabled_short_read)
        return false;
 
    if (!inj_io_error_state->short_read_result_set)
        return false;
 
    io_proc = GetPGProcByNumber(pgaio_io_get_owner(ioh));
    io_pid = io_proc->pid;
    inj_pid = inj_io_error_state->short_read_pid;
 
    if (inj_pid != InvalidPid && inj_pid != io_pid)
        return false;
 
    td = pgaio_io_get_target_data(ioh);
 
    if (inj_io_error_state->short_read_relfilenode != InvalidOid &&
        td->smgr.rlocator.relNumber != inj_io_error_state->short_read_relfilenode)
        return false;
 
    /*
     * Only shorten reads that are actually longer than the target size,
     * otherwise we can trigger over-reads.
     */
    if (inj_io_error_state->short_read_result >= ioh->result)
        return false;
 
    return true;
}
 
static bool
inj_io_completion_wait_matches(PgAioHandle *ioh)
{
    PGPROC     *io_proc;
    int32       io_pid;
    PgAioTargetData *td;
    int32       inj_pid;
    BlockNumber io_blockno;
    BlockNumber inj_blockno;
    Oid         inj_relfilenode;
 
    if (!inj_io_error_state->enabled_completion_wait)
        return false;
 
    io_proc = GetPGProcByNumber(pgaio_io_get_owner(ioh));
    io_pid = io_proc->pid;
    inj_pid = inj_io_error_state->completion_wait_pid;
 
    if (inj_pid != InvalidPid && inj_pid != io_pid)
        return false;
 
    td = pgaio_io_get_target_data(ioh);
 
    inj_relfilenode = inj_io_error_state->completion_wait_relfilenode;
    if (inj_relfilenode != InvalidOid &&
        td->smgr.rlocator.relNumber != inj_relfilenode)
        return false;
 
    inj_blockno = inj_io_error_state->completion_wait_blockno;
    io_blockno = td->smgr.blockNum;
    if (inj_blockno != InvalidBlockNumber &&
        !(inj_blockno >= io_blockno && inj_blockno < (io_blockno + td->smgr.nblocks)))
        return false;
 
    return true;
}
 
static void
inj_io_completion_wait_hook(const char *name, const void *private_data, void *arg)
{
    PgAioHandle *ioh = (PgAioHandle *) arg;
 
    if (!inj_io_completion_wait_matches(ioh))
        return;
 
    ConditionVariablePrepareToSleep(&inj_io_error_state->cv);
 
    while (true)
    {
        if (!inj_io_completion_wait_matches(ioh))
            break;
 
        ConditionVariableSleep(&inj_io_error_state->cv,
                               inj_io_error_state->completion_wait_event);
    }
 
    ConditionVariableCancelSleep();
}
 
static void
inj_io_short_read_hook(const char *name, const void *private_data, void *arg)
{
    PgAioHandle *ioh = (PgAioHandle *) arg;
 
    ereport(LOG,
            errmsg("short read injection point called, is enabled: %d",
                   inj_io_error_state->enabled_short_read),
            errhidestmt(true), errhidecontext(true));
 
    if (inj_io_short_read_matches(ioh))
    {
        struct iovec *iov = &pgaio_ctl->iovecs[ioh->iovec_off];
        int32       old_result = ioh->result;
        int32       new_result = inj_io_error_state->short_read_result;
        int32       processed = 0;
 
        ereport(LOG,
                errmsg("short read inject point, changing result from %d to %d",
                       old_result, new_result),
                errhidestmt(true), errhidecontext(true));
 
        /*
         * The underlying IO actually completed OK, and thus the "invalid"
         * portion of the IOV actually contains valid data. That can hide a
         * lot of problems, e.g. if we were to wrongly mark a buffer, that
         * wasn't read according to the shortened-read, IO as valid, the
         * contents would look valid and we might miss a bug.
         *
         * To avoid that, iterate through the IOV and zero out the "failed"
         * portion of the IO.
         */
        for (int i = 0; i < ioh->op_data.read.iov_length; i++)
        {
            if (processed + iov[i].iov_len <= new_result)
                processed += iov[i].iov_len;
            else if (processed <= new_result)
            {
                uint32      ok_part = new_result - processed;
 
                memset((char *) iov[i].iov_base + ok_part, 0, iov[i].iov_len - ok_part);
                processed += iov[i].iov_len;
            }
            else
            {
                memset((char *) iov[i].iov_base, 0, iov[i].iov_len);
            }
        }
 
        ioh->result = new_result;
    }
}
 
void
inj_io_completion_hook(const char *name, const void *private_data, void *arg)
{
    inj_io_completion_wait_hook(name, private_data, arg);
    inj_io_short_read_hook(name, private_data, arg);
}
 
void
inj_io_reopen(const char *name, const void *private_data, void *arg)
{
    ereport(LOG,
            errmsg("reopen injection point called, is enabled: %d",
                   inj_io_error_state->enabled_reopen),
            errhidestmt(true), errhidecontext(true));
 
    if (inj_io_error_state->enabled_reopen)
        elog(ERROR, "injection point triggering failure to reopen ");
}
#endif
 
PG_FUNCTION_INFO_V1(inj_io_completion_wait);
Datum
inj_io_completion_wait(PG_FUNCTION_ARGS)
{
#ifdef USE_INJECTION_POINTS
    inj_io_error_state->enabled_completion_wait = true;
    inj_io_error_state->completion_wait_pid =
        PG_ARGISNULL(0) ? InvalidPid : PG_GETARG_INT32(0);
    inj_io_error_state->completion_wait_relfilenode =
        PG_ARGISNULL(1) ? InvalidOid : PG_GETARG_OID(1);
    inj_io_error_state->completion_wait_blockno =
        PG_ARGISNULL(2) ? InvalidBlockNumber : PG_GETARG_UINT32(2);
#else
    elog(ERROR, "injection points not supported");
#endif
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(inj_io_completion_continue);
Datum
inj_io_completion_continue(PG_FUNCTION_ARGS)
{
#ifdef USE_INJECTION_POINTS
    inj_io_error_state->enabled_completion_wait = false;
    inj_io_error_state->completion_wait_pid = InvalidPid;
    inj_io_error_state->completion_wait_relfilenode = InvalidOid;
    inj_io_error_state->completion_wait_blockno = InvalidBlockNumber;
    ConditionVariableBroadcast(&inj_io_error_state->cv);
#else
    elog(ERROR, "injection points not supported");
#endif
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(inj_io_short_read_attach);
Datum
inj_io_short_read_attach(PG_FUNCTION_ARGS)
{
#ifdef USE_INJECTION_POINTS
    inj_io_error_state->enabled_short_read = true;
    inj_io_error_state->short_read_result_set = !PG_ARGISNULL(0);
    if (inj_io_error_state->short_read_result_set)
        inj_io_error_state->short_read_result = PG_GETARG_INT32(0);
    inj_io_error_state->short_read_pid =
        PG_ARGISNULL(1) ? InvalidPid : PG_GETARG_INT32(1);
    inj_io_error_state->short_read_relfilenode =
        PG_ARGISNULL(2) ? InvalidOid : PG_GETARG_OID(2);
#else
    elog(ERROR, "injection points not supported");
#endif
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(inj_io_short_read_detach);
Datum
inj_io_short_read_detach(PG_FUNCTION_ARGS)
{
#ifdef USE_INJECTION_POINTS
    inj_io_error_state->enabled_short_read = false;
#else
    elog(ERROR, "injection points not supported");
#endif
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(inj_io_reopen_attach);
Datum
inj_io_reopen_attach(PG_FUNCTION_ARGS)
{
#ifdef USE_INJECTION_POINTS
    inj_io_error_state->enabled_reopen = true;
#else
    elog(ERROR, "injection points not supported");
#endif
 
    PG_RETURN_VOID();
}
 
PG_FUNCTION_INFO_V1(inj_io_reopen_detach);
Datum
inj_io_reopen_detach(PG_FUNCTION_ARGS)
{
#ifdef USE_INJECTION_POINTS
    inj_io_error_state->enabled_reopen = false;
#else
    elog(ERROR, "injection points not supported");
#endif
    PG_RETURN_VOID();
}