Repository navigation
Expand file tree
/
Copy pathschema.sql
More file actions
1339 lines (1238 loc) · 65.4 KB
/
Copy pathschema.sql
File metadata and controls
1339 lines (1238 loc) · 65.4 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
-- schema.sql: the whole RAPID foundation schema, applied once to an empty
-- PostgreSQL 18 database:
--
-- psql -v ON_ERROR_STOP=1 -f database/schema.sql
--
-- Design authority: the foundation plan (PLAN.md v5, 2026-10-20), Part 1.
-- Every published product row names the job that wrote it; the job names
-- its run. Published rows are never changed. `official` says, per product
-- and scientific key, which job's rows are official, and which run is live
-- per survey and dataset; one `<table>_official` view per product table
-- shows the official rows. Nothing here is ever deleted.
--
-- Scientific keys (every key is stored in `official.key` as a jsonb object
-- whose names are the column names below, e.g.
-- {"dataset": "d", "expid": 7, "sca": 3, "fid": 1}):
--
-- exposures dataset, expid
-- l2files, diffimages, sources,
-- packets dataset, expid, sca, fid
-- refimages dataset, field, fid
-- psfs dataset, fid, sca, kind (science)
-- dataset, psf_set, fid, sca, kind (reference)
-- catalogs dataset, name, version
--
-- An `expid` is the integer `exposure_ids` handed out for the producer's
-- own name for the exposure within its dataset (rapid_docs exposure-id,
-- Ben's ruling of 2026-11-14); it carries no meaning, and the name beside it
-- says which exposure it is.
--
-- A set (a reference image, a source set, a packet set) is identified by its
-- job: its child rows (refimimages, refimcatalogs, refimmeta, sources,
-- objects, memberships, packets, forced, external) carry the set's job and
-- their own id, and their views join through the set's open binding on
-- (product, job).
-- The object catalog has no binding of its own (plan amendment 2026-10-24):
-- a difference job's founded objects and memberships ride on its source
-- set's binding.
--
-- Columns are dev's names and types (database/schema/rapidOpsTables.sql and
-- rapidOpsSourcesTable.sql on dev) for the columns the team's queries read,
-- without vbest, version or ppid: a binding replaces vbest, a job replaces
-- version, and a run has one pipeline.
--------------------------------------------------------------------------------
-- Static tables (dev's columns and insert data)
--------------------------------------------------------------------------------
CREATE TABLE filters (
fid smallint PRIMARY KEY, -- FITS-header keyword: FILTERID
filter character varying(16) NOT NULL UNIQUE -- FITS-header keyword: FILTER
);
INSERT INTO filters (fid, filter) VALUES
(1, 'F184'), (2, 'H158'), (3, 'J129'), (4, 'K213'),
(5, 'R062'), (6, 'Y106'), (7, 'Z087'), (8, 'W146');
CREATE TABLE scas (
sca smallint PRIMARY KEY CHECK (sca BETWEEN 1 AND 18)
);
INSERT INTO scas (sca) SELECT generate_series(1, 18);
CREATE TABLE surveys (
program_id integer PRIMARY KEY, -- Roman program number, negative for simulations
program_title character varying(255) NOT NULL,
category character varying(6), -- CCS, GAS, GI, CAL, COM, ENG, ..., SIM
created timestamp without time zone DEFAULT now() NOT NULL
);
-- One negative id per simulation, the commissioning programs (Azalee, 2026-11-10), and the calibration, core community (CCS) and general astrophysics (GAS) programs, titled from STScI's Roman program pages (Azalee, 2026-11-17).
INSERT INTO surveys (program_id, program_title, category) VALUES
(-1, 'SOCSIMS GBTDS', 'SIM'),
(-2, 'OpenUniverse', 'SIM'),
(-3, 'RimTimSims', 'SIM'),
(-4, 'SOCSIMS HLWAS', 'SIM'),
(1006, 'WFI photometric and astrometric performance', 'COM'),
(1007, 'WFI Bandpass Uniformity', 'COM'),
(1008, 'WFI Photometric Stability', 'COM'),
(1018, 'Crowded Field Time Series', 'COM'),
(1020, 'Backgrounds', 'COM'),
(1043, 'Ice Characterization (Imaging)', 'COM'),
(1049, 'RAPID Implementation', 'COM'),
(1053, 'High-Extinction Guiding Opportunity Activity', 'COM'),
(1054, 'Early GBTDS observations', 'COM'),
(1055, 'Time Series of a Second Crowded Field', 'COM'),
(1062, 'Early Demonstration of HLTDS observation strategy and performance', 'COM'),
(1083, 'Ecliptic Plane Moving Object Study', 'COM'),
(1064, 'WFI dark current calibration', 'CAL'),
(1065, 'WFI photometric and astrometric performance', 'CAL'),
(1066, 'WFI flat field and linearity calibration', 'CAL'),
(1067, 'WFI Photometric Stability', 'CAL'),
(1068, 'WFI Photometry Spatial Uniformity', 'CAL'),
(1069, 'Flux Calibration', 'CAL'),
(1070, 'CAL Bandpass Variation Characterization', 'CAL'),
(1071, 'Roman WFI Sub-pixel Response Calibration', 'CAL'),
(1072, 'Roman WFI Cross Calibration Program', 'CAL'),
(1073, 'Ice Characterization (Imaging)', 'CAL'),
(1074, 'Ice decontamination and rebaseline', 'CAL'),
(1075, 'Count-rate dependent nonlinearity (Direct)', 'CAL'),
(1076, 'Lamp-On/Lamp-Off (LOLO) Calibration', 'CAL'),
(1077, 'Dispersion Solution - Grism', 'CAL'),
(1078, 'Dispersion Solution - Prism', 'CAL'),
(1079, 'Edge/Trace mapping, Spectral PSF, and Relative flux - Grism', 'CAL'),
(1080, 'Edge/Trace mapping, Spectral PSF, and Relative flux - Prism', 'CAL'),
(1081, 'Absolute flux and spectrophotometric stability - Grism', 'CAL'),
(1082, 'Absolute flux and spectrophotometric stability - Prism', 'CAL'),
(1400, 'Roman High-Latitude Wide-Area Survey', 'CCS'),
(1410, 'Roman High Latitude Time Domain Survey', 'CCS'),
(1420, 'Galactic Bulge Time Domain Survey', 'CCS'),
(1430, 'Roman Galactic Plane Survey', 'GAS'),
(2000, 'The First Complete Census of Stars in Key Globular Clusters', 'GAS'),
(2001, 'Roman eXtreme Deep Field (RXDF): transformative science at Roman''s limit', 'GAS'),
(2002, 'A Legacy Survey of Andromeda and Triangulum with Roman', 'GAS'),
(2003, 'GRACE: The Grism Reionization And Cosmic Evolution Survey', 'GAS'),
(2004, 'The Roman-Kepler Legacy Survey', 'GAS');
CREATE TABLE fields (
field integer PRIMARY KEY, -- Roman tessellation index for (ra0,dec0)
hp6 integer NOT NULL,
hp9 integer NOT NULL,
ra1 double precision NOT NULL, -- lower-left corner
dec1 double precision NOT NULL,
ra2 double precision NOT NULL, -- lower-right corner
dec2 double precision NOT NULL,
ra3 double precision NOT NULL, -- upper-right corner
dec3 double precision NOT NULL,
ra4 double precision NOT NULL, -- upper-left corner
dec4 double precision NOT NULL,
ra0 double precision NOT NULL, -- centre
dec0 double precision NOT NULL
);
--------------------------------------------------------------------------------
-- Bookkeeping: datasets, runs, jobs, job_inputs, official
--------------------------------------------------------------------------------
-- A dataset is the set of inputs whose objects are shared (rapid_docs
-- datasets): registered once, by `rapidpipe dataset create`, before its first
-- run. runs.dataset references it, so `run create` refuses a name it does not
-- hold and a misspelt name cannot found an object catalog of its own. A run
-- records its own survey and overlay (Russ Laher, 2026-11-24).
CREATE TABLE datasets (
name text PRIMARY KEY, -- the dataset every scientific key carries
description text NOT NULL,
created timestamp with time zone DEFAULT now() NOT NULL
);
-- Flight is one dataset for every program (rapid_docs datasets, Flight data).
INSERT INTO datasets (name, description) VALUES
('flight', 'Flight, every program; the first, program 1049');
-- A run is a recipe someone launched for a reason, for one survey and dataset.
CREATE TABLE runs (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
who text NOT NULL,
created timestamp with time zone DEFAULT now() NOT NULL,
why text NOT NULL,
revision text NOT NULL, -- code revision
settings jsonb NOT NULL, -- the full settings body, resolved
layers jsonb, -- its hash and the layers it was resolved from (rapidpipe.foundation.layers); null on earlier runs
survey text NOT NULL,
dataset text NOT NULL REFERENCES datasets (name),
stopped timestamp with time zone, -- when `rapidpipe run stop` closed the run to further jobs; null while it takes them
stopped_by text, -- who stopped it
stopped_why text, -- and why
CHECK ((stopped IS NULL) = (stopped_by IS NULL) AND (stopped IS NULL) = (stopped_why IS NULL))
);
-- A job is one execution of one stage under a run.
CREATE TABLE jobs (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
run bigint NOT NULL REFERENCES runs (id),
stage text NOT NULL,
scope jsonb NOT NULL,
state text DEFAULT 'launched' NOT NULL
CHECK (state IN ('launched', 'running', 'published', 'failed')),
image_digest text, -- none for a local checkout
instance_type text, -- the EC2 instance type it ran on; null when unknown
settings_hash text,
published_attempt integer,
manifest jsonb,
refusal text, -- why the automatic binding was refused, why Batch ended it unrecorded, or why the stage failed (a named failure's message, any other exception's type and message)
launched timestamp with time zone DEFAULT now() NOT NULL,
started timestamp with time zone,
ended timestamp with time zone,
exit_code integer,
kafka_published_at timestamp with time zone, -- every packet of its set acknowledged by the topic
kafka_partition integer, -- the partition its set's acknowledgements named
kafka_first_offset bigint, -- the first and last offsets they gave; other sets interleave between
kafka_last_offset bigint,
batch_job_id text, -- the Batch job of its latest submission; null for a local job
attempts jsonb DEFAULT '{}'::jsonb NOT NULL, -- one entry per Batch submission, keyed by Batch job id (rapidpipe.foundation.attempts)
CHECK ((state = 'published') = (published_attempt IS NOT NULL))
);
CREATE INDEX jobs_run_idx ON jobs (run);
CREATE INDEX jobs_state_idx ON jobs (state);
-- Official bindings: a product binding (product, key, job) or the live
-- binding (survey, dataset, run). Both directions of a change are
-- attributed on the row, so a change group's effect is the rows it opened
-- and the rows it closed.
CREATE SEQUENCE change_groups AS bigint;
CREATE TABLE official (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
product text,
key jsonb,
job bigint REFERENCES jobs (id),
run bigint REFERENCES runs (id),
survey text,
dataset text,
opened_group bigint NOT NULL,
opened_by text NOT NULL,
opened_at timestamp with time zone DEFAULT now() NOT NULL,
opened_why text NOT NULL,
closed_group bigint,
closed_by text,
closed_at timestamp with time zone,
closed_why text,
CHECK (
(job IS NOT NULL AND product IS NOT NULL AND key IS NOT NULL AND run IS NULL)
OR (run IS NOT NULL AND survey IS NOT NULL AND dataset IS NOT NULL
AND job IS NULL AND product IS NULL AND key IS NULL)),
CHECK (
(closed_at IS NULL AND closed_group IS NULL AND closed_by IS NULL AND closed_why IS NULL)
OR (closed_at IS NOT NULL AND closed_group IS NOT NULL AND closed_by IS NOT NULL
AND closed_why IS NOT NULL))
);
CREATE UNIQUE INDEX official_open_product_key ON official (product, key)
WHERE closed_at IS NULL AND job IS NOT NULL;
CREATE UNIQUE INDEX official_open_live ON official (survey, dataset)
WHERE closed_at IS NULL AND run IS NOT NULL;
CREATE INDEX official_open_product_job ON official (product, job)
WHERE closed_at IS NULL;
CREATE INDEX official_opened_group_idx ON official (opened_group);
CREATE INDEX official_closed_group_idx ON official (closed_group);
-- A job's inputs, chosen at launch, or recorded at its read by the publisher
-- from the manifest's `reads` (a difference job's object catalog: one
-- `objects` row per founding job whose objects it read; a packet job's
-- members and forced window):
-- input a set the job reads (product, key, source_job)
-- expects per output key, the open binding the job expects to replace
-- (binding), or none (binding null)
-- SMDC also holds `excludes` rows, the retired set chain's (2026-10-24).
CREATE TABLE job_inputs (
job bigint NOT NULL REFERENCES jobs (id),
kind text NOT NULL CHECK (kind IN ('input', 'expects')),
product text NOT NULL,
key jsonb NOT NULL,
source_job bigint REFERENCES jobs (id),
binding bigint REFERENCES official (id)
);
CREATE INDEX job_inputs_job_idx ON job_inputs (job, kind);
CREATE INDEX job_inputs_source_job_idx ON job_inputs (source_job);
-- The exposure ids: the producer's own name for an exposure, unique within a
-- dataset, and the integer the database handed out for it (rapid_docs
-- exposure-id). Bookkeeping, as jobs and official are, not a product: no job
-- writes it and nothing binds it. The loop resolves the names of the stems it
-- is about to take in before it plans (rapidpipe.foundation.loop.exposure_ids:
-- INSERT ... ON CONFLICT DO NOTHING, then SELECT, committed before any intake
-- is submitted) and puts the id in each intake's scope; the intake checks its
-- scope's id against this table; `rapidpipe exposure id` resolves one by hand.
-- Rows are never changed or deleted, so a name keeps its id across passes,
-- reruns and runs of its dataset; one identity serves every dataset, so no two
-- exposures share an id the identity handed out, and the same exposure taken
-- into two datasets has two. The key is (dataset, expid), not expid alone: an
-- exposure a database stored before this table keeps the id it had
-- (2026-11-20-exposure-ids.sql), and those ids are unique within a dataset and
-- repeat across datasets, so every join to this table matches on dataset too.
CREATE TABLE exposure_ids (
expid integer GENERATED ALWAYS AS IDENTITY,
dataset text NOT NULL,
name text NOT NULL, -- the producer's name for the exposure (loop.stem_ids)
created timestamp with time zone DEFAULT now() NOT NULL,
PRIMARY KEY (dataset, expid),
UNIQUE (dataset, name)
);
--------------------------------------------------------------------------------
-- Scientific tables: every row names its job
--------------------------------------------------------------------------------
CREATE TABLE exposures (
job bigint NOT NULL REFERENCES jobs (id),
dataset text NOT NULL,
expid integer NOT NULL, -- exposure_ids' id for the producer's name of the exposure
dateobs timestamp without time zone NOT NULL, -- DATE-OBS
exptime real NOT NULL, -- EXPTIME
program_id integer, -- the file's PROGRAM, or the dataset's [intake] program_id
-- The Roman observation identifiers, ASDF meta.observation, dev's
-- migration 20261015 (5ba96e38): null for a source that has none.
execution_plan smallint, -- EXECPLAN
pass smallint, -- PASS
segment smallint, -- SEGMENT
observation smallint, -- OBSERVTN
visit smallint, -- VISIT
exposure smallint, -- EXPOSURE
visit_id character varying(19), -- VISIT_ID
observation_id character varying(28), -- OBS_ID
UNIQUE (dataset, expid, job)
);
-- l2files with dev's l2filemeta centre and corners folded in.
CREATE TABLE l2files (
rid integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
job bigint NOT NULL REFERENCES jobs (id),
dataset text NOT NULL,
expid integer NOT NULL,
sca smallint NOT NULL REFERENCES scas (sca), -- SCA-NUM
fid smallint NOT NULL REFERENCES filters (fid),
field integer NOT NULL, -- Roman tessellation index for (ra0,dec0)
hp6 integer NOT NULL,
hp9 integer NOT NULL,
dateobs timestamp without time zone NOT NULL, -- DATE-OBS
mjdobs double precision NOT NULL, -- MJD-OBS
exptime real NOT NULL, -- EXPTIME
infobits integer DEFAULT 0 NOT NULL,
filename character varying(255) NOT NULL,
checksum character varying(32) NOT NULL, -- MD5 of the file
status smallint DEFAULT 0 NOT NULL,
crval1 double precision NOT NULL,
crval2 double precision NOT NULL,
crpix1 real NOT NULL,
crpix2 real NOT NULL,
cd11 double precision NOT NULL,
cd12 double precision NOT NULL,
cd21 double precision NOT NULL,
cd22 double precision NOT NULL,
ra double precision NOT NULL, -- RA_TARG
dec double precision NOT NULL, -- DEC_TARG
zptmag real,
limmag real, -- 5-sigma limiting magnitude, computed before publish
overlapfields integer[] DEFAULT '{}'::integer[] NOT NULL,
ra0 double precision NOT NULL, -- centre
dec0 double precision NOT NULL,
ra1 double precision NOT NULL, -- corners
dec1 double precision NOT NULL,
ra2 double precision NOT NULL,
dec2 double precision NOT NULL,
ra3 double precision NOT NULL,
dec3 double precision NOT NULL,
ra4 double precision NOT NULL,
dec4 double precision NOT NULL,
created timestamp without time zone DEFAULT now() NOT NULL,
pixel_area_map text, -- PAMREF, the map's CRDS name; null when none
mjdmid double precision, -- MJD-AVG, the exposure's mid-time; null: stored before the column
UNIQUE (dataset, expid, sca, fid, job)
);
CREATE INDEX l2files_job_idx ON l2files (job);
CREATE INDEX l2files_field_idx ON l2files (field);
CREATE INDEX l2files_mjdobs_idx ON l2files (mjdobs);
CREATE INDEX l2files_overlapfields_idx ON l2files USING gin (overlapfields);
-- refimages with dev's refimmeta columns the team reads folded in.
CREATE TABLE refimages (
rfid integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
job bigint NOT NULL REFERENCES jobs (id),
dataset text NOT NULL,
field integer NOT NULL,
fid smallint NOT NULL REFERENCES filters (fid),
filename character varying(255) NOT NULL,
checksum character varying(32) NOT NULL,
status smallint DEFAULT 0 NOT NULL,
infobits integer DEFAULT 0 NOT NULL,
nframes smallint, -- images in the stack
cov5percent real, -- coverage-depth QA metric
created timestamp without time zone DEFAULT now() NOT NULL,
UNIQUE (dataset, field, fid, job)
);
CREATE INDEX refimages_job_idx ON refimages (job);
-- Reference membership: which L2 rows built a reference (child of refimages).
CREATE TABLE refimimages (
job bigint NOT NULL REFERENCES jobs (id), -- the reference's job
rfid integer NOT NULL, -- the reference row
rid integer NOT NULL, -- an L2 row it was built from
UNIQUE (job, rfid, rid)
);
-- A reference's mosaic catalogs, keyed by reference and catalog type
-- (child of refimages).
CREATE TABLE refimcatalogs (
rfcatid integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
job bigint NOT NULL REFERENCES jobs (id), -- the reference's job
rfid integer NOT NULL,
cattype smallint NOT NULL,
filename character varying(255) NOT NULL,
checksum character varying(32) NOT NULL,
UNIQUE (job, rfid, cattype)
);
-- dev's RefImMeta: a reference's quality measurements, dev's columns by name
-- and type (child of refimages; rapid_docs decision-refimmeta). hp6 and hp9 are
-- of the reference's centre, mjdobsmin and mjdobsmax of its coadded frames;
-- npucatsources is null, since no Photutils catalog of the reference is made.
CREATE TABLE refimmeta (
rfid integer PRIMARY KEY REFERENCES refimages (rfid),
job bigint NOT NULL REFERENCES jobs (id), -- the reference's job
field integer NOT NULL, -- Roman tessellation index for (ra0,dec0)
hp6 integer NOT NULL, -- level-6 HEALPix index (NESTED) for (ra0,dec0)
hp9 integer NOT NULL, -- level-9 HEALPix index (NESTED) for (ra0,dec0)
fid smallint NOT NULL REFERENCES filters (fid),
nframes smallint NOT NULL, -- images in the stack
mjdobsmin double precision NOT NULL, -- minimum MJD of the input images
mjdobsmax double precision NOT NULL, -- maximum MJD of the input images
npixnan integer NOT NULL, -- NaN pixels in the reference image
clmean real NOT NULL, -- pixel mean after clipping
clstddev real NOT NULL, -- pixel standard deviation after clipping and reinflating
clnoutliers integer NOT NULL, -- pixels discarded in clipping
gmedian real NOT NULL, -- global pixel median
datascale real NOT NULL, -- global robust spread, 0.5*(p84-p16)
gmin real NOT NULL, -- global minimum pixel value
gmax real NOT NULL, -- global maximum pixel value
cov5percent real NOT NULL, -- coverage-depth QA metric (depth of at least 5)
medncov real NOT NULL, -- median of the depth-of-coverage image
medpixunc real NOT NULL, -- median of the uncertainty image
fwhmmedpix real NOT NULL, -- FWHM_IMAGE of the SExtractor catalog's point sources [pixels]
fwhmminpix real NOT NULL, -- minimum FWHM_IMAGE [pixels]
fwhmmaxpix real NOT NULL, -- maximum FWHM_IMAGE [pixels]
nsxcatsources integer NOT NULL, -- sources in the SExtractor catalog
npucatsources integer -- sources in the Photutils catalog (null: none is made)
);
CREATE INDEX refimmeta_job_idx ON refimmeta (job);
CREATE INDEX refimmeta_field_idx ON refimmeta (field);
CREATE INDEX refimmeta_hp6_idx ON refimmeta (hp6);
CREATE INDEX refimmeta_hp9_idx ON refimmeta (hp9);
CREATE INDEX refimmeta_fid_idx ON refimmeta (fid);
CREATE INDEX refimmeta_nframes_idx ON refimmeta (nframes);
CREATE INDEX refimmeta_cov5percent_idx ON refimmeta (cov5percent);
CREATE TABLE diffimages (
pid integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
job bigint NOT NULL REFERENCES jobs (id),
dataset text NOT NULL,
expid integer NOT NULL,
sca smallint NOT NULL REFERENCES scas (sca),
fid smallint NOT NULL REFERENCES filters (fid),
rid integer, -- the L2 row it differenced (lineage, not a join key)
rfid integer, -- the reference row it used (lineage, not a join key)
field integer NOT NULL,
jd double precision, -- Julian date of start of image
ra0 double precision NOT NULL, -- centre
dec0 double precision NOT NULL,
ra1 double precision NOT NULL, -- corners
dec1 double precision NOT NULL,
ra2 double precision NOT NULL,
dec2 double precision NOT NULL,
ra3 double precision NOT NULL,
dec3 double precision NOT NULL,
ra4 double precision NOT NULL,
dec4 double precision NOT NULL,
infobitssci integer NOT NULL,
infobitsref integer NOT NULL,
filename text NOT NULL,
checksum character varying(32),
status smallint DEFAULT 0 NOT NULL,
created timestamp without time zone DEFAULT now() NOT NULL,
UNIQUE (dataset, expid, sca, fid, job)
);
CREATE INDEX diffimages_job_idx ON diffimages (job);
CREATE INDEX diffimages_field_idx ON diffimages (field);
CREATE INDEX diffimages_jd_idx ON diffimages (jd);
CREATE TABLE psfs (
psfid integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
job bigint NOT NULL REFERENCES jobs (id),
dataset text NOT NULL,
fid smallint NOT NULL REFERENCES filters (fid),
sca smallint NOT NULL REFERENCES scas (sca),
kind text NOT NULL CHECK (kind IN ('science', 'reference')), -- a science image's PSF or a reference's
psf_set text, -- the imported set a reference PSF belongs to; none for a science PSF
filename character varying(255) NOT NULL,
checksum character varying(32) NOT NULL,
status smallint DEFAULT 0 NOT NULL,
created timestamp without time zone DEFAULT now() NOT NULL,
CHECK ((kind = 'reference') = (psf_set IS NOT NULL)),
UNIQUE NULLS NOT DISTINCT (dataset, psf_set, fid, sca, kind, job)
);
CREATE INDEX psfs_job_idx ON psfs (job);
-- One source set per (dataset, expid, sca, fid) and job; a source is
-- (source set job, sid). The columns the alert provider's Source reads.
-- Split as dev's sources are, by observation date and SCA: the rows live in
-- child tables that inherit this one, one per UT day of mjdobs and SCA,
-- sources_<yyyymmdd>_<sca>, made by create_catalog_child (below) when a set
-- first needs it and loaded by COPY (rapidpipe.foundation.publish). The parent
-- holds no rows: sources_holds_no_rows, which its children do not inherit.
CREATE TABLE sources (
job bigint NOT NULL REFERENCES jobs (id), -- the source set's job
sid bigint NOT NULL, -- the source's id within its set
dataset text NOT NULL,
expid integer NOT NULL,
sca smallint NOT NULL,
fid smallint NOT NULL,
pid integer NOT NULL, -- the difference-image row it was found on
id integer NOT NULL, -- photutils catalog id
isdiffpos boolean DEFAULT true NOT NULL,
field integer NOT NULL,
hp6 integer NOT NULL,
hp9 integer NOT NULL,
mjdobs double precision NOT NULL,
ra double precision NOT NULL,
dec double precision NOT NULL,
xfit real NOT NULL,
yfit real NOT NULL,
xerr real NOT NULL,
yerr real NOT NULL,
fluxfit real NOT NULL,
fluxerr real NOT NULL,
flags smallint NOT NULL, -- photutils' fit bits (to 1 << 11); RAPID's 1 << 14 = 16384 the bright
-- footprint, informational: eligible is flags & ~16384 = 0 (rapidpipe.products.diffimage)
qfit real NOT NULL,
cfit real NOT NULL,
redchi real DEFAULT 0.0 NOT NULL,
npixfit smallint NOT NULL,
sharpness real NOT NULL,
roundness1 real NOT NULL,
roundness2 real NOT NULL,
peak real NOT NULL,
sumrat real, -- dev's pixel columns (rapidpipe.science.difference.columns),
nneg smallint, -- on a 5 x 5 stamp after a 64 px local background; null where
nbad smallint, -- unmeasurable (stamp off the image) and on rows made before them
rb real, -- dev's RuBR-AT score (rapidpipe.science.difference.realbogus), SFFT
rblabel smallint, -- only; label 1 real, 0 bogus, -1 not scored; null: not computed
UNIQUE (job, sid)
);
ALTER TABLE sources ADD CONSTRAINT sources_holds_no_rows CHECK (false) NO INHERIT;
-- A source set's SExtractor sources: dev's xsources, every source of the SFFT
-- difference image's positive and negative SExtractor catalogs that passes
-- dev's loader cuts, with the extra columns dev computes on them (rapidpipe.
-- science.difference.xsources). Written by the difference job beside its
-- sources, a child of its source set: an xsource is (source set job, xsid), and
-- it is official when its set is (xsources_official). Split as sources is, into
-- child tables that inherit this one, xsources_<yyyymmdd>_<sca>, loaded by COPY;
-- the parent holds no rows.
CREATE TABLE xsources (
job bigint NOT NULL REFERENCES jobs (id), -- the source set's job
xsid bigint NOT NULL, -- the xsource's id within its set
dataset text NOT NULL,
expid integer NOT NULL,
sca smallint NOT NULL,
fid smallint NOT NULL,
pid integer NOT NULL, -- the difference-image row it was found on
num integer NOT NULL, -- NUMBER in its SExtractor catalog (one per sign)
isdiffpos boolean DEFAULT true NOT NULL, -- t: the positive catalog, f: the negative
ra double precision NOT NULL, -- ALPHAWIN_J2000
dec double precision NOT NULL, -- DELTAWIN_J2000
x real NOT NULL, -- XWIN_IMAGE (one-based)
y real NOT NULL, -- YWIN_IMAGE (one-based)
fluxap real NOT NULL, -- FLUX_APER, FLUX_APER_1 ... FLUX_APER_5
fluxap1 real NOT NULL,
fluxap2 real NOT NULL,
fluxap3 real NOT NULL,
fluxap4 real NOT NULL,
fluxap5 real NOT NULL,
fluxerrap real NOT NULL, -- FLUXERR_APER, FLUXERR_APER_1 ... FLUXERR_APER_5
fluxerrap1 real NOT NULL,
fluxerrap2 real NOT NULL,
fluxerrap3 real NOT NULL,
fluxerrap4 real NOT NULL,
fluxerrap5 real NOT NULL,
awinworld real NOT NULL, -- AWIN_WORLD
bwinworld real NOT NULL, -- BWIN_WORLD
awinimage real NOT NULL, -- AWIN_IMAGE
bwinimage real NOT NULL, -- BWIN_IMAGE
fwhmimage real NOT NULL, -- FWHM_IMAGE
classstar real NOT NULL, -- CLASS_STAR
flags smallint NOT NULL, -- SExtractor's FLAGS
field integer NOT NULL,
hp6 integer NOT NULL,
hp9 integer NOT NULL,
mjdobs double precision NOT NULL,
rb real, -- dev's extra columns; null: not computed
nneg smallint,
nbad smallint,
sumrat real,
sharpness real,
roundness1 real,
roundness2 real,
fluxfit real,
snrfit real,
redchi real,
npixfit smallint,
flagsfit smallint,
cfit real,
rblabel smallint,
errx2win real, -- ERRX2WIN_IMAGE, pixel^2; null: published before the column
erry2win real, -- ERRY2WIN_IMAGE
errxywin real, -- ERRXYWIN_IMAGE
errawinworld real, -- ERRAWIN_WORLD, deg
errbwinworld real, -- ERRBWIN_WORLD, deg
errthetawinworld real, -- ERRTHETAWIN_WORLD, deg from the RA axis
x2win real, -- X2WIN_IMAGE, pixel^2; null: published before the column
y2win real, -- Y2WIN_IMAGE
xywin real, -- XYWIN_IMAGE
UNIQUE (job, xsid)
);
ALTER TABLE xsources ADD CONSTRAINT xsources_holds_no_rows CHECK (false) NO INHERIT;
-- The object catalog, per run and field, append-only (plan amendment
-- 2026-10-24): an object is founded by the difference job whose unmatched
-- source it was; its id is the position hash of that source. The publisher
-- reconciles a job's founded objects against those founded since its read
-- (rapidpipe.foundation.catalog), so no two jobs found one object. An object
-- names no survey: surveys overlap, and its members may come from more than
-- one (objects-without-survey); its run records the survey it was run for.
--
-- objects, memberships, packets and forced keep their rows in child tables
-- that inherit them, one per field, <parent>_<field>, as dev's astroobjects
-- and merges are split by field; each row is filed under its object's field,
-- so an object's row, memberships, forced photometry and packet rows share
-- one field's children. The parents hold no rows (create_field_child below).
CREATE TABLE objects (
job bigint NOT NULL REFERENCES jobs (id), -- the difference job that founded it
aid bigint NOT NULL,
dataset text NOT NULL,
field integer NOT NULL,
ra0 double precision NOT NULL, -- founding position, the origin of its members' offsets
dec0 double precision NOT NULL,
UNIQUE (dataset, field, aid, job)
);
CREATE INDEX objects_job_idx ON objects (job, aid);
CREATE INDEX objects_field_idx ON objects (dataset, field);
ALTER TABLE objects ADD CONSTRAINT objects_holds_no_rows CHECK (false) NO INHERIT;
-- A difference job's memberships: each of its sources (source_job, sid) in
-- each object (object_job, aid) it matched, with the source's share of the
-- object's sufficient sums: its offsets from the object's founding position
-- (degrees, RA wrapped into [-180, 180)) and its fluxfit
-- (rapidpipe.science.statistics.sums). source_job is the job itself.
CREATE TABLE memberships (
job bigint NOT NULL REFERENCES jobs (id), -- the difference job
object_job bigint NOT NULL REFERENCES jobs (id), -- the object's founding job
aid bigint NOT NULL,
field integer NOT NULL, -- the object's field
source_job bigint NOT NULL REFERENCES jobs (id), -- the source set's job
sid bigint NOT NULL,
dra double precision NOT NULL,
ddec double precision NOT NULL,
flux double precision NOT NULL,
UNIQUE (job, object_job, aid, source_job, sid)
);
CREATE INDEX memberships_source_idx ON memberships (source_job, sid);
CREATE INDEX memberships_object_idx ON memberships (object_job, aid);
ALTER TABLE memberships ADD CONSTRAINT memberships_holds_no_rows CHECK (false) NO INHERIT;
-- The xsources' object catalog, dev's XAstroObjects (crossMatchXSources.py):
-- objects and memberships as above, of the xsources of the SFFT difference
-- image (both signs) instead of the sources. An xobject is founded by the
-- difference job whose unmatched xsource it was, with that xsource's
-- position, its aperture flux (flux0, dev's) and its field; the publisher
-- reconciles it as it does an object. Split by field like objects.
CREATE TABLE xobjects (
job bigint NOT NULL REFERENCES jobs (id), -- the difference job that founded it
xaid bigint NOT NULL,
dataset text NOT NULL,
field integer NOT NULL,
ra0 double precision NOT NULL, -- founding position, the origin of its members' offsets
dec0 double precision NOT NULL,
flux0 real NOT NULL, -- the founding xsource's fluxap
UNIQUE (dataset, field, xaid, job)
);
CREATE INDEX xobjects_job_idx ON xobjects (job, xaid);
CREATE INDEX xobjects_field_idx ON xobjects (dataset, field);
ALTER TABLE xobjects ADD CONSTRAINT xobjects_holds_no_rows CHECK (false) NO INHERIT;
-- A difference job's xmemberships, dev's XMerges: each of its xsources
-- (source_job, xsid) in each xobject (object_job, xaid) it matched, with the
-- xsource's offsets from the xobject's founding position (degrees, RA wrapped
-- into [-180, 180)) and its fluxap, the xobject's sufficient sums.
CREATE TABLE xmemberships (
job bigint NOT NULL REFERENCES jobs (id), -- the difference job
object_job bigint NOT NULL REFERENCES jobs (id), -- the xobject's founding job
xaid bigint NOT NULL,
field integer NOT NULL, -- the xobject's field
source_job bigint NOT NULL REFERENCES jobs (id), -- the source set's job, whose xsources these are
xsid bigint NOT NULL,
dra double precision NOT NULL,
ddec double precision NOT NULL,
flux double precision NOT NULL,
UNIQUE (job, object_job, xaid, source_job, xsid)
);
CREATE INDEX xmemberships_source_idx ON xmemberships (source_job, xsid);
CREATE INDEX xmemberships_object_idx ON xmemberships (object_job, xaid);
ALTER TABLE xmemberships ADD CONSTRAINT xmemberships_holds_no_rows CHECK (false) NO INHERIT;
-- The packet index: one row per (packet set, source); the Avro bytes are at
-- (filename, byte_offset, byte_length).
CREATE TABLE packets (
job bigint NOT NULL REFERENCES jobs (id), -- the packet set's job
dataset text NOT NULL, -- packet set key
expid integer NOT NULL,
sca smallint NOT NULL,
fid smallint NOT NULL,
object_job bigint NOT NULL REFERENCES jobs (id), -- the object's founding job (an associate job's set before 2026-10-24)
aid bigint NOT NULL,
field integer NOT NULL, -- the object's field
source_job bigint NOT NULL REFERENCES jobs (id), -- the source set
sid bigint NOT NULL,
trigger_mjd double precision NOT NULL, -- the packet's midpointMjd: the trigger's exposure start plus half its exposure time (2026-11-16)
band character varying(16) NOT NULL,
schema_version text NOT NULL,
filename text NOT NULL,
byte_offset bigint NOT NULL,
byte_length bigint NOT NULL,
UNIQUE (job, source_job, sid)
);
CREATE INDEX packets_object_idx ON packets (object_job, aid);
ALTER TABLE packets ADD CONSTRAINT packets_holds_no_rows CHECK (false) NO INHERIT;
-- Forced photometry rows of a packet set (dev's FORCED_TABLE_DTYPE).
CREATE TABLE forced (
job bigint NOT NULL REFERENCES jobs (id), -- the packet set's job
forced_id bigint NOT NULL, -- hash of image, object and position
aid bigint NOT NULL,
field integer NOT NULL, -- the object's field
rid integer NOT NULL, -- the L2 row measured
pid integer NOT NULL, -- the difference-image row measured
expid integer NOT NULL,
sca smallint NOT NULL,
fid smallint NOT NULL,
band character varying(16) NOT NULL,
mjdobs double precision NOT NULL,
ra double precision NOT NULL,
dec double precision NOT NULL,
x real NOT NULL,
y real NOT NULL,
psf_flux double precision,
psf_fluxerr double precision,
science_flux double precision,
science_fluxerr double precision,
flags integer NOT NULL,
time_proc double precision NOT NULL,
mjdmid double precision, -- its image's l2files.mjdmid; null: measured before the column
UNIQUE (job, forced_id)
);
CREATE INDEX forced_aid_idx ON forced (aid);
ALTER TABLE forced ADD CONSTRAINT forced_holds_no_rows CHECK (false) NO INHERIT;
-- The fields each job's rows of the field-split tables were filed under,
-- written by the publisher with the rows, so a reader finds a job's children
-- without reading a parent.
CREATE TABLE job_fields (
job bigint NOT NULL REFERENCES jobs (id),
parent text NOT NULL CHECK (parent IN ('objects', 'memberships', 'packets', 'forced',
'xobjects', 'xmemberships')),
field integer NOT NULL,
PRIMARY KEY (job, parent, field)
);
-- Reference catalogs as products: Gaia, NED, NED-LVS, solar-system predictions.
CREATE TABLE catalogs (
job bigint NOT NULL REFERENCES jobs (id),
dataset text NOT NULL,
name text NOT NULL,
version text NOT NULL,
location text NOT NULL,
UNIQUE (dataset, name, version, job)
);
-- Catalog match rows of a packet set (Gaia, NED, NED-LVS), each naming the catalog job
-- matched against; catid is Gaia's source_id or NED's preferred name.
CREATE TABLE external (
job bigint NOT NULL REFERENCES jobs (id), -- the packet set's job
source_job bigint NOT NULL REFERENCES jobs (id),
sid bigint NOT NULL,
catalog_job bigint NOT NULL REFERENCES jobs (id),
catid text NOT NULL, -- the catalog's own source id
ra double precision NOT NULL,
dec double precision NOT NULL,
sep double precision NOT NULL,
pa double precision NOT NULL,
UNIQUE (job, source_job, sid, catalog_job, catid)
);
--------------------------------------------------------------------------------
-- Child tables of the catalog tables: one per UT observation date and SCA
--------------------------------------------------------------------------------
-- The child of `parent` that holds rows at `mjdobs` on `sca`, made if it does
-- not exist, as dev's loaders make theirs: a table LIKE the parent with its
-- defaults and its unique index, then INHERIT, which locks the
-- parent SHARE UPDATE EXCLUSIVE (loads and reads go on). Its CHECK holds it
-- to [floor(mjdobs), floor(mjdobs) + 1) and the SCA, so a query on a date or
-- an SCA skips the other children. MJD days begin at 0h UT, so the name's date
-- is the UT observation date. No foreign key to jobs, as dev's children have
-- none: adding one would lock jobs against every publish in flight; the
-- publisher sets job from the job row it holds. An advisory lock per child
-- makes two callers agree. SECURITY DEFINER: the pipeline's role holds table
-- grants only, so the schema's owner makes the tables, and EXECUTE is granted
-- to that role alone (docs/foundation-smdc-step.md).
-- The name of the child of `parent` holding rows at `mjdobs` on `sca`:
-- <parent>_<yyyymmdd>_<sca>, the UT date of mjdobs (MJD days begin at 0h UT).
CREATE FUNCTION catalog_child_name(parent text, mjdobs double precision, sca integer)
RETURNS text
LANGUAGE sql IMMUTABLE AS $$
SELECT parent || '_' || to_char(date '1858-11-17' + floor(mjdobs)::integer, 'YYYYMMDD') || '_' || sca
$$;
CREATE FUNCTION create_catalog_child(parent text, mjdobs double precision, sca integer)
RETURNS text
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public, pg_temp AS $$
DECLARE
day double precision := floor(mjdobs);
child text := catalog_child_name(parent, mjdobs, sca);
BEGIN
IF parent <> ALL (ARRAY['sources', 'xsources']) THEN
RAISE EXCEPTION 'create_catalog_child: % is not a catalog table with children', parent;
END IF;
IF to_regclass('public.' || quote_ident(child)) IS NOT NULL THEN
RETURN child;
END IF;
PERFORM pg_advisory_xact_lock(hashtext('create_catalog_child'), hashtext(child));
-- After the wait, a query of pg_class: its snapshot sees a child another
-- caller committed meanwhile, where to_regclass's cached lookup may not.
IF NOT EXISTS (SELECT FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public' AND c.relname = child) THEN
EXECUTE format('CREATE TABLE public.%I (LIKE public.%I INCLUDING DEFAULTS INCLUDING INDEXES,'
' CONSTRAINT %I CHECK (mjdobs >= %s AND mjdobs < %s AND sca = %s))',
child, parent, child || '_date_sca', day, day + 1, sca);
EXECUTE format('ALTER TABLE public.%I INHERIT public.%I', child, parent);
END IF;
RETURN child;
END
$$;
REVOKE EXECUTE ON FUNCTION create_catalog_child(text, double precision, integer) FROM PUBLIC;
-- Reading the children a task needs, never a parent: a query on sources or
-- xsources (or their _official views) visits every child, and they grow by up
-- to 18 a night. A source set's child is named by its difference image's L2
-- mjdobs and SCA (the job that wrote the set wrote the image), so finding it
-- reads no parent; null when the set has no child of `parent` (no such rows).
-- An image with no L2 row (an imported or synthetic one without its lineage)
-- falls back to its own jd, which the stages set to the same mjdobs + 2400000.5.
CREATE FUNCTION source_set_child(parent text, job bigint)
RETURNS text
LANGUAGE sql STABLE AS $$
SELECT c.relname::text
FROM diffimages d
LEFT JOIN l2files l ON l.rid = d.rid
JOIN pg_class c
ON c.relname = catalog_child_name(parent, coalesce(l.mjdobs, d.jd - 2400000.5), d.sca)
JOIN pg_namespace n ON n.oid = c.relnamespace AND n.nspname = 'public'
WHERE d.job = source_set_child.job
$$;
-- The rows of the source sets `jobs`, read from their children only:
-- FROM sources_of(ARRAY[...]) s where a query would read FROM sources s.
CREATE FUNCTION sources_of(jobs bigint[])
RETURNS SETOF sources
LANGUAGE plpgsql STABLE AS $$
DECLARE
child text;
BEGIN
FOR child IN SELECT DISTINCT source_set_child('sources', j) FROM unnest(jobs) j LOOP
CONTINUE WHEN child IS NULL;
RETURN QUERY EXECUTE format('SELECT * FROM public.%I WHERE job = ANY($1)', child) USING jobs;
END LOOP;
END
$$;
-- The xsources of the source sets `jobs`, read from their children only.
CREATE FUNCTION xsources_of(jobs bigint[])
RETURNS SETOF xsources
LANGUAGE plpgsql STABLE AS $$
DECLARE
child text;
BEGIN
FOR child IN SELECT DISTINCT source_set_child('xsources', j) FROM unnest(jobs) j LOOP
CONTINUE WHEN child IS NULL;
RETURN QUERY EXECUTE format('SELECT * FROM public.%I WHERE job = ANY($1)', child) USING jobs;
END LOOP;
END
$$;
-- The child tables of objects, memberships, packets, forced, xobjects and
-- xmemberships, one per field, made as the catalog children are: by the publisher, as the schema's
-- owner, LIKE the parent with its defaults and indexes, then INHERIT, with a
-- CHECK holding the child to its field and no foreign key to jobs.
CREATE FUNCTION field_child_name(parent text, field integer)
RETURNS text
LANGUAGE sql IMMUTABLE AS $$
SELECT parent || '_' || field
$$;
CREATE FUNCTION create_field_child(parent text, field integer)
RETURNS text
LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, public, pg_temp AS $$
DECLARE
child text := field_child_name(parent, field);
BEGIN
IF parent <> ALL (ARRAY['objects', 'memberships', 'packets', 'forced', 'xobjects',
'xmemberships']) THEN
RAISE EXCEPTION 'create_field_child: % is not a table with field children', parent;
END IF;
IF field IS NULL OR field < 0 THEN
RAISE EXCEPTION 'create_field_child: field % is not a tessellation field', field;
END IF;
IF to_regclass('public.' || quote_ident(child)) IS NOT NULL THEN
RETURN child;
END IF;
PERFORM pg_advisory_xact_lock(hashtext('create_field_child'), hashtext(child));
-- After the wait, a query of pg_class, as in create_catalog_child.
IF NOT EXISTS (SELECT FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'public' AND c.relname = child) THEN
EXECUTE format('CREATE TABLE public.%I (LIKE public.%I INCLUDING DEFAULTS INCLUDING INDEXES,'
' CONSTRAINT %I CHECK (field = %s))', child, parent, child || '_field', field);
EXECUTE format('ALTER TABLE public.%I INHERIT public.%I', child, parent);
END IF;
RETURN child;
END
$$;
REVOKE EXECUTE ON FUNCTION create_field_child(text, integer) FROM PUBLIC;
-- Reading the field children a task needs, never a parent. `parent` is a
-- null of the parent's row type (NULL::objects); the rows of the children of
-- `fields` that exist, of the jobs `jobs` when it is not null.
CREATE FUNCTION field_rows(parent anyelement, fields integer[], jobs bigint[])
RETURNS SETOF anyelement
LANGUAGE plpgsql STABLE AS $$
DECLARE
name text := (SELECT t.typname FROM pg_type t WHERE t.oid = pg_typeof(parent));
child text;
BEGIN
FOR child IN SELECT DISTINCT field_child_name(name, f) FROM unnest(fields) f
WHERE f IS NOT NULL ORDER BY 1 LOOP
CONTINUE WHEN to_regclass('public.' || quote_ident(child)) IS NULL;
IF jobs IS NULL THEN
RETURN QUERY EXECUTE format('SELECT * FROM public.%I', child);
ELSE
RETURN QUERY EXECUTE format('SELECT * FROM public.%I WHERE job = ANY($1)', child)
USING jobs;
END IF;
END LOOP;
END
$$;
-- field_rows of the dataset `dataset` only, of every run: the dataset's
-- filter inside each child's query, so a field's rows of other datasets are
-- never returned through the function (each RETURN QUERY holds its rows until
-- the caller reads them, in temp files past work_mem; throughput-record, run
-- 228). A difference job matches every run's objects of its dataset
-- (objects-across-runs).
CREATE FUNCTION field_dataset_rows(parent anyelement, fields integer[], dataset text)
RETURNS SETOF anyelement
LANGUAGE plpgsql STABLE AS $$
DECLARE
name text := (SELECT t.typname FROM pg_type t WHERE t.oid = pg_typeof(parent));
child text;
BEGIN
FOR child IN SELECT DISTINCT field_child_name(name, f) FROM unnest(fields) f
WHERE f IS NOT NULL ORDER BY 1 LOOP
CONTINUE WHEN to_regclass('public.' || quote_ident(child)) IS NULL;
RETURN QUERY EXECUTE format('SELECT * FROM public.%I WHERE dataset = $1', child)
USING dataset;
END LOOP;
END
$$;
-- The packet stage's three field reads, each query's filter inside each
-- child's query, of every run (packet-field-reads, run 229): the objects of
-- the (job, aid) pairs; their memberships of the job's own set `own` or an
-- open sources binding; forced rows on the images `pids` of an open packets
-- binding. Under xobjects and xmemberships the pairs' column is xaid.
CREATE FUNCTION packet_origin_rows(parent anyelement, fields integer[], jobs bigint[],
aids bigint[])
RETURNS SETOF anyelement
LANGUAGE plpgsql STABLE AS $$
DECLARE
name text := (SELECT t.typname FROM pg_type t WHERE t.oid = pg_typeof(parent));
aid text := CASE WHEN name = 'xobjects' THEN 'xaid' ELSE 'aid' END;
child text;
BEGIN