-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcatalog.rs
More file actions
1173 lines (1069 loc) · 36.3 KB
/
catalog.rs
File metadata and controls
1173 lines (1069 loc) · 36.3 KB
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
use diesel::select;
use diesel::sql_query;
use diesel::sql_types::{Bool, Integer};
use diesel::{insert_into, OptionalExtension};
use diesel::{
sql_types::{Array, BigInt, Double, Nullable, Text},
ExpressionMethods, QueryDsl,
};
use diesel_async::{RunQueryDsl, SimpleAsyncConnection};
use graph::components::store::VersionStats;
use graph::prelude::BlockNumber;
use graph::schema::EntityType;
use itertools::Itertools;
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fmt::Write;
use std::iter::FromIterator;
use std::sync::Arc;
use std::time::Duration;
use graph::prelude::anyhow::anyhow;
use graph::{
data::subgraph::schema::POI_TABLE,
prelude::{lazy_static, StoreError, BLOCK_NUMBER_MAX},
};
use crate::AsyncPgConnection;
use crate::{
block_range::BLOCK_RANGE_COLUMN,
pool::ForeignServer,
primary::{Namespace, Site, NAMESPACE_PUBLIC},
relational::SqlName,
};
// This is a view not a table. We only read from it
table! {
information_schema.foreign_tables(foreign_table_schema, foreign_table_name) {
foreign_table_catalog -> Text,
foreign_table_schema -> Text,
foreign_table_name -> Text,
foreign_server_catalog -> Text,
foreign_server_name -> Text,
}
}
// Readonly; not all columns are mapped
table! {
pg_namespace(oid) {
oid -> Oid,
#[sql_name = "nspname"]
name -> Text,
}
}
// Readonly; only mapping the columns we want
table! {
pg_database(datname) {
datname -> Text,
datcollate -> Text,
datctype -> Text,
}
}
// Readonly; not all columns are mapped
table! {
pg_class(oid) {
oid -> Oid,
#[sql_name = "relname"]
name -> Text,
#[sql_name = "relnamespace"]
namespace -> Oid,
#[sql_name = "relpages"]
pages -> Integer,
#[sql_name = "reltuples"]
tuples -> Integer,
#[sql_name = "relkind"]
kind -> Char,
#[sql_name = "relnatts"]
natts -> Smallint,
}
}
// Readonly; not all columns are mapped
table! {
pg_attribute(oid) {
#[sql_name = "attrelid"]
oid -> Oid,
#[sql_name = "attrelid"]
relid -> Oid,
#[sql_name = "attname"]
name -> Text,
#[sql_name = "attnum"]
num -> Smallint,
#[sql_name = "attstattarget"]
stats_target -> Integer,
}
}
joinable!(pg_class -> pg_namespace(namespace));
joinable!(pg_attribute -> pg_class(relid));
allow_tables_to_appear_in_same_query!(pg_class, pg_namespace, pg_attribute);
table! {
subgraphs.table_stats {
id -> Integer,
deployment -> Integer,
table_name -> Text,
is_account_like -> Nullable<Bool>,
last_pruned_block -> Nullable<Integer>,
}
}
table! {
__diesel_schema_migrations(version) {
version -> Text,
run_on -> Timestamp,
}
}
lazy_static! {
/// The name of the table in which Diesel records migrations
static ref MIGRATIONS_TABLE: SqlName =
SqlName::verbatim("__diesel_schema_migrations".to_string());
}
pub struct Locale {
collate: String,
ctype: String,
encoding: String,
}
impl Locale {
/// Load locale information for current database
pub async fn load(conn: &mut AsyncPgConnection) -> Result<Locale, StoreError> {
use diesel::dsl::sql;
use pg_database as db;
let (collate, ctype, encoding) = db::table
.filter(db::datname.eq(sql("current_database()")))
.select((
db::datcollate,
db::datctype,
sql::<Text>("pg_encoding_to_char(encoding)::text"),
))
.get_result::<(String, String, String)>(conn)
.await?;
Ok(Locale {
collate,
ctype,
encoding,
})
}
pub fn suitable(&self) -> Result<(), String> {
if self.collate != "C" {
return Err(format!(
"database collation is `{}` but must be `C`",
self.collate
));
}
if self.ctype != "C" {
return Err(format!(
"database ctype is `{}` but must be `C`",
self.ctype
));
}
if self.encoding != "UTF8" {
return Err(format!(
"database encoding is `{}` but must be `UTF8`",
self.encoding
));
}
Ok(())
}
}
/// Information about what tables and columns we have in the database
#[derive(Debug, Clone)]
pub struct Catalog {
pub site: Arc<Site>,
text_columns: HashMap<String, HashSet<String>>,
pub use_poi: bool,
/// Whether `bytea` columns are indexed with just a prefix (`true`) or
/// in their entirety. This influences both DDL generation and how
/// queries are generated
pub use_bytea_prefix: bool,
/// Set of tables which have an explicit causality region column.
pub(crate) entities_with_causality_region: BTreeSet<EntityType>,
/// Whether the database supports `int4_minmax_multi_ops` etc.
/// See the [Postgres docs](https://www.postgresql.org/docs/15/brin-builtin-opclasses.html)
pub has_minmax_multi_ops: bool,
/// Whether the column `pg_stats.range_bounds_histogram` introduced in
/// Postgres 17 exists. See the [Postgres
/// docs](https://www.postgresql.org/docs/17/view-pg-stats.html)
pg_stats_has_range_bounds_histogram: bool,
}
impl Catalog {
/// Load the catalog for an existing subgraph
pub async fn load(
conn: &mut AsyncPgConnection,
site: Arc<Site>,
use_bytea_prefix: bool,
entities_with_causality_region: Vec<EntityType>,
) -> Result<Self, StoreError> {
let text_columns = get_text_columns(conn, &site.namespace).await?;
let use_poi = supports_proof_of_indexing(conn, &site.namespace).await?;
let has_minmax_multi_ops = has_minmax_multi_ops(conn).await?;
let pg_stats_has_range_bounds_histogram = pg_stats_has_range_bounds_histogram(conn).await?;
Ok(Catalog {
site,
text_columns,
use_poi,
use_bytea_prefix,
entities_with_causality_region: entities_with_causality_region.into_iter().collect(),
has_minmax_multi_ops,
pg_stats_has_range_bounds_histogram,
})
}
/// Return a new catalog suitable for creating a new subgraph
pub async fn for_creation(
conn: &mut AsyncPgConnection,
site: Arc<Site>,
entities_with_causality_region: BTreeSet<EntityType>,
) -> Result<Self, StoreError> {
let has_minmax_multi_ops = has_minmax_multi_ops(conn).await?;
let pg_stats_has_range_bounds_histogram = pg_stats_has_range_bounds_histogram(conn).await?;
Ok(Catalog {
site,
text_columns: HashMap::default(),
// DDL generation creates a POI table
use_poi: true,
// DDL generation creates indexes for prefixes of bytes columns
// see: attr-bytea-prefix
use_bytea_prefix: true,
entities_with_causality_region,
has_minmax_multi_ops,
pg_stats_has_range_bounds_histogram,
})
}
/// Make a catalog as if the given `schema` did not exist in the database
/// yet. This function should only be used in situations where a database
/// connection is definitely not available, such as in unit tests
pub fn for_tests(
site: Arc<Site>,
entities_with_causality_region: BTreeSet<EntityType>,
) -> Result<Self, StoreError> {
Ok(Catalog {
site,
text_columns: HashMap::default(),
use_poi: false,
use_bytea_prefix: true,
entities_with_causality_region,
has_minmax_multi_ops: false,
pg_stats_has_range_bounds_histogram: false,
})
}
/// Return `true` if `table` exists and contains the given `column` and
/// if that column is of data type `text`
pub fn is_existing_text_column(&self, table: &SqlName, column: &SqlName) -> bool {
self.text_columns
.get(table.as_str())
.map(|cols| cols.contains(column.as_str()))
.unwrap_or(false)
}
/// The operator classes to use for BRIN indexes. The first entry if the
/// operator class for `int4`, the second is for `int8`
pub fn minmax_ops(&self) -> (&str, &str) {
const MINMAX_OPS: (&str, &str) = ("int4_minmax_ops", "int8_minmax_ops");
const MINMAX_MULTI_OPS: (&str, &str) = ("int4_minmax_multi_ops", "int8_minmax_multi_ops");
if self.has_minmax_multi_ops {
MINMAX_MULTI_OPS
} else {
MINMAX_OPS
}
}
pub async fn stats(
&self,
conn: &mut AsyncPgConnection,
) -> Result<Vec<VersionStats>, StoreError> {
#[derive(Queryable, QueryableByName)]
pub struct DbStats {
#[diesel(sql_type = BigInt)]
pub entities: i64,
#[diesel(sql_type = BigInt)]
pub versions: i64,
#[diesel(sql_type = Text)]
pub tablename: String,
/// The ratio `entities / versions`
#[diesel(sql_type = Double)]
pub ratio: f64,
#[diesel(sql_type = Nullable<Integer>)]
pub last_pruned_block: Option<i32>,
}
impl From<DbStats> for VersionStats {
fn from(s: DbStats) -> Self {
VersionStats {
entities: s.entities,
versions: s.versions,
tablename: s.tablename,
ratio: s.ratio,
last_pruned_block: s.last_pruned_block,
block_range_upper: vec![],
}
}
}
#[derive(Queryable, QueryableByName)]
struct RangeHistogram {
#[diesel(sql_type = Text)]
tablename: String,
#[diesel(sql_type = Array<Integer>)]
upper: Vec<i32>,
}
async fn block_range_histogram(
conn: &mut AsyncPgConnection,
namespace: &Namespace,
) -> Result<Vec<RangeHistogram>, StoreError> {
let query = format!(
"select tablename, \
array_agg(coalesce(upper(block_range), {BLOCK_NUMBER_MAX})) upper \
from (select tablename,
unnest(range_bounds_histogram::text::int4range[]) block_range
from pg_stats where schemaname = $1 and attname = '{BLOCK_RANGE_COLUMN}') a
group by tablename
order by tablename"
);
let result = sql_query(query)
.bind::<Text, _>(namespace.as_str())
.get_results::<RangeHistogram>(conn)
.await?;
Ok(result)
}
// Get an estimate of number of rows (pg_class.reltuples) and number of
// distinct entities (based on the planners idea of how many distinct
// values there are in the `id` column) See the [Postgres
// docs](https://www.postgresql.org/docs/current/view-pg-stats.html) for
// the precise meaning of n_distinct
let query = "select case when s.n_distinct < 0 then (- s.n_distinct * c.reltuples)::int8
else s.n_distinct::int8
end as entities,
c.reltuples::int8 as versions,
c.relname as tablename,
case when c.reltuples = 0 then 0::float8
when s.n_distinct < 0 then (-s.n_distinct)::float8
else greatest(s.n_distinct, 1)::float8 / c.reltuples::float8
end as ratio,
ts.last_pruned_block
from pg_namespace n, pg_class c, pg_stats s
left outer join subgraphs.table_stats ts
on (ts.table_name = s.tablename
and ts.deployment = $1)
where n.nspname = $2
and c.relnamespace = n.oid
and s.schemaname = n.nspname
and s.attname = 'id'
and c.relname = s.tablename
order by c.relname"
.to_string();
let stats = sql_query(query)
.bind::<Integer, _>(self.site.id)
.bind::<Text, _>(self.site.namespace.as_str())
.load::<DbStats>(conn)
.await
.map_err(StoreError::from)?;
let mut range_histogram = if self.pg_stats_has_range_bounds_histogram {
block_range_histogram(conn, &self.site.namespace).await?
} else {
vec![]
};
let stats = stats
.into_iter()
.map(|s| {
let pos = range_histogram
.iter()
.position(|h| h.tablename == s.tablename);
let mut upper = pos
.map(|pos| range_histogram.swap_remove(pos))
.map(|h| h.upper)
.unwrap_or(vec![]);
// Since lower and upper are supposed to be histograms, we
// sort them
upper.sort_unstable();
let mut vs = VersionStats::from(s);
vs.block_range_upper = upper;
vs
})
.collect::<Vec<_>>();
Ok(stats)
}
}
async fn get_text_columns(
conn: &mut AsyncPgConnection,
namespace: &Namespace,
) -> Result<HashMap<String, HashSet<String>>, StoreError> {
const QUERY: &str = "
select table_name, column_name
from information_schema.columns
where table_schema = $1 and data_type = 'text'";
#[derive(Debug, QueryableByName)]
struct Column {
#[diesel(sql_type = Text)]
pub table_name: String,
#[diesel(sql_type = Text)]
pub column_name: String,
}
let map: HashMap<String, HashSet<String>> = diesel::sql_query(QUERY)
.bind::<Text, _>(namespace.as_str())
.load::<Column>(conn)
.await?
.into_iter()
.fold(HashMap::new(), |mut map, col| {
map.entry(col.table_name)
.or_default()
.insert(col.column_name);
map
});
Ok(map)
}
pub async fn table_exists(
conn: &mut AsyncPgConnection,
namespace: &str,
table: &SqlName,
) -> Result<bool, StoreError> {
#[derive(Debug, QueryableByName)]
struct Table {
#[diesel(sql_type = Text)]
#[allow(dead_code)]
pub table_name: String,
}
let query =
"SELECT table_name FROM information_schema.tables WHERE table_schema=$1 AND table_name=$2";
let result: Vec<Table> = diesel::sql_query(query)
.bind::<Text, _>(namespace)
.bind::<Text, _>(table.as_str())
.load(conn)
.await?;
Ok(!result.is_empty())
}
pub async fn supports_proof_of_indexing(
conn: &mut AsyncPgConnection,
namespace: &Namespace,
) -> Result<bool, StoreError> {
lazy_static! {
static ref POI_TABLE_NAME: SqlName = SqlName::verbatim(POI_TABLE.to_owned());
}
table_exists(conn, namespace.as_str(), &POI_TABLE_NAME).await
}
pub async fn current_servers(conn: &mut AsyncPgConnection) -> Result<Vec<String>, StoreError> {
#[derive(QueryableByName)]
struct Srv {
#[diesel(sql_type = Text)]
srvname: String,
}
Ok(sql_query("select srvname from pg_foreign_server")
.get_results::<Srv>(conn)
.await?
.into_iter()
.map(|srv| srv.srvname)
.collect())
}
/// Return the options for the foreign server `name` as a map of option
/// names to values
pub async fn server_options(
conn: &mut AsyncPgConnection,
name: &str,
) -> Result<HashMap<String, Option<String>>, StoreError> {
#[derive(QueryableByName)]
struct Srv {
#[diesel(sql_type = Array<Text>)]
srvoptions: Vec<String>,
}
let entries = sql_query("select srvoptions from pg_foreign_server where srvname = $1")
.bind::<Text, _>(name)
.get_result::<Srv>(conn)
.await?
.srvoptions
.into_iter()
.filter_map(|opt| {
let mut parts = opt.splitn(2, '=');
let key = parts.next();
let value = parts.next().map(|value| value.to_string());
key.map(|key| (key.to_string(), value))
});
Ok(HashMap::from_iter(entries))
}
pub async fn has_namespace(
conn: &mut AsyncPgConnection,
namespace: &Namespace,
) -> Result<bool, StoreError> {
use pg_namespace as nsp;
Ok(select(diesel::dsl::exists(
nsp::table.filter(nsp::name.eq(namespace.as_str())),
))
.get_result::<bool>(conn)
.await?)
}
/// Drop the schema for `src` if it is a foreign schema imported from
/// another database. If the schema does not exist, or is not a foreign
/// schema, do nothing. This crucially depends on the fact that we never mix
/// foreign and local tables in the same schema.
pub async fn drop_foreign_schema(
conn: &mut AsyncPgConnection,
src: &Site,
) -> Result<(), StoreError> {
use foreign_tables as ft;
let is_foreign = select(diesel::dsl::exists(
ft::table.filter(ft::foreign_table_schema.eq(src.namespace.as_str())),
))
.get_result::<bool>(conn)
.await?;
if is_foreign {
let query = format!("drop schema if exists {} cascade", src.namespace);
conn.batch_execute(&query).await?;
}
Ok(())
}
pub async fn foreign_tables(
conn: &mut AsyncPgConnection,
nsp: &str,
) -> Result<Vec<String>, StoreError> {
use foreign_tables as ft;
ft::table
.filter(ft::foreign_table_schema.eq(nsp))
.select(ft::foreign_table_name)
.get_results::<String>(conn)
.await
.map_err(StoreError::from)
}
/// Drop the schema `nsp` and all its contents if it exists, and create it
/// again so that `nsp` is an empty schema
pub async fn recreate_schema(conn: &mut AsyncPgConnection, nsp: &str) -> Result<(), StoreError> {
let query = format!(
"drop schema if exists {nsp} cascade;\
create schema {nsp};",
nsp = nsp
);
Ok(conn.batch_execute(&query).await?)
}
/// Drop the schema `nsp` and all its contents if it exists
pub async fn drop_schema(conn: &mut AsyncPgConnection, nsp: &str) -> Result<(), StoreError> {
let query = format!("drop schema if exists {nsp} cascade;", nsp = nsp);
Ok(conn.batch_execute(&query).await?)
}
pub async fn migration_count(conn: &mut AsyncPgConnection) -> Result<usize, StoreError> {
use __diesel_schema_migrations as m;
if !table_exists(conn, NAMESPACE_PUBLIC, &MIGRATIONS_TABLE).await? {
return Ok(0);
}
m::table
.count()
.get_result(conn)
.await
.map(|n: i64| n as usize)
.map_err(StoreError::from)
}
pub async fn account_like(
conn: &mut AsyncPgConnection,
site: &Site,
) -> Result<HashSet<String>, StoreError> {
use table_stats as ts;
let names = ts::table
.filter(ts::deployment.eq(site.id))
.select((ts::table_name, ts::is_account_like))
.get_results::<(String, Option<bool>)>(conn)
.await
.optional()?
.unwrap_or_default()
.into_iter()
.filter_map(|(name, account_like)| {
if account_like == Some(true) {
Some(name)
} else {
None
}
})
.collect();
Ok(names)
}
pub async fn set_account_like(
conn: &mut AsyncPgConnection,
site: &Site,
table_name: &SqlName,
is_account_like: bool,
) -> Result<(), StoreError> {
use table_stats as ts;
insert_into(ts::table)
.values((
ts::deployment.eq(site.id),
ts::table_name.eq(table_name.as_str()),
ts::is_account_like.eq(is_account_like),
))
.on_conflict((ts::deployment, ts::table_name))
.do_update()
.set(ts::is_account_like.eq(is_account_like))
.execute(conn)
.await?;
Ok(())
}
pub async fn copy_account_like(
conn: &mut AsyncPgConnection,
src: &Site,
dst: &Site,
) -> Result<usize, StoreError> {
let src_nsp = ForeignServer::metadata_schema_in(&src.shard, &dst.shard);
let query = format!(
"insert into subgraphs.table_stats(deployment, table_name, is_account_like, last_pruned_block)
select $2 as deployment, ts.table_name, ts.is_account_like, ts.last_pruned_block
from {src_nsp}.table_stats ts
where ts.deployment = $1",
src_nsp = src_nsp
);
Ok(sql_query(query)
.bind::<Integer, _>(src.id)
.bind::<Integer, _>(dst.id)
.execute(conn)
.await?)
}
pub async fn set_last_pruned_block(
conn: &mut AsyncPgConnection,
site: &Site,
table_name: &SqlName,
last_pruned_block: BlockNumber,
) -> Result<(), StoreError> {
use table_stats as ts;
insert_into(ts::table)
.values((
ts::deployment.eq(site.id),
ts::table_name.eq(table_name.as_str()),
ts::last_pruned_block.eq(last_pruned_block),
))
.on_conflict((ts::deployment, ts::table_name))
.do_update()
.set(ts::last_pruned_block.eq(last_pruned_block))
.execute(conn)
.await?;
Ok(())
}
pub(crate) mod table_schema {
use super::*;
/// The name and data type for the column in a table. The data type is
/// in a form that it can be used in a `create table` statement
pub struct Column {
pub column_name: String,
pub data_type: String,
}
#[derive(QueryableByName)]
struct ColumnInfo {
#[diesel(sql_type = Text)]
column_name: String,
#[diesel(sql_type = Text)]
data_type: String,
#[diesel(sql_type = Text)]
udt_name: String,
#[diesel(sql_type = Text)]
udt_schema: String,
#[diesel(sql_type = Nullable<Text>)]
elem_type: Option<String>,
}
impl From<ColumnInfo> for Column {
fn from(ci: ColumnInfo) -> Self {
// See description of `data_type` in
// https://www.postgresql.org/docs/current/infoschema-columns.html
let data_type = match ci.data_type.as_str() {
"ARRAY" => format!(
"{}[]",
ci.elem_type.expect("array columns have an elem_type")
),
"USER-DEFINED" => format!("{}.{}", ci.udt_schema, ci.udt_name),
_ => ci.data_type.clone(),
};
Self {
column_name: ci.column_name,
data_type,
}
}
}
pub async fn columns(
conn: &mut AsyncPgConnection,
nsp: &str,
table_name: &str,
) -> Result<Vec<Column>, StoreError> {
const QUERY: &str = " \
select c.column_name::text, c.data_type::text,
c.udt_name::text, c.udt_schema::text, e.data_type::text as elem_type
from information_schema.columns c
left join information_schema.element_types e
on ((c.table_catalog, c.table_schema, c.table_name, 'TABLE', c.dtd_identifier)
= (e.object_catalog, e.object_schema, e.object_name, e.object_type, e.collection_type_identifier))
where c.table_schema = $1
and c.table_name = $2
order by c.ordinal_position";
Ok(sql_query(QUERY)
.bind::<Text, _>(nsp)
.bind::<Text, _>(table_name)
.get_results::<ColumnInfo>(conn)
.await?
.into_iter()
.map(|ci| ci.into())
.collect())
}
}
/// Return a SQL statement to create the foreign table
/// `{dst_nsp}.{table_name}` for the server `server` which has the same
/// schema as the (local) table `{src_nsp}.{table_name}`
pub async fn create_foreign_table(
conn: &mut AsyncPgConnection,
src_nsp: &str,
table_name: &str,
dst_nsp: &str,
server: &str,
) -> Result<String, StoreError> {
fn build_query(
columns: Vec<table_schema::Column>,
src_nsp: &str,
table_name: &str,
dst_nsp: &str,
server: &str,
) -> Result<String, std::fmt::Error> {
let mut query = String::new();
write!(
query,
"create foreign table \"{}\".\"{}\" (",
dst_nsp, table_name
)?;
for (idx, column) in columns.into_iter().enumerate() {
if idx > 0 {
write!(query, ", ")?;
}
write!(query, "\"{}\" {}", column.column_name, column.data_type)?;
}
writeln!(
query,
") server \"{}\" options(schema_name '{}');",
server, src_nsp
)?;
Ok(query)
}
let columns = table_schema::columns(conn, src_nsp, table_name).await?;
let query = build_query(columns, src_nsp, table_name, dst_nsp, server).map_err(|_| {
anyhow!(
"failed to generate 'create foreign table' query for {}.{}",
dst_nsp,
table_name
)
})?;
Ok(query)
}
/// Create a SQL statement unioning imported tables from all shards,
/// something like
///
/// ```sql
/// create view "dst_nsp"."src_table" as
/// select 'shard1' as shard, "col1", "col2" from "shard_shard1_subgraphs"."table_name"
/// union all
/// ...
/// ````
///
/// The list `shard_nsps` consists of pairs `(name, namespace)` where `name`
/// is the name of the shard and `namespace` is the namespace where the
/// `src_table` is mapped
pub async fn create_cross_shard_view(
conn: &mut AsyncPgConnection,
src_nsp: &str,
src_table: &str,
dst_nsp: &str,
shard_nsps: &[(&str, String)],
) -> Result<String, StoreError> {
fn build_query(
columns: &[table_schema::Column],
table_name: &str,
dst_nsp: &str,
shard_nsps: &[(&str, String)],
) -> Result<String, std::fmt::Error> {
let mut query = String::new();
write!(query, "create view \"{}\".\"{}\" as ", dst_nsp, table_name)?;
for (idx, (name, nsp)) in shard_nsps.iter().enumerate() {
if idx > 0 {
write!(query, " union all ")?;
}
write!(query, "select '{name}' as shard")?;
for column in columns {
write!(query, ", \"{}\"", column.column_name)?;
}
writeln!(query, " from \"{}\".\"{}\"", nsp, table_name)?;
}
Ok(query)
}
let columns = table_schema::columns(conn, src_nsp, src_table).await?;
let query = build_query(&columns, src_table, dst_nsp, shard_nsps).map_err(|_| {
anyhow!(
"failed to generate 'create foreign table' query for {}.{}",
dst_nsp,
src_table
)
})?;
Ok(query)
}
/// Checks in the database if a given index is valid.
pub(crate) async fn check_index_is_valid(
conn: &mut AsyncPgConnection,
schema_name: &str,
index_name: &str,
) -> Result<bool, StoreError> {
#[derive(Queryable, QueryableByName)]
struct ManualIndexCheck {
#[diesel(sql_type = Bool)]
is_valid: bool,
}
let query = "
select
i.indisvalid as is_valid
from
pg_class c
join pg_index i on i.indexrelid = c.oid
join pg_namespace n on c.relnamespace = n.oid
where
n.nspname = $1
and c.relname = $2";
let result = sql_query(query)
.bind::<Text, _>(schema_name)
.bind::<Text, _>(index_name)
.get_result::<ManualIndexCheck>(conn)
.await
.optional()
.map_err::<StoreError, _>(Into::into)?
.map(|check| check.is_valid);
Ok(matches!(result, Some(true)))
}
pub(crate) async fn indexes_for_table(
conn: &mut AsyncPgConnection,
schema_name: &str,
table_name: &str,
) -> Result<Vec<String>, StoreError> {
#[derive(Queryable, QueryableByName)]
struct IndexName {
#[diesel(sql_type = Text)]
#[diesel(column_name = indexdef)]
def: String,
}
let query = "
select
indexdef
from
pg_indexes
where
schemaname = $1
and tablename = $2
order by indexname";
let results = sql_query(query)
.bind::<Text, _>(schema_name)
.bind::<Text, _>(table_name)
.load::<IndexName>(conn)
.await
.map_err::<StoreError, _>(Into::into)?;
Ok(results.into_iter().map(|i| i.def).collect())
}
pub(crate) async fn drop_index(
conn: &mut AsyncPgConnection,
schema_name: &str,
index_name: &str,
) -> Result<(), StoreError> {
let query = format!("drop index concurrently {schema_name}.{index_name}");
sql_query(query)
.bind::<Text, _>(schema_name)
.bind::<Text, _>(index_name)
.execute(conn)
.await
.map_err::<StoreError, _>(Into::into)?;
Ok(())
}
/// Return by how much the slowest replica connected to the database `conn`
/// is lagging. The returned value has millisecond precision. If the
/// database has no replicas, return `0`
pub(crate) async fn replication_lag(conn: &mut AsyncPgConnection) -> Result<Duration, StoreError> {
#[derive(Queryable, QueryableByName)]
struct Lag {
#[diesel(sql_type = Nullable<Integer>)]
ms: Option<i32>,
}
let lag = sql_query(
"select (extract(epoch from max(greatest(write_lag, flush_lag, replay_lag)))*1000)::int as ms \
from pg_stat_replication",
)
.get_result::<Lag>(conn).await?;
let lag = lag
.ms
.map(|ms| if ms <= 0 { 0 } else { ms as u64 })
.unwrap_or(0);
Ok(Duration::from_millis(lag))
}
pub(crate) async fn cancel_vacuum(
conn: &mut AsyncPgConnection,
namespace: &Namespace,
) -> Result<(), StoreError> {
sql_query(
"select pg_cancel_backend(v.pid) \
from pg_stat_progress_vacuum v, \
pg_class c, \
pg_namespace n \
where v.relid = c.oid \
and c.relnamespace = n.oid \
and n.nspname = $1",
)
.bind::<Text, _>(namespace)
.execute(conn)
.await?;
Ok(())
}
pub(crate) async fn default_stats_target(conn: &mut AsyncPgConnection) -> Result<i32, StoreError> {
#[derive(Queryable, QueryableByName)]
struct Target {
#[diesel(sql_type = Integer)]
setting: i32,
}
let target =
sql_query("select setting::int from pg_settings where name = 'default_statistics_target'")
.get_result::<Target>(conn)
.await?;
Ok(target.setting)
}
pub(crate) async fn stats_targets(
conn: &mut AsyncPgConnection,
namespace: &Namespace,
) -> Result<BTreeMap<SqlName, BTreeMap<SqlName, i32>>, StoreError> {
use pg_attribute as a;
use pg_class as c;
use pg_namespace as n;
let targets = c::table
.inner_join(n::table)
.inner_join(a::table)
.filter(c::kind.eq("r"))
.filter(n::name.eq(namespace.as_str()))