This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathcatalog.cpp
1701 lines (1507 loc) · 70.6 KB
/
catalog.cpp
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
//===----------------------------------------------------------------------===//
//
// Peloton
//
// catalog.cpp
//
// Identification: src/catalog/catalog.cpp
//
// Copyright (c) 2015-2018, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include "catalog/catalog.h"
#include "catalog/column_catalog.h"
#include "catalog/database_catalog.h"
#include "catalog/database_metrics_catalog.h"
#include "catalog/index_catalog.h"
#include "catalog/index_metrics_catalog.h"
#include "catalog/language_catalog.h"
#include "catalog/proc_catalog.h"
#include "catalog/query_history_catalog.h"
#include "catalog/query_metrics_catalog.h"
#include "catalog/settings_catalog.h"
#include "catalog/system_catalogs.h"
#include "catalog/table_catalog.h"
#include "catalog/table_metrics_catalog.h"
#include "catalog/trigger_catalog.h"
#include "concurrency/transaction_manager_factory.h"
#include "executor/executor_context.h"
#include "executor/insert_executor.h"
#include "executor/seq_scan_executor.h"
#include "function/date_functions.h"
#include "function/decimal_functions.h"
#include "function/old_engine_string_functions.h"
#include "function/timestamp_functions.h"
#include "index/index_factory.h"
#include "planner/insert_plan.h"
#include "planner/seq_scan_plan.h"
#include "settings/settings_manager.h"
#include "storage/storage_manager.h"
#include "storage/table_factory.h"
#include "type/ephemeral_pool.h"
namespace peloton {
namespace catalog {
// Get instance of the global catalog
Catalog *Catalog::GetInstance() {
static Catalog global_catalog;
return &global_catalog;
}
/* Initialization of catalog, including:
* 1) create peloton database, create catalog tables, add them into
* peloton database, insert columns into pg_attribute
* 2) create necessary indexes, insert into pg_index
* 3) insert peloton into pg_database, catalog tables into pg_table
*/
Catalog::Catalog() : pool_(new type::EphemeralPool()) {
// Begin transaction for catalog initialization
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
auto txn = txn_manager.BeginTransaction();
auto storage_manager = storage::StorageManager::GetInstance();
// Create peloton database
auto peloton = new storage::Database(CATALOG_DATABASE_OID);
peloton->setDBName(CATALOG_DATABASE_NAME);
storage_manager->AddDatabaseToStorageManager(peloton);
// Create catalog tables
DatabaseCatalog::GetInstance(peloton, pool_.get(), txn);
BootstrapSystemCatalogs(peloton, txn);
// Insert peloton database into pg_database
DatabaseCatalog::GetInstance()->InsertDatabase(
CATALOG_DATABASE_OID, CATALOG_DATABASE_NAME, pool_.get(), txn);
// Commit transaction
txn_manager.CommitTransaction(txn);
}
/*@brief This function *MUST* be called after a new database is created to
* bootstrap all system catalog tables for that database. The system catalog
* tables must be created in certain order to make sure all tuples are indexed
*
* @param database database which this system catalogs belong to
* @param txn transaction context
*/
void Catalog::BootstrapSystemCatalogs(storage::Database *database,
concurrency::TransactionContext *txn) {
oid_t database_oid = database->GetOid();
catalog_map_.emplace(database_oid,
std::shared_ptr<SystemCatalogs>(
new SystemCatalogs(database, pool_.get(), txn)));
auto system_catalogs = catalog_map_[database_oid];
// Create indexes on catalog tables, insert them into pg_index
// actual index already added in
system_catalogs->GetIndexCatalog()->InsertIndex(
COLUMN_CATALOG_PKEY_OID, COLUMN_CATALOG_NAME "_pkey", COLUMN_CATALOG_OID,
CATALOG_SCHEMA_NAME, IndexType::BWTREE, IndexConstraintType::PRIMARY_KEY,
true,
{ColumnCatalog::ColumnId::TABLE_OID,
ColumnCatalog::ColumnId::COLUMN_NAME},
pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
COLUMN_CATALOG_SKEY0_OID, COLUMN_CATALOG_NAME "_skey0",
COLUMN_CATALOG_OID, CATALOG_SCHEMA_NAME, IndexType::BWTREE,
IndexConstraintType::UNIQUE, true,
{ColumnCatalog::ColumnId::TABLE_OID, ColumnCatalog::ColumnId::COLUMN_ID},
pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
COLUMN_CATALOG_SKEY1_OID, COLUMN_CATALOG_NAME "_skey1",
COLUMN_CATALOG_OID, CATALOG_SCHEMA_NAME, IndexType::BWTREE,
IndexConstraintType::DEFAULT, false, {ColumnCatalog::ColumnId::TABLE_OID},
pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
INDEX_CATALOG_PKEY_OID, INDEX_CATALOG_NAME "_pkey", INDEX_CATALOG_OID,
CATALOG_SCHEMA_NAME, IndexType::BWTREE, IndexConstraintType::PRIMARY_KEY,
true, {IndexCatalog::ColumnId::INDEX_OID}, pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
INDEX_CATALOG_SKEY0_OID, INDEX_CATALOG_NAME "_skey0", INDEX_CATALOG_OID,
CATALOG_SCHEMA_NAME, IndexType::BWTREE, IndexConstraintType::UNIQUE, true,
{IndexCatalog::ColumnId::INDEX_NAME}, pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
INDEX_CATALOG_SKEY1_OID, INDEX_CATALOG_NAME "_skey1", INDEX_CATALOG_OID,
CATALOG_SCHEMA_NAME, IndexType::BWTREE, IndexConstraintType::DEFAULT,
false, {IndexCatalog::ColumnId::TABLE_OID}, pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
DATABASE_CATALOG_PKEY_OID, DATABASE_CATALOG_NAME "_pkey",
DATABASE_CATALOG_OID, CATALOG_SCHEMA_NAME, IndexType::BWTREE,
IndexConstraintType::PRIMARY_KEY, true,
{DatabaseCatalog::ColumnId::DATABASE_OID}, pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
DATABASE_CATALOG_SKEY0_OID, DATABASE_CATALOG_NAME "_skey0",
DATABASE_CATALOG_OID, CATALOG_SCHEMA_NAME, IndexType::BWTREE,
IndexConstraintType::UNIQUE, true,
{DatabaseCatalog::ColumnId::DATABASE_NAME}, pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
SCHEMA_CATALOG_PKEY_OID, SCHEMA_CATALOG_NAME "_pkey", SCHEMA_CATALOG_OID,
CATALOG_SCHEMA_NAME, IndexType::BWTREE, IndexConstraintType::PRIMARY_KEY,
true, {SchemaCatalog::ColumnId::SCHEMA_OID}, pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
SCHEMA_CATALOG_SKEY0_OID, SCHEMA_CATALOG_NAME "_skey0",
SCHEMA_CATALOG_OID, CATALOG_SCHEMA_NAME, IndexType::BWTREE,
IndexConstraintType::UNIQUE, true, {SchemaCatalog::ColumnId::SCHEMA_NAME},
pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
TABLE_CATALOG_PKEY_OID, TABLE_CATALOG_NAME "_pkey", TABLE_CATALOG_OID,
CATALOG_SCHEMA_NAME, IndexType::BWTREE, IndexConstraintType::PRIMARY_KEY,
true, {TableCatalog::ColumnId::TABLE_OID}, pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
TABLE_CATALOG_SKEY0_OID, TABLE_CATALOG_NAME "_skey0", TABLE_CATALOG_OID,
CATALOG_SCHEMA_NAME, IndexType::BWTREE, IndexConstraintType::UNIQUE, true,
{TableCatalog::ColumnId::TABLE_NAME}, pool_.get(), txn);
system_catalogs->GetIndexCatalog()->InsertIndex(
TABLE_CATALOG_SKEY1_OID, TABLE_CATALOG_NAME "_skey1", TABLE_CATALOG_OID,
CATALOG_SCHEMA_NAME, IndexType::BWTREE, IndexConstraintType::DEFAULT,
false, {TableCatalog::ColumnId::DATABASE_OID}, pool_.get(), txn);
// Insert records(default + pg_catalog namespace) into pg_namespace
system_catalogs->GetSchemaCatalog()->InsertSchema(
CATALOG_SCHEMA_OID, CATALOG_SCHEMA_NAME, pool_.get(), txn);
system_catalogs->GetSchemaCatalog()->InsertSchema(
DEFUALT_SCHEMA_OID, DEFUALT_SCHEMA_NAME, pool_.get(), txn);
// Insert catalog tables into pg_table
// pg_database record is shared across different databases
system_catalogs->GetTableCatalog()->InsertTable(
DATABASE_CATALOG_OID, DATABASE_CATALOG_NAME, CATALOG_SCHEMA_NAME,
CATALOG_DATABASE_OID, pool_.get(), txn);
system_catalogs->GetTableCatalog()->InsertTable(
SCHEMA_CATALOG_OID, SCHEMA_CATALOG_NAME, CATALOG_SCHEMA_NAME,
database_oid, pool_.get(), txn);
system_catalogs->GetTableCatalog()->InsertTable(
TABLE_CATALOG_OID, TABLE_CATALOG_NAME, CATALOG_SCHEMA_NAME, database_oid,
pool_.get(), txn);
system_catalogs->GetTableCatalog()->InsertTable(
INDEX_CATALOG_OID, INDEX_CATALOG_NAME, CATALOG_SCHEMA_NAME, database_oid,
pool_.get(), txn);
system_catalogs->GetTableCatalog()->InsertTable(
COLUMN_CATALOG_OID, COLUMN_CATALOG_NAME, CATALOG_SCHEMA_NAME,
database_oid, pool_.get(), txn);
}
void Catalog::Bootstrap() {
auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();
auto txn = txn_manager.BeginTransaction();
// bootstrap pg_catalog database
catalog_map_[CATALOG_DATABASE_OID]->Bootstrap(CATALOG_DATABASE_NAME, txn);
// bootstrap other global catalog tables
DatabaseMetricsCatalog::GetInstance(txn);
SettingsCatalog::GetInstance(txn);
LanguageCatalog::GetInstance(txn);
// TODO: change pg_proc to per database
ProcCatalog::GetInstance(txn);
if (settings::SettingsManager::GetBool(settings::SettingId::brain)) {
QueryHistoryCatalog::GetInstance(txn);
}
txn_manager.CommitTransaction(txn);
InitializeLanguages();
InitializeFunctions();
}
//===----------------------------------------------------------------------===//
// CREATE FUNCTIONS
//===----------------------------------------------------------------------===//
ResultType Catalog::CreateDatabase(const std::string &database_name,
concurrency::TransactionContext *txn) {
if (txn == nullptr)
throw CatalogException("Do not have transaction to create database " +
database_name);
auto pg_database = DatabaseCatalog::GetInstance();
auto storage_manager = storage::StorageManager::GetInstance();
// Check if a database with the same name exists
auto database_object = pg_database->GetDatabaseObject(database_name, txn);
if (database_object != nullptr)
throw CatalogException("Database " + database_name + " already exists");
// Create actual database
oid_t database_oid = pg_database->GetNextOid();
storage::Database *database = new storage::Database(database_oid);
// TODO: This should be deprecated, dbname should only exists in pg_db
database->setDBName(database_name);
{
std::lock_guard<std::mutex> lock(catalog_mutex);
storage_manager->AddDatabaseToStorageManager(database);
}
// put database object into rw_object_set
txn->RecordCreate(database_oid, INVALID_OID, INVALID_OID);
// Insert database record into pg_db
pg_database->InsertDatabase(database_oid, database_name, pool_.get(), txn);
// add core & non-core system catalog tables into database
BootstrapSystemCatalogs(database, txn);
catalog_map_[database_oid]->Bootstrap(database_name, txn);
LOG_TRACE("Database %s created. Returning RESULT_SUCCESS.",
database_name.c_str());
return ResultType::SUCCESS;
}
/*@brief create schema(namespace)
* @param database_name the database which the namespace belongs to
* @param schema_name name of the schema
* @param txn TransactionContext
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/
ResultType Catalog::CreateSchema(const std::string &database_name,
const std::string &schema_name,
concurrency::TransactionContext *txn) {
if (txn == nullptr)
throw CatalogException(
"Do not have transaction to create schema(namespace) " + database_name);
// check whether database exists from pg_database
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_name, txn);
if (database_object == nullptr)
throw CatalogException("Can't find Database " + database_name +
" to create schema");
// check whether namespace exists from pg_namespace
auto pg_namespace =
catalog_map_[database_object->GetDatabaseOid()]->GetSchemaCatalog();
auto schema_object = pg_namespace->GetSchemaObject(schema_name, txn);
if (schema_object != nullptr)
throw CatalogException("Schema(namespace) " + schema_name +
" already exists");
// Since there isn't physical class corresponds to schema(namespace), the only
// thing needs to be done is inserting record into pg_namespace
pg_namespace->InsertSchema(pg_namespace->GetNextOid(), schema_name,
pool_.get(), txn);
LOG_TRACE("Schema(namespace) %s created. Returning RESULT_SUCCESS.",
schema_name.c_str());
return ResultType::SUCCESS;
}
/*@brief create table
* @param database_name the database which the table belongs to
* @param schema_name name of schema the table belongs to
* @param table_name name of the table
* @param schema schema, a.k.a metadata of the table
* @param txn TransactionContext
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/
ResultType Catalog::CreateTable(const std::string &database_name,
const std::string &schema_name,
const std::string &table_name,
std::unique_ptr<catalog::Schema> schema,
concurrency::TransactionContext *txn,
bool is_catalog, oid_t tuples_per_tilegroup) {
if (txn == nullptr)
throw CatalogException("Do not have transaction to create table " +
table_name);
LOG_TRACE("Creating table %s in database %s", table_name.c_str(),
database_name.c_str());
// check whether database exists from pg_database
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_name, txn);
if (database_object == nullptr)
throw CatalogException("Can't find Database " + database_name +
" to create table");
// check whether namespace exists from pg_namespace
auto schema_object = catalog_map_[database_object->GetDatabaseOid()]
->GetSchemaCatalog()
->GetSchemaObject(schema_name, txn);
if (schema_object == nullptr)
throw CatalogException("Can't find namespace " + schema_name +
" to create table");
// get table oid from pg_table
auto table_object = database_object->GetTableObject(table_name, schema_name);
if (table_object != nullptr)
throw CatalogException("Table: " + schema_name + "." + table_name +
" already exists");
auto storage_manager = storage::StorageManager::GetInstance();
auto database =
storage_manager->GetDatabaseWithOid(database_object->GetDatabaseOid());
// Check duplicate column names
std::set<std::string> column_names;
auto columns = schema.get()->GetColumns();
for (auto column : columns) {
auto column_name = column.GetName();
if (column_names.count(column_name) == 1)
throw CatalogException("Can't create table " + table_name +
" with duplicate column name");
column_names.insert(column_name);
}
// Create actual table
auto pg_table =
catalog_map_[database_object->GetDatabaseOid()]->GetTableCatalog();
auto pg_attribute =
catalog_map_[database_object->GetDatabaseOid()]->GetColumnCatalog();
oid_t table_oid = pg_table->GetNextOid();
bool own_schema = true;
bool adapt_table = false;
auto table = storage::TableFactory::GetDataTable(
database_object->GetDatabaseOid(), table_oid, schema.release(),
table_name, tuples_per_tilegroup, own_schema, adapt_table, is_catalog);
database->AddTable(table, is_catalog);
// put data table object into rw_object_set
txn->RecordCreate(database_object->GetDatabaseOid(), table_oid, INVALID_OID);
// Update pg_table with table info
pg_table->InsertTable(table_oid, table_name, schema_name,
database_object->GetDatabaseOid(), pool_.get(), txn);
oid_t column_id = 0;
for (const auto &column : table->GetSchema()->GetColumns()) {
pg_attribute->InsertColumn(table_oid, column.GetName(), column_id,
column.GetOffset(), column.GetType(),
column.IsInlined(), column.GetConstraints(),
pool_.get(), txn);
// Create index on unique single column
if (column.IsUnique()) {
std::string col_name = column.GetName();
std::string index_name = table->GetName() + "_" + col_name + "_UNIQ";
CreateIndex(database_name, schema_name, table_name, {column_id},
index_name, true, IndexType::BWTREE, txn);
LOG_DEBUG("Added a UNIQUE index on %s in %s.", col_name.c_str(),
table_name.c_str());
}
column_id++;
}
CreatePrimaryIndex(database_object->GetDatabaseOid(), table_oid, schema_name,
txn);
return ResultType::SUCCESS;
}
/*@brief create primary index on table
* Note that this is a catalog helper function only called within catalog.cpp
* If you want to create index on table outside, call CreateIndex() instead
* @param database_oid the database which the indexed table belongs to
* @param table_oid oid of the table to add index on
* @param schema_name the schema which the indexed table belongs to
* @param txn TransactionContext
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/
ResultType Catalog::CreatePrimaryIndex(oid_t database_oid, oid_t table_oid,
const std::string &schema_name,
concurrency::TransactionContext *txn) {
LOG_TRACE("Trying to create primary index for table %d", table_oid);
auto storage_manager = storage::StorageManager::GetInstance();
auto database = storage_manager->GetDatabaseWithOid(database_oid);
auto table = database->GetTableWithOid(table_oid);
std::vector<oid_t> key_attrs;
catalog::Schema *key_schema = nullptr;
index::IndexMetadata *index_metadata = nullptr;
auto schema = table->GetSchema();
// Find primary index attributes
int column_idx = 0;
auto &schema_columns = schema->GetColumns();
for (auto &column : schema_columns) {
if (column.IsPrimary()) {
key_attrs.push_back(column_idx);
}
column_idx++;
}
if (key_attrs.empty()) return ResultType::FAILURE;
key_schema = catalog::Schema::CopySchema(schema, key_attrs);
key_schema->SetIndexedColumns(key_attrs);
std::string index_name = table->GetName() + "_pkey";
bool unique_keys = true;
auto pg_index = catalog_map_[database_oid]->GetIndexCatalog();
oid_t index_oid = pg_index->GetNextOid();
index_metadata = new index::IndexMetadata(
index_name, index_oid, table_oid, database_oid, IndexType::BWTREE,
IndexConstraintType::PRIMARY_KEY, schema, key_schema, key_attrs,
unique_keys);
std::shared_ptr<index::Index> pkey_index(
index::IndexFactory::GetIndex(index_metadata));
table->AddIndex(pkey_index);
// put index object into rw_object_set
txn->RecordCreate(database_oid, table_oid, index_oid);
// insert index record into index_catalog(pg_index) table
pg_index->InsertIndex(index_oid, index_name, table_oid, schema_name,
IndexType::BWTREE, IndexConstraintType::PRIMARY_KEY,
unique_keys, key_attrs, pool_.get(), txn);
LOG_TRACE("Successfully created primary key index '%s' for table '%s'",
index_name.c_str(), table->GetName().c_str());
return ResultType::SUCCESS;
}
/*@brief create index on table
* @param database_name the database which the indexed table belongs to
* @param schema_name the namespace which the indexed table belongs to
* @param table_name name of the table to add index on
* @param index_attr collection of the indexed attribute(column) name
* @param index_name name of the table to add index on
* @param unique_keys index supports duplicate key or not
* @param index_type the type of index(default value is BWTREE)
* @param txn TransactionContext
* @param is_catalog index is built on catalog table or not(useful in
* catalog table Initialization)
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/
ResultType Catalog::CreateIndex(const std::string &database_name,
const std::string &schema_name,
const std::string &table_name,
const std::vector<oid_t> &key_attrs,
const std::string &index_name, bool unique_keys,
IndexType index_type,
concurrency::TransactionContext *txn) {
if (txn == nullptr)
throw CatalogException("Do not have transaction to create database " +
index_name);
LOG_TRACE("Trying to create index %s for table %s", index_name.c_str(),
table_name.c_str());
// check if database exists
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_name, txn);
if (database_object == nullptr)
throw CatalogException("Can't find Database " + database_name +
" to create index");
// check if table exists
auto table_object = database_object->GetTableObject(table_name, schema_name);
if (table_object == nullptr)
throw CatalogException("Can't find table " + schema_name + "." +
table_name + " to create index");
IndexConstraintType index_constraint =
unique_keys ? IndexConstraintType::UNIQUE : IndexConstraintType::DEFAULT;
ResultType success = CreateIndex(
database_object->GetDatabaseOid(), table_object->GetTableOid(), key_attrs,
schema_name, index_name, index_type, index_constraint, unique_keys, txn);
return success;
}
ResultType Catalog::CreateIndex(
oid_t database_oid, oid_t table_oid, const std::vector<oid_t> &key_attrs,
const std::string &schema_name, const std::string &index_name,
IndexType index_type, IndexConstraintType index_constraint,
bool unique_keys, concurrency::TransactionContext *txn, bool is_catalog) {
if (txn == nullptr)
throw CatalogException("Do not have transaction to create index " +
index_name);
LOG_TRACE("Trying to create index for table %d", table_oid);
if (is_catalog == false) {
// check if table already has index with same name
// only check when is_catalog flag == false
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_oid, txn);
auto table_object = database_object->GetTableObject(table_oid);
auto index_object = table_object->GetIndexObject(index_name);
if (index_object != nullptr)
throw CatalogException("Index " + index_name + " already exists in" +
database_object->GetDatabaseName());
}
auto storage_manager = storage::StorageManager::GetInstance();
auto database = storage_manager->GetDatabaseWithOid(database_oid);
auto table = database->GetTableWithOid(table_oid);
auto schema = table->GetSchema();
// Passed all checks, now get all index metadata
LOG_TRACE("Trying to create index %s on table %d", index_name.c_str(),
table_oid);
auto pg_index = catalog_map_[database_oid]->GetIndexCatalog();
oid_t index_oid = pg_index->GetNextOid();
auto key_schema = catalog::Schema::CopySchema(schema, key_attrs);
key_schema->SetIndexedColumns(key_attrs);
// Set index metadata
auto index_metadata = new index::IndexMetadata(
index_name, index_oid, table_oid, database_oid, index_type,
index_constraint, schema, key_schema, key_attrs, unique_keys);
// Add index to table
std::shared_ptr<index::Index> key_index(
index::IndexFactory::GetIndex(index_metadata));
table->AddIndex(key_index);
// Put index object into rw_object_set
txn->RecordCreate(database_oid, table_oid, index_oid);
// Insert index record into pg_index
pg_index->InsertIndex(index_oid, index_name, table_oid, schema_name,
index_type, index_constraint, unique_keys, key_attrs,
pool_.get(), txn);
LOG_TRACE("Successfully add index for table %s contains %d indexes",
table->GetName().c_str(), (int)table->GetValidIndexCount());
return ResultType::SUCCESS;
}
//===----------------------------------------------------------------------===//
// DROP FUNCTIONS
//===----------------------------------------------------------------------===//
ResultType Catalog::DropDatabaseWithName(const std::string &database_name,
concurrency::TransactionContext *txn) {
if (txn == nullptr)
throw CatalogException("Do not have transaction to drop database " +
database_name);
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_name, txn);
if (database_object == nullptr)
throw CatalogException("Drop Database: " + database_name +
" does not exist");
return DropDatabaseWithOid(database_object->GetDatabaseOid(), txn);
}
ResultType Catalog::DropDatabaseWithOid(oid_t database_oid,
concurrency::TransactionContext *txn) {
if (txn == nullptr)
throw CatalogException("Do not have transaction to drop database " +
std::to_string(database_oid));
auto storage_manager = storage::StorageManager::GetInstance();
// Drop actual tables in the database
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_oid, txn);
auto table_objects = database_object->GetTableObjects();
for (auto it : table_objects) {
DropTable(database_oid, it.second->GetTableOid(), txn);
}
// Drop database record in catalog
if (!DatabaseCatalog::GetInstance()->DeleteDatabase(database_oid, txn))
throw CatalogException("Database record: " + std::to_string(database_oid) +
" does not exist in pg_database");
catalog_map_.erase(database_oid);
// put database object into rw_object_set
storage_manager->GetDatabaseWithOid(database_oid);
txn->RecordDrop(database_oid, INVALID_OID, INVALID_OID);
return ResultType::SUCCESS;
}
/*@brief Drop schema
* 1. drop all the tables within this schema
* 2. delete record within pg_namespace
* @param database_name the database which the dropped table belongs to
* @param schema_name the dropped schema(namespace) name
* @param txn TransactionContext
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/
ResultType Catalog::DropSchema(const std::string &database_name,
const std::string &schema_name,
concurrency::TransactionContext *txn) {
if (txn == nullptr)
throw CatalogException("Do not have transaction to drop schema " +
schema_name);
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_name, txn);
if (database_object == nullptr)
throw CatalogException("Drop Schema: database " + database_name +
" does not exist");
// check whether namespace exists from pg_namespace
auto pg_namespace =
catalog_map_[database_object->GetDatabaseOid()]->GetSchemaCatalog();
auto schema_object = pg_namespace->GetSchemaObject(schema_name, txn);
if (schema_object == nullptr)
throw CatalogException("Can't find namespace " + schema_name + " to drop");
auto table_objects = database_object->GetTableObjects(schema_name);
for (auto it : table_objects) {
DropTable(it->GetDatabaseOid(), it->GetTableOid(), txn);
}
// remove record within pg_namespace
pg_namespace->DeleteSchema(schema_name, txn);
return ResultType::SUCCESS;
}
/*@brief Drop table
* 1. drop all the indexes on actual table, and drop index records in
* pg_index
* 2. drop all the columns records in pg_attribute
* 3. drop table record in pg_table
* 4. delete actual table(storage level), cleanup schema, foreign keys,
* tile_groups
* @param database_name the database which the dropped table belongs to
* @param schema_name the namespace which the dropped table belongs
* to
* @param table_name the dropped table name
* @param txn TransactionContext
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/
ResultType Catalog::DropTable(const std::string &database_name,
const std::string &schema_name,
const std::string &table_name,
concurrency::TransactionContext *txn) {
if (txn == nullptr)
throw CatalogException("Do not have transaction to drop table " +
table_name);
// Checking if statement is valid
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_name, txn);
if (database_object == nullptr)
throw CatalogException("Drop Table: database " + database_name +
" does not exist");
// check if table exists
auto table_object = database_object->GetTableObject(table_name, schema_name);
if (table_object == nullptr)
throw CatalogException("Drop Table: table " + schema_name + "." +
table_name + " does not exist");
ResultType result = DropTable(database_object->GetDatabaseOid(),
table_object->GetTableOid(), txn);
return result;
}
/*@brief Drop table
* 1. drop all the indexes on actual table, and drop index records in pg_index
* 2. drop all the columns records in pg_attribute
* 3. drop table record in pg_table
* 4. delete actual table(storage level), cleanup schema, foreign keys,
* tile_groups
* @param database_oid the database which the dropped table belongs to
* @param table_oid the dropped table name
* @param txn TransactionContext
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/
ResultType Catalog::DropTable(oid_t database_oid, oid_t table_oid,
concurrency::TransactionContext *txn) {
LOG_TRACE("Dropping table %d from database %d", database_oid, table_oid);
auto storage_manager = storage::StorageManager::GetInstance();
auto database = storage_manager->GetDatabaseWithOid(database_oid);
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_oid, txn);
auto table_object = database_object->GetTableObject(table_oid);
auto index_objects = table_object->GetIndexObjects();
LOG_TRACE("dropping #%d indexes", (int)index_objects.size());
// delete trigger and records in pg_trigger
auto pg_trigger =
catalog_map_[database_object->GetDatabaseOid()]->GetTriggerCatalog();
std::unique_ptr<trigger::TriggerList> trigger_lists =
pg_trigger->GetTriggers(table_oid, txn);
for (int i = 0; i < trigger_lists->GetTriggerListSize(); i++)
pg_trigger->DropTrigger(database_oid, table_oid,
trigger_lists->Get(i)->GetTriggerName(), txn);
// delete index and records pg_index
for (auto it : index_objects)
DropIndex(database_oid, it.second->GetIndexOid(), txn);
// delete record in pg_attribute
auto pg_attribute =
catalog_map_[database_object->GetDatabaseOid()]->GetColumnCatalog();
pg_attribute->DeleteColumns(table_oid, txn);
// delete record in pg_table
auto pg_table =
catalog_map_[database_object->GetDatabaseOid()]->GetTableCatalog();
pg_table->DeleteTable(table_oid, txn);
database->GetTableWithOid(table_oid);
txn->RecordDrop(database_oid, table_oid, INVALID_OID);
return ResultType::SUCCESS;
}
/*@brief Drop Index on table
* @param index_oid the oid of the index to be dropped
* @param txn TransactionContext
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/
ResultType Catalog::DropIndex(oid_t database_oid, oid_t index_oid,
concurrency::TransactionContext *txn) {
if (txn == nullptr)
throw CatalogException("Do not have transaction to drop index " +
std::to_string(index_oid));
// find index catalog object by looking up pg_index or read from cache using
// index_oid
auto pg_index = catalog_map_[database_oid]->GetIndexCatalog();
auto index_object = pg_index->GetIndexObject(index_oid, txn);
if (index_object == nullptr) {
throw CatalogException("Can't find index " + std::to_string(index_oid) +
" to drop");
}
auto storage_manager = storage::StorageManager::GetInstance();
auto table = storage_manager->GetTableWithOid(database_oid,
index_object->GetTableOid());
// drop record in pg_index
pg_index->DeleteIndex(index_oid, txn);
LOG_TRACE("Successfully drop index %d for table %s", index_oid,
table->GetName().c_str());
// register index object in rw_object_set
table->GetIndexWithOid(index_oid);
txn->RecordDrop(database_oid, index_object->GetTableOid(), index_oid);
return ResultType::SUCCESS;
}
//===--------------------------------------------------------------------===//
// GET WITH NAME - CHECK FROM CATALOG TABLES, USING TRANSACTION
//===--------------------------------------------------------------------===//
/* Check database from pg_database with database_name using txn,
* get it from storage layer using database_oid,
* throw exception and abort txn if not exists/invisible
* */
storage::Database *Catalog::GetDatabaseWithName(
const std::string &database_name,
concurrency::TransactionContext *txn) const {
PELOTON_ASSERT(txn != nullptr);
// Check in pg_database using txn
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_name, txn);
if (database_object == nullptr) {
throw CatalogException("Database " + database_name + " is not found");
}
auto storage_manager = storage::StorageManager::GetInstance();
return storage_manager->GetDatabaseWithOid(database_object->GetDatabaseOid());
}
/* Check table from pg_table with table_name & schema_name using txn,
* get it from storage layer using table_oid,
* throw exception and abort txn if not exists/invisible
* */
storage::DataTable *Catalog::GetTableWithName(
const std::string &database_name, const std::string &schema_name,
const std::string &table_name, concurrency::TransactionContext *txn) {
PELOTON_ASSERT(txn != nullptr);
LOG_TRACE("Looking for table %s in database %s", table_name.c_str(),
database_name.c_str());
// Check in pg_table, throw exception and abort txn if not exists
auto table_object =
GetTableObject(database_name, schema_name, table_name, txn);
// Get table from storage manager
auto storage_manager = storage::StorageManager::GetInstance();
return storage_manager->GetTableWithOid(table_object->GetDatabaseOid(),
table_object->GetTableOid());
}
/* Check table from pg_table with table_name using txn,
* get it from storage layer using table_oid,
* throw exception and abort txn if not exists/invisible
* */
std::shared_ptr<DatabaseCatalogObject> Catalog::GetDatabaseObject(
const std::string &database_name, concurrency::TransactionContext *txn) {
if (txn == nullptr) {
throw CatalogException("Do not have transaction to get table object " +
database_name);
}
LOG_TRACE("Looking for database %s", database_name.c_str());
// Check in pg_database, throw exception and abort txn if not exists
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_name, txn);
if (!database_object || database_object->GetDatabaseOid() == INVALID_OID) {
throw CatalogException("Database " + database_name + " is not found");
}
return database_object;
}
std::shared_ptr<DatabaseCatalogObject> Catalog::GetDatabaseObject(
oid_t database_oid, concurrency::TransactionContext *txn) {
if (txn == nullptr) {
throw CatalogException("Do not have transaction to get database object " +
std::to_string(database_oid));
}
LOG_TRACE("Looking for database %u", database_oid);
// Check in pg_database, throw exception and abort txn if not exists
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_oid, txn);
if (!database_object || database_object->GetDatabaseOid() == INVALID_OID) {
throw CatalogException("Database " + std::to_string(database_oid) +
" is not found");
}
return database_object;
}
/* Check table from pg_table with table_name & schema_name using txn,
* get it from storage layer using table_oid,
* throw exception and abort txn if not exists/invisible
* */
std::shared_ptr<TableCatalogObject> Catalog::GetTableObject(
const std::string &database_name, const std::string &schema_name,
const std::string &table_name, concurrency::TransactionContext *txn) {
if (txn == nullptr) {
throw CatalogException("Do not have transaction to get table object " +
database_name + "." + table_name);
}
LOG_TRACE("Looking for table %s in database %s", table_name.c_str(),
database_name.c_str());
// Check in pg_database, throw exception and abort txn if not exists
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_name, txn);
if (!database_object || database_object->GetDatabaseOid() == INVALID_OID) {
throw CatalogException("Database " + database_name + " is not found");
}
// Check in pg_table using txn
auto table_object = database_object->GetTableObject(table_name, schema_name);
if (!table_object || table_object->GetTableOid() == INVALID_OID) {
// throw table not found exception and explicitly abort txn
throw CatalogException("Table " + schema_name + "." + table_name +
" is not found");
}
return table_object;
}
std::shared_ptr<TableCatalogObject> Catalog::GetTableObject(
oid_t database_oid, oid_t table_oid, concurrency::TransactionContext *txn) {
if (txn == nullptr) {
throw CatalogException("Do not have transaction to get table object " +
std::to_string(database_oid) + "." +
std::to_string(table_oid));
}
LOG_TRACE("Looking for table %u in database %u", table_oid, database_oid);
// Check in pg_database, throw exception and abort txn if not exists
auto database_object =
DatabaseCatalog::GetInstance()->GetDatabaseObject(database_oid, txn);
if (!database_object || database_object->GetDatabaseOid() == INVALID_OID) {
throw CatalogException("Database " + std::to_string(database_oid) +
" is not found");
}
// Check in pg_table using txn
auto table_object = database_object->GetTableObject(table_oid);
if (!table_object || table_object->GetTableOid() == INVALID_OID) {
// throw table not found exception and explicitly abort txn
throw CatalogException("Table " + std::to_string(table_oid) +
" is not found");
}
return table_object;
}
std::shared_ptr<SystemCatalogs> Catalog::GetSystemCatalogs(
const oid_t database_oid) {
if (catalog_map_.find(database_oid) == catalog_map_.end()) {
throw CatalogException("Failed to find SystemCatalog for database_oid = " +
std::to_string(database_oid));
}
return catalog_map_[database_oid];
}
//===--------------------------------------------------------------------===//
// ALTER TABLE
//===--------------------------------------------------------------------===//
/**
* @brief Helper function for alter table, called internally
* @param database_oid database to which the table belongs to
* @param table_oid table to which the column belongs to
* @param new_schema the new table schema
* @param txn the transaction Context
* @return TransactionContext ResultType(SUCCESS or FAILURE)
*/
ResultType Catalog::AlterTable(oid_t database_oid, oid_t table_oid, const std::string &schema_name,
std::unique_ptr<catalog::Schema> &new_schema,
concurrency::TransactionContext *txn) {
LOG_TRACE("AlterTable in Catalog");
if (txn == nullptr)
throw CatalogException("Alter table requires transaction");
try {
auto storage_manager = storage::StorageManager::GetInstance();
auto database = storage_manager->GetDatabaseWithOid(database_oid);
try {
auto old_table = database->GetTableWithOid(table_oid);
auto old_schema = old_table->GetSchema();
auto pg_index = catalog_map_[database_oid]->GetIndexCatalog();
// Step 1: build empty table with new schema
bool own_schema = true;
bool adapt_table = false;
auto new_table = storage::TableFactory::GetDataTable(
database_oid, table_oid,
catalog::Schema::CopySchema(new_schema.get()), old_table->GetName(),
DEFAULT_TUPLES_PER_TILEGROUP, own_schema, adapt_table);
// Step 2: Copy indexes
auto old_index_oids = pg_index->GetIndexObjects(table_oid, txn);
for (auto index_oid_pair : old_index_oids) {
oid_t index_oid = index_oid_pair.first;
// delete record in pg_index
pg_index->DeleteIndex(index_oid, txn);
// Check if all indexed columns still exists
auto old_index = old_table->GetIndexWithOid(index_oid);
bool index_exist = true;
std::vector<oid_t> new_key_attrs;
for (oid_t column_id : old_index->GetMetadata()->GetKeyAttrs()) {
bool is_found = false;
std::string column_name = old_schema->GetColumn(column_id).GetName();
oid_t i = 0;
for (auto new_column : new_schema->GetColumns()) {
if (column_name == new_column.GetName()) {
is_found = true;
new_key_attrs.push_back(i);
break;
}
i++;
}
if (!is_found) {
index_exist = false;
break;
}
}
if (!index_exist) continue;