forked from open-gpdb/yezzey
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyezzey.c
1210 lines (989 loc) · 38.3 KB
/
yezzey.c
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
#include "postgres.h"
#include "catalog/dependency.h"
#include "catalog/pg_extension.h"
#include "commands/extension.h"
#include "access/xlog.h"
#include "catalog/storage_xlog.h"
#include "common/relpath.h"
#include "executor/spi.h"
#include "funcapi.h"
#include "pgstat.h"
#include "utils/builtins.h"
#include "access/aocssegfiles.h"
#include "access/aosegfiles.h"
#include "storage/lmgr.h"
#include "utils/tqual.h"
#include "utils/fmgroids.h"
#include "catalog/catalog.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_tablespace.h"
#include "catalog/storage.h"
#include "access/xact.h"
#include "catalog/indexing.h"
#include "utils/guc.h"
#include "fmgr.h"
// #include "smgr_s3_frontend.h"
// For GpIdentity
#include "c.h"
#include "catalog/pg_namespace.h"
#include "cdb/cdbvars.h"
#include "utils/catcache.h"
#include "utils/syscache.h"
#include "catalog/heap.h"
#include "catalog/pg_namespace.h"
#include "nodes/primnodes.h"
#include "yezzey.h"
#include "storage.h"
#include "util.h"
#include "virtual_index.h"
#include "catalog/oid_dispatch.h"
#include "offload_policy.h"
#include "offload.h"
#define GET_STR(textp) \
DatumGetCString(DirectFunctionCall1(textout, PointerGetDatum(textp)))
/* STORAGE */
char *storage_prefix = NULL;
char *storage_bucket = NULL;
char *storage_config = NULL;
char *storage_host = NULL;
/* GPG */
char *gpg_engine_path = NULL;
char *gpg_key_id = NULL;
bool use_gpg_crypto = false;
/* WAL-G */
char *walg_bin_path = NULL;
char *walg_config_path = NULL;
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(yezzey_offload_relation);
PG_FUNCTION_INFO_V1(yezzey_load_relation);
PG_FUNCTION_INFO_V1(yezzey_load_relation_seg);
PG_FUNCTION_INFO_V1(yezzey_force_segment_offload);
PG_FUNCTION_INFO_V1(yezzey_offload_relation_status_internal);
PG_FUNCTION_INFO_V1(yezzey_offload_relation_status_per_filesegment);
PG_FUNCTION_INFO_V1(
yezzey_relation_describe_external_storage_structure_internal);
PG_FUNCTION_INFO_V1(yezzey_define_relation_offload_policy_internal);
PG_FUNCTION_INFO_V1(yezzey_define_relation_offload_policy_internal_seg);
PG_FUNCTION_INFO_V1(yezzey_offload_relation_to_external_path);
PG_FUNCTION_INFO_V1(yezzey_show_relation_external_path);
PG_FUNCTION_INFO_V1(yezzey_init_metadata_seg);
PG_FUNCTION_INFO_V1(yezzey_init_metadata);
/*
* Perform XLogInsert of a XLOG_SMGR_CREATE record to WAL.
*/
void yezzey_log_smgroffload(RelFileNode *rnode);
Datum yezzey_init_metadata(PG_FUNCTION_ARGS) {
(void)YezzeyOffloadPolicyRelation();
PG_RETURN_VOID();
}
Datum yezzey_init_metadata_seg(PG_FUNCTION_ARGS) {
return yezzey_init_metadata(fcinfo);
}
int yezzey_offload_relation_internal(Oid reloid, bool remove_locally,
const char *external_storage_path);
static void ATExecSetTableSpace(Relation aorel, Oid reloid,
Oid desttablespace_oid);
/*
* yezzey_define_relation_offload_policy_internal:
* do all the work with initial relation offloading:
* 1) Open relation in exclusive mode
* 2) Create yezzey aux index relation for offload relation
* 3)
* 3.1) Do main offloading job on segments
* 3.2) On dispather, clear pre-assigned oids.
* 4) change relation tablespace to virtual tablespace
* 5) record entry in offload metadata to track and process
* in bgworker routines
* 6) add the dependency in pg_depend
*/
Datum yezzey_define_relation_offload_policy_internal(PG_FUNCTION_ARGS) {
Oid reloid;
Oid yezzey_ext_oid;
ObjectAddress relationAddr, extensionAddr;
int rc;
Relation aorel;
Oid yandexoid;
reloid = PG_GETARG_OID(0);
relationAddr.classId = RelationRelationId;
relationAddr.objectId = reloid;
relationAddr.objectSubId = 0;
yezzey_ext_oid = get_extension_oid("yezzey", false);
if (!yezzey_ext_oid) {
elog(ERROR, "failed to get yezzey extnsion oid");
}
extensionAddr.classId = ExtensionRelationId;
extensionAddr.objectId = yezzey_ext_oid;
extensionAddr.objectSubId = 0;
elog(yezzey_log_level, "recording dependency on yezzey for relation %d",
reloid);
/*
* 2) Create auxularry yezzey table to track external storage
* chunks
*/
aorel = relation_open(reloid, AccessExclusiveLock);
RelationOpenSmgr(aorel);
(void)YezzeyCreateAuxIndex(aorel);
/*
* @brief do main offload job on segments
*
*/
if (Gp_role != GP_ROLE_DISPATCH) {
/* */
if ((rc = yezzey_offload_relation_internal_rel(aorel, true, NULL)) < 0) {
elog(ERROR,
"failed to offload relation (oid=%d) to external storage: return "
"code "
"%d",
reloid, rc);
}
} else {
/* clear all pre-assigned oids
* for auxularry yezzey table relation
*
* We need to do it, else we will get error
* about assigned, but not dispatched oids
*/
GetAssignedOidsForDispatch();
}
/* change relation tablespace */
(void)ATExecSetTableSpace(aorel, reloid, YEZZEYTABLESPACE_OID);
/* record entry in offload metadata */
(void)YezzeyOffloadPolicyInsert(reloid, 1 /* always external */);
/*
* OK, add the dependency.
*/
// recordDependencyOn(&relationAddr, &extensionAddr, DEPENDENCY_EXTENSION);
// recordDependencyOn(&extensionAddr, &relationAddr, DEPENDENCY_NORMAL);
recordDependencyOn(&relationAddr, &extensionAddr, DEPENDENCY_NORMAL);
// recordDependencyOn(&extensionAddr, &relationAddr, DEPENDENCY_INTERNAL);
relation_close(aorel, AccessExclusiveLock);
PG_RETURN_VOID();
}
/*
* yezzey_define_relation_offload_policy_internal_seg:
* Follow the query dispatcher logic
*/
Datum yezzey_define_relation_offload_policy_internal_seg(PG_FUNCTION_ARGS) {
return yezzey_define_relation_offload_policy_internal(fcinfo);
}
/*
* Execute ALTER TABLE SET TABLESPACE for cases where there is no tuple
* rewriting to be done, so we just want to copy the data as fast as possible.
*/
static void ATExecSetTableSpace(Relation aorel, Oid reloid,
Oid desttablespace_oid) {
Relation pg_class;
HeapTuple tuple;
Form_pg_class rd_rel;
/*
* Need lock here in case we are recursing to toast table or index
*/
/*
* We cannot support moving mapped relations into different tablespaces.
* (In particular this eliminates all shared catalogs.)
*/
if (RelationIsMapped(aorel))
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot move system relation \"%s\"",
RelationGetRelationName(aorel))));
/*
* Don't allow moving temp tables of other backends ... their local buffer
* manager is not going to cope.
*/
if (RELATION_IS_OTHER_TEMP(aorel))
ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot move temporary tables of other sessions")));
/* Fetch the list of indexes on toast relation if necessary */
Assert(!OidIsValid(aorel->rd_rel->reltoastrelid));
// /* Get the bitmap sub objects */
// if (RelationIsBitmapIndex(rel))
// GetBitmapIndexAuxOids(rel, &relbmrelid, &relbmidxid);
/* Get a modifiable copy of the relation's pg_class row */
pg_class = heap_open(RelationRelationId, RowExclusiveLock);
tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(reloid));
if (!HeapTupleIsValid(tuple))
elog(ERROR, "cache lookup failed for relation %u", reloid);
rd_rel = (Form_pg_class)GETSTRUCT(tuple);
/*
* Since we copy the file directly without looking at the shared buffers,
* we'd better first flush out any pages of the source relation that are
* in shared buffers. We assume no new changes will be made while we are
* holding exclusive lock on the rel.
*/
FlushRelationBuffers(aorel);
/*
* Relfilenodes are not unique in databases across tablespaces, so we need
* to allocate a new one in the new tablespace.
*/
/* Open old and new relation */
/*
* Create and copy all forks of the relation, and schedule unlinking of
* old physical files.
*
* NOTE: any conflict in relfilenode value will be caught in
* RelationCreateStorage().
*/
/* data already copied */
/*
* Append-only tables now include init forks for unlogged tables, so we copy
* over all forks. AO tables, so far, do not have visimap or fsm forks.
*/
/* drop old relation, and close new one */
RelationDropStorage(aorel);
/* update the pg_class row */
if (desttablespace_oid != YEZZEYTABLESPACE_OID) {
rd_rel->relfilenode = GetNewRelFileNode(desttablespace_oid, NULL,
aorel->rd_rel->relpersistence);
}
rd_rel->reltablespace = desttablespace_oid;
simple_heap_update(pg_class, &tuple->t_self, tuple);
CatalogUpdateIndexes(pg_class, tuple);
InvokeObjectPostAlterHook(RelationRelationId, RelationGetRelid(aorel), 0);
heap_freetuple(tuple);
heap_close(pg_class, RowExclusiveLock);
// yezzey: do we need this?
// /* MPP-6929: metadata tracking */
// if ((Gp_role == GP_ROLE_DISPATCH) && MetaTrackValidKindNsp(rel->rd_rel))
// MetaTrackUpdObject(RelationRelationId,
// RelationGetRelid(rel),
// GetUserId(),
// "ALTER", "SET TABLESPACE");
/* Make sure the reltablespace change is visible */
CommandCounterIncrement();
/* yezzey: do we need to move indexes? */
// /*
// * MPP-7996 - bitmap index subobjects w/Alter Table Set tablespace
// */
// if (OidIsValid(relbmrelid))
// {
// Assert(!relaosegrelid);
// ATExecSetTableSpace(relbmrelid, newTableSpace, lockmode);
// }
// if (OidIsValid(relbmidxid))
// ATExecSetTableSpace(relbmidxid, newTableSpace, lockmode);
/* Clean up */
}
/*
* yezzey_load_relation_internal:
* TBD: doc the logic
*/
int yezzey_load_relation_internal(Oid reloid, const char *dest_path) {
Relation aorel;
int i;
int segno;
int pseudosegno;
int nvp;
int inat;
int total_segfiles;
FileSegInfo **segfile_array;
AOCSFileSegInfo **segfile_array_cs;
Snapshot appendOnlyMetaDataSnapshot;
ObjectAddress object;
Oid origrelfilenode;
int rc;
/* XXX: maybe fix lock here to take more granular lock
*/
aorel = relation_open(reloid, AccessExclusiveLock);
nvp = aorel->rd_att->natts;
origrelfilenode = aorel->rd_rel->relfilenode;
/*
* Relation segments named base/DBOID/aorel->rd_node.*
*/
elog(yezzey_log_level, "loading relnode %d", aorel->rd_node.relNode);
/* for now, we locked relation */
/* GetAllFileSegInfo_pg_aoseg_rel */
/* acquire snapshot for aoseg table lookup */
appendOnlyMetaDataSnapshot = SnapshotSelf;
/*sanity check */
if (aorel->rd_node.spcNode != YEZZEYTABLESPACE_OID) {
/* shoulde never happen*/
elog(ERROR, "attempted to load non-offloaded relation");
}
/* Perform actual deletion of yezzey virtual index and metadata changes */
(void) ATExecSetTableSpace(aorel, reloid, DEFAULTTABLESPACE_OID);
/* Get information about all the file segments we need to scan */
if (aorel->rd_rel->relstorage == 'a') {
/* ao rows relation */
segfile_array =
GetAllFileSegInfo(aorel, appendOnlyMetaDataSnapshot, &total_segfiles);
for (i = 0; i < total_segfiles; i++) {
segno = segfile_array[i]->segno;
elog(yezzey_log_level, "loading segment no %d", segno);
rc = loadRelationSegment(aorel, origrelfilenode, segno, dest_path);
if (rc < 0) {
elog(ERROR, "failed to offload segment number %d", segno);
}
/* segment if loaded */
}
if (segfile_array) {
FreeAllSegFileInfo(segfile_array, total_segfiles);
pfree(segfile_array);
}
} else if (aorel->rd_rel->relstorage == 'c') {
/* ao columns, relstorage == 'c' */
segfile_array_cs = GetAllAOCSFileSegInfo(aorel, appendOnlyMetaDataSnapshot,
&total_segfiles);
for (inat = 0; inat < nvp; ++inat) {
for (i = 0; i < total_segfiles; i++) {
segno = segfile_array_cs[i]->segno;
pseudosegno = (inat * AOTupleId_MultiplierSegmentFileNum) + segno;
elog(yezzey_log_level, "loading cs segment no %d pseudosegno %d", segno,
pseudosegno);
rc =
loadRelationSegment(aorel, origrelfilenode, pseudosegno, dest_path);
if (rc < 0) {
elog(ERROR, "failed to load cs segment number %d pseudosegno %d",
segno, pseudosegno);
}
/* segment if loaded */
}
}
if (segfile_array_cs) {
FreeAllAOCSSegFileInfo(segfile_array_cs, total_segfiles);
pfree(segfile_array_cs);
}
} else {
elog(ERROR, "not an AO/AOCS relation, no action performed");
}
/*
Do not drop, just empty
object.classId = RelationRelationId;
object.objectId = YezzeyFindAuxIndex(reloid);
object.objectSubId = 0;
performDeletion(&object, DROP_CASCADE, PERFORM_DELETION_INTERNAL);
*/
/* empty all track info */
(void) emptyYezzeyIndex(YezzeyFindAuxIndex(reloid));
/* cleanup */
relation_close(aorel, AccessExclusiveLock);
return 0;
}
Datum yezzey_load_relation(PG_FUNCTION_ARGS) {
/*
* Force table loading from external storage
* In order:
* 1) lock table in IN EXCLUSIVE MODE (is that needed?)
* 2) check pg_aoseg.pg_aoseg_XXX table for all segments
* 3) go and load each segment (XXX: enhancement: do loading in parallel)
*/
Oid reloid;
int rc;
char *dest_path = NULL;
reloid = PG_GETARG_OID(0);
dest_path = GET_STR(PG_GETARG_TEXT_P(1));
rc = yezzey_load_relation_internal(reloid, NULL);
if (rc) {
elog(ERROR, "failed to load relation (oid=%d) files to path %s", reloid,
dest_path);
}
PG_RETURN_VOID();
}
Datum yezzey_load_relation_seg(PG_FUNCTION_ARGS) {
return yezzey_load_relation(fcinfo);
}
Datum yezzey_offload_relation(PG_FUNCTION_ARGS) {
/*
* Force table offloading to external storage
* In order:
* 1) lock table in IN EXCLUSIVE MODE
* 2) check pg_aoseg.pg_aoseg_XXX table for all segments
* 3) go and offload each segment (XXX: enhancement: do offloading in
* parallel)
*/
Oid reloid;
bool remove_locally;
int rc;
reloid = PG_GETARG_OID(0);
remove_locally = PG_GETARG_BOOL(1);
rc = yezzey_offload_relation_internal(reloid, remove_locally, NULL);
PG_RETURN_VOID();
}
Datum yezzey_force_segment_offload(PG_FUNCTION_ARGS) { PG_RETURN_VOID(); }
Datum yezzey_offload_relation_to_external_path(PG_FUNCTION_ARGS) {
/*
* Force table offloading to external storage
* In order:
* 1) lock table in IN EXCLUSIVE MODE
* 2) check pg_aoseg.pg_aoseg_XXX table for all segments
* 3) go and offload each segment (XXX: enhancement: do offloading in
* parallel)
*/
Oid reloid;
bool remove_locally;
const char *external_path;
int rc;
reloid = PG_GETARG_OID(0);
remove_locally = PG_GETARG_BOOL(1);
external_path = GET_STR(PG_GETARG_TEXT_P(2));
rc = yezzey_offload_relation_internal(reloid, remove_locally, external_path);
PG_RETURN_VOID();
}
Datum yezzey_show_relation_external_path(PG_FUNCTION_ARGS) {
Oid reloid;
Relation aorel;
RelFileNode rnode;
int32 segno;
char *pgptr;
char *ptr;
HeapTuple tp;
char *nspname;
reloid = PG_GETARG_OID(0);
segno = PG_GETARG_OID(1);
aorel = relation_open(reloid, AccessShareLock);
rnode = aorel->rd_node;
tp = SearchSysCache1(NAMESPACEOID,
ObjectIdGetDatum(aorel->rd_rel->relnamespace));
if (HeapTupleIsValid(tp)) {
Form_pg_namespace nsptup = (Form_pg_namespace)GETSTRUCT(tp);
nspname = pstrdup(NameStr(nsptup->nspname));
ReleaseSysCache(tp);
} else {
elog(ERROR, "yezzey: failed to get namescape name of relation %s",
aorel->rd_rel->relname.data);
}
(void)getYezzeyExternalStoragePathByCoords(
nspname, aorel->rd_rel->relname.data, storage_host /*host*/,
storage_bucket /*bucket*/, storage_prefix /*prefix*/, rnode.dbNode,
rnode.relNode, segno, GpIdentity.segindex, &ptr);
pgptr = pstrdup(ptr);
free(ptr);
pfree(nspname);
relation_close(aorel, AccessShareLock);
PG_RETURN_TEXT_P(cstring_to_text(pgptr));
}
/**
* @brief yezzey_offload_relation_status_per_filesegment:
* List relation external storage usage per filesegment(block)
*/
Datum yezzey_offload_relation_status_per_filesegment(PG_FUNCTION_ARGS) {
Oid reloid;
Relation aorel;
int i;
int segno;
int total_segfiles;
int64 modcount;
int64 logicalEof;
int pseudosegno;
int inat;
int nvp;
FileSegInfo **segfile_array;
AOCSFileSegInfo **segfile_array_cs;
Snapshot appendOnlyMetaDataSnapshot;
FuncCallContext *funcctx;
MemoryContext oldcontext;
AttInMetadata *attinmeta;
int32 call_cntr;
reloid = PG_GETARG_OID(0);
segfile_array_cs = NULL;
segfile_array = NULL;
/* This mode guarantees that the holder is the only transaction accessing the
* table in any way. we need to be sure, thar no other transaction either
* reads or write to given relation because we are going to delete relation
* from local storage
*/
aorel = relation_open(reloid, AccessShareLock);
nvp = aorel->rd_att->natts;
/* GetAllFileSegInfo_pg_aoseg_rel */
/* acquire snapshot for aoseg table lookup */
appendOnlyMetaDataSnapshot = SnapshotSelf;
/* stuff done only on the first call of the function */
if (SRF_IS_FIRSTCALL()) {
/* create a function context for cross-call persistence */
funcctx = SRF_FIRSTCALL_INIT();
/*
* switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
if (aorel->rd_rel->relstorage == 'a') {
/* ao rows relation */
segfile_array =
GetAllFileSegInfo(aorel, appendOnlyMetaDataSnapshot, &total_segfiles);
} else if (aorel->rd_rel->relstorage == 'c') {
segfile_array_cs = GetAllAOCSFileSegInfo(
aorel, appendOnlyMetaDataSnapshot, &total_segfiles);
} else {
elog(ERROR, "wrong relation storage type, not AO/AOCS relation");
}
/*
* Build a tuple descriptor for our result type
* The number and type of attributes have to match the definition of the
* view yezzey_offload_relation_status_internal
*/
funcctx->tuple_desc =
CreateTemplateTupleDesc(NUM_USED_OFFLOAD_PER_SEGMENT_STATUS, false);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)1, "reloid", OIDOID,
-1 /* typmod */, 0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)2, "segindex", INT4OID,
-1 /* typmod */, 0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)3, "segfileindex",
INT4OID, -1 /* typmod */, 0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)4, "local_bytes",
INT8OID, -1 /* typmod */, 0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)5,
"local_commited_bytes", INT8OID, -1 /* typmod */,
0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)6, "external_bytes",
INT8OID, -1 /* typmod */, 0 /* attdim */);
funcctx->tuple_desc = BlessTupleDesc(funcctx->tuple_desc);
/*
* Generate attribute metadata needed later to produce tuples from raw
* C strings
*/
attinmeta = TupleDescGetAttInMetadata(funcctx->tuple_desc);
funcctx->attinmeta = attinmeta;
if (total_segfiles > 0) {
if (aorel->rd_rel->relstorage == 'a') {
funcctx->max_calls = total_segfiles;
funcctx->user_fctx = segfile_array;
} else if (aorel->rd_rel->relstorage == 'c') {
funcctx->max_calls = total_segfiles * nvp;
funcctx->user_fctx = segfile_array_cs;
} else {
Assert(false);
}
/* funcctx->user_fctx */
} else {
/* fast path when no results */
MemoryContextSwitchTo(oldcontext);
relation_close(aorel, AccessShareLock);
SRF_RETURN_DONE(funcctx);
}
MemoryContextSwitchTo(oldcontext);
}
/* stuff done on every call of the function */
funcctx = SRF_PERCALL_SETUP();
call_cntr = funcctx->call_cntr;
attinmeta = funcctx->attinmeta;
if (call_cntr == funcctx->max_calls) {
/* no pfree on segfile_array because context will be destroyed */
relation_close(aorel, AccessShareLock);
/* do when there is no more left */
SRF_RETURN_DONE(funcctx);
}
size_t local_bytes = 0;
size_t external_bytes = 0;
size_t local_commited_bytes = 0;
if (aorel->rd_rel->relstorage == 'a') {
/* ao rows relation */
segfile_array = funcctx->user_fctx;
i = call_cntr;
segno = segfile_array[i]->segno;
modcount = segfile_array[i]->modcount;
logicalEof = segfile_array[i]->eof;
elog(yezzey_log_level,
"stat segment no %d, modcount %ld with to logial eof %ld", segno,
modcount, logicalEof);
size_t curr_local_bytes = 0;
size_t curr_external_bytes = 0;
size_t curr_local_commited_bytes = 0;
if (statRelationSpaceUsage(aorel, segno, modcount, logicalEof,
&curr_local_bytes, &curr_local_commited_bytes,
&curr_external_bytes) < 0) {
elog(ERROR, "failed to stat segment %d usage", segno);
}
local_bytes = curr_local_bytes;
external_bytes = curr_external_bytes;
local_commited_bytes = curr_local_commited_bytes;
} else if (aorel->rd_rel->relstorage == 'c') {
segfile_array_cs = funcctx->user_fctx;
i = call_cntr / nvp;
inat = call_cntr % nvp;
segno = segfile_array_cs[i]->segno;
/* in AOCS case actual *segno* differs from segfile_array_cs[i]->segno
* whis is logical number of segment. On physical level, each logical
* segno (segfile_array_cs[i]->segno) is represented by
* AOTupleId_MultiplierSegmentFileNum in storage (1 file per attribute)
*/
pseudosegno = (inat * AOTupleId_MultiplierSegmentFileNum) + segno;
modcount = segfile_array_cs[i]->modcount;
logicalEof = segfile_array_cs[i]->vpinfo.entry[inat].eof;
elog(yezzey_log_level,
"stat segment no %d, modcount %ld with to logial eof %ld", segno,
modcount, logicalEof);
size_t curr_local_bytes = 0;
size_t curr_external_bytes = 0;
size_t curr_local_commited_bytes = 0;
if (statRelationSpaceUsage(aorel, pseudosegno, modcount, logicalEof,
&curr_local_bytes, &curr_local_commited_bytes,
&curr_external_bytes) < 0) {
elog(ERROR, "failed to stat segment block %d usage", pseudosegno);
}
local_bytes = curr_local_bytes;
external_bytes = curr_external_bytes;
local_commited_bytes = curr_local_commited_bytes;
} else {
elog(ERROR, "wrong relation storage type, not AO/AOCS");
}
/* segment if loaded */
Datum values[NUM_USED_OFFLOAD_PER_SEGMENT_STATUS];
bool nulls[NUM_USED_OFFLOAD_PER_SEGMENT_STATUS];
MemSet(nulls, 0, sizeof(nulls));
values[0] = ObjectIdGetDatum(reloid);
values[1] = Int32GetDatum(GpIdentity.segindex);
if (aorel->rd_rel->relstorage == 'a') {
values[2] = Int32GetDatum(segno);
} else if (aorel->rd_rel->relstorage == 'c') {
values[2] = Int32GetDatum(pseudosegno);
}
values[3] = Int64GetDatum(local_bytes);
values[4] = Int64GetDatum(local_commited_bytes);
values[5] = Int64GetDatum(external_bytes);
HeapTuple tuple = heap_form_tuple(funcctx->tuple_desc, values, nulls);
Datum result = HeapTupleGetDatum(tuple);
relation_close(aorel, AccessShareLock);
SRF_RETURN_NEXT(funcctx, result);
}
typedef struct yezzeyChunkMetaInfo {
Oid reloid; // "reloid" column
int32_t segindex; // "segindex" column
int32_t segfileindex; // "segfileindex" column
const char *external_storage_filepath; // "external_storage_filepath" column
int64_t local_bytes; // "local_bytes" column
int64_t local_commited_bytes; // "local_commited_bytes" column
int64_t external_bytes; // "external_bytes" column
} yezzeyChunkMetaInfo;
/*
* yezzey_relation_describe_external_storage_structure_internal:
* List yezzey external storage detailed infomation, includeing
* names of external storage relation chunks
*/
Datum yezzey_relation_describe_external_storage_structure_internal(
PG_FUNCTION_ARGS) {
Oid reloid;
Relation aorel;
int i;
int segno;
int pseudosegno;
int total_segfiles;
int nvp;
int inat;
int64 modcount;
int64 logicalEof;
FileSegInfo **segfile_array;
AOCSFileSegInfo **segfile_array_cs;
Snapshot appendOnlyMetaDataSnapshot;
FuncCallContext *funcctx;
MemoryContext oldcontext;
AttInMetadata *attinmeta;
int32 call_cntr;
yezzeyChunkMetaInfo *chunkInfo;
chunkInfo = palloc(sizeof(yezzeyChunkMetaInfo));
reloid = PG_GETARG_OID(0);
segfile_array_cs = NULL;
segfile_array = NULL;
/* This mode guarantees that the holder is the only transaction accessing the
* table in any way. we need to be sure, thar no other transaction either
* reads or write to given relation because we are going to delete relation
* from local storage
*/
aorel = relation_open(reloid, AccessShareLock);
nvp = aorel->rd_att->natts;
/* GetAllFileSegInfo_pg_aoseg_rel */
/* acquire snapshot for aoseg table lookup */
appendOnlyMetaDataSnapshot = SnapshotSelf;
/* stuff done only on the first call of the function */
if (SRF_IS_FIRSTCALL()) {
/* create a function context for cross-call persistence */
funcctx = SRF_FIRSTCALL_INIT();
/*
* switch to memory context appropriate for multiple function calls
*/
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
size_t total_row = 0;
size_t local_bytes = 0;
size_t external_bytes = 0;
size_t local_commited_bytes = 0;
if (aorel->rd_rel->relstorage == 'a') {
/* ao rows relation */
segfile_array =
GetAllFileSegInfo(aorel, appendOnlyMetaDataSnapshot, &total_segfiles);
for (i = 0; i < total_segfiles; ++i) {
segno = segfile_array[i]->segno;
modcount = segfile_array[i]->modcount;
logicalEof = segfile_array[i]->eof;
elog(yezzey_log_level,
"stat segment no %d, modcount %ld with to logial eof %ld", segno,
modcount, logicalEof);
size_t curr_local_bytes = 0;
size_t curr_external_bytes = 0;
size_t curr_local_commited_bytes = 0;
yezzeyChunkMeta *list;
size_t cnt_chunks;
if (statRelationSpaceUsagePerExternalChunk(
aorel, segno, modcount, logicalEof, &curr_local_bytes,
&curr_local_commited_bytes, &list, &cnt_chunks) < 0) {
elog(ERROR, "failed to stat segment %d usage", segno);
}
local_bytes = curr_local_bytes;
external_bytes = curr_external_bytes;
local_commited_bytes = curr_local_commited_bytes;
chunkInfo = repalloc(chunkInfo, sizeof(yezzeyChunkMetaInfo) *
(total_row + cnt_chunks));
for (size_t chunk_index = 0; chunk_index < cnt_chunks; ++chunk_index) {
chunkInfo[total_row + chunk_index].reloid = reloid;
chunkInfo[total_row + chunk_index].segindex = GpIdentity.segindex;
chunkInfo[total_row + chunk_index].segfileindex = i;
chunkInfo[total_row + chunk_index].external_storage_filepath =
list[chunk_index].chunkName;
chunkInfo[total_row + chunk_index].local_bytes = local_bytes;
chunkInfo[total_row + chunk_index].local_commited_bytes =
local_commited_bytes;
chunkInfo[total_row + chunk_index].external_bytes =
list[chunk_index].chunkSize;
}
total_row += cnt_chunks;
}
/*
* Build a tuple descriptor for our result type
* The number and type of attributes have to match the definition of the
* view yezzey_offload_relation_status_internal
*/
} else if (aorel->rd_rel->relstorage == 'c') {
segfile_array_cs = GetAllAOCSFileSegInfo(
aorel, appendOnlyMetaDataSnapshot, &total_segfiles);
for (inat = 0; inat < nvp; ++inat) {
for (i = 0; i < total_segfiles; i++) {
segno = segfile_array_cs[i]->segno;
/* in AOCS case actual *segno* differs from segfile_array_cs[i]->segno
* whis is logical number of segment. On physical level, each logical
* segno (segfile_array_cs[i]->segno) is represented by
* AOTupleId_MultiplierSegmentFileNum in storage (1 file per
* attribute)
*/
pseudosegno = (inat * AOTupleId_MultiplierSegmentFileNum) + segno;
modcount = segfile_array_cs[i]->modcount;
logicalEof = segfile_array_cs[i]->vpinfo.entry[inat].eof;
size_t curr_local_bytes = 0;
size_t curr_external_bytes = 0;
size_t curr_local_commited_bytes = 0;
yezzeyChunkMeta *list;
size_t cnt_chunks;
if (statRelationSpaceUsagePerExternalChunk(
aorel, pseudosegno, modcount, logicalEof, &curr_local_bytes,
&curr_local_commited_bytes, &list, &cnt_chunks) < 0) {
elog(ERROR, "failed to stat segment %d usage", segno);
}
local_bytes = curr_local_bytes;
external_bytes = curr_external_bytes;
local_commited_bytes = curr_local_commited_bytes;
chunkInfo = repalloc(chunkInfo, sizeof(yezzeyChunkMetaInfo) *
(total_row + cnt_chunks));
for (size_t chunk_index = 0; chunk_index < cnt_chunks;
++chunk_index) {
chunkInfo[total_row + chunk_index].reloid = reloid;
chunkInfo[total_row + chunk_index].segindex = GpIdentity.segindex;
chunkInfo[total_row + chunk_index].segfileindex = i;
chunkInfo[total_row + chunk_index].external_storage_filepath =
list[chunk_index].chunkName;
chunkInfo[total_row + chunk_index].local_bytes = local_bytes;
chunkInfo[total_row + chunk_index].local_commited_bytes =
local_commited_bytes;
chunkInfo[total_row + chunk_index].external_bytes =
list[chunk_index].chunkSize;
}
total_row += cnt_chunks;
}
}
} else {
elog(ERROR, "yezzey: wrong relation storage type: not AO/AOCS relation");
}
funcctx->tuple_desc = CreateTemplateTupleDesc(
NUM_USED_OFFLOAD_PER_SEGMENT_STATUS_STRUCT, false);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)1, "reloid", OIDOID,
-1 /* typmod */, 0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)2, "segindex", INT4OID,
-1 /* typmod */, 0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)3, "segfileindex",
INT4OID, -1 /* typmod */, 0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)4,
"external_storage_filepath", TEXTOID, -1 /* typmod */,
0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)5, "local_bytes",
INT8OID, -1 /* typmod */, 0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)6,
"local_commited_bytes", INT8OID, -1 /* typmod */,
0 /* attdim */);
TupleDescInitEntry(funcctx->tuple_desc, (AttrNumber)7, "external_bytes",
INT8OID, -1 /* typmod */, 0 /* attdim */);
funcctx->tuple_desc = BlessTupleDesc(funcctx->tuple_desc);
/*
* Generate attribute metadata needed later to produce tuples from raw
* C strings
*/
attinmeta = TupleDescGetAttInMetadata(funcctx->tuple_desc);
funcctx->attinmeta = attinmeta;
if (total_row > 0) {
funcctx->max_calls = total_row;
funcctx->user_fctx = chunkInfo;