-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathqueries.sql.go
More file actions
3362 lines (3219 loc) · 114 KB
/
Copy pathqueries.sql.go
File metadata and controls
3362 lines (3219 loc) · 114 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
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: queries.sql
package queries
import (
"context"
"database/sql"
"github.com/lib/pq"
)
const getAggregates = `-- name: GetAggregates :many
SELECT
n.nspname AS aggregate_schema,
p.proname AS aggregate_name,
pg_get_function_arguments(p.oid) AS aggregate_signature,
oidvectortypes(p.proargtypes) AS aggregate_arguments,
format_type(p.prorettype, NULL) AS aggregate_return_type,
-- Get transition function
COALESCE(tf.proname, '') AS transition_function,
COALESCE(tfn.nspname, '') AS transition_function_schema,
-- Get state type
format_type(a.aggtranstype, NULL) AS state_type,
-- Get initial condition
a.agginitval AS initial_condition,
-- Get final function if exists
COALESCE(ff.proname, '') AS final_function,
COALESCE(ffn.nspname, '') AS final_function_schema,
-- Comment
COALESCE(d.description, '') AS aggregate_comment
FROM pg_proc p
JOIN pg_namespace n ON p.pronamespace = n.oid
JOIN pg_aggregate a ON a.aggfnoid = p.oid
LEFT JOIN pg_proc tf ON a.aggtransfn = tf.oid
LEFT JOIN pg_namespace tfn ON tf.pronamespace = tfn.oid
LEFT JOIN pg_proc ff ON a.aggfinalfn = ff.oid
LEFT JOIN pg_namespace ffn ON ff.pronamespace = ffn.oid
LEFT JOIN pg_description d ON d.objoid = p.oid AND d.classoid = 'pg_proc'::regclass
WHERE p.prokind = 'a' -- Only aggregates
AND n.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
AND n.nspname NOT LIKE 'pg_temp_%'
AND n.nspname NOT LIKE 'pg_toast_temp_%'
AND NOT EXISTS (
SELECT 1 FROM pg_depend dep
WHERE dep.objid = p.oid AND dep.deptype = 'e'
) -- Exclude extension members
ORDER BY n.nspname, p.proname
`
type GetAggregatesRow struct {
AggregateSchema string `db:"aggregate_schema" json:"aggregate_schema"`
AggregateName string `db:"aggregate_name" json:"aggregate_name"`
AggregateSignature sql.NullString `db:"aggregate_signature" json:"aggregate_signature"`
AggregateArguments sql.NullString `db:"aggregate_arguments" json:"aggregate_arguments"`
AggregateReturnType sql.NullString `db:"aggregate_return_type" json:"aggregate_return_type"`
TransitionFunction sql.NullString `db:"transition_function" json:"transition_function"`
TransitionFunctionSchema sql.NullString `db:"transition_function_schema" json:"transition_function_schema"`
StateType sql.NullString `db:"state_type" json:"state_type"`
InitialCondition sql.NullString `db:"initial_condition" json:"initial_condition"`
FinalFunction sql.NullString `db:"final_function" json:"final_function"`
FinalFunctionSchema sql.NullString `db:"final_function_schema" json:"final_function_schema"`
AggregateComment sql.NullString `db:"aggregate_comment" json:"aggregate_comment"`
}
// GetAggregates retrieves all user-defined aggregates
func (q *Queries) GetAggregates(ctx context.Context) ([]GetAggregatesRow, error) {
rows, err := q.db.QueryContext(ctx, getAggregates)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAggregatesRow
for rows.Next() {
var i GetAggregatesRow
if err := rows.Scan(
&i.AggregateSchema,
&i.AggregateName,
&i.AggregateSignature,
&i.AggregateArguments,
&i.AggregateReturnType,
&i.TransitionFunction,
&i.TransitionFunctionSchema,
&i.StateType,
&i.InitialCondition,
&i.FinalFunction,
&i.FinalFunctionSchema,
&i.AggregateComment,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getAggregatesForSchema = `-- name: GetAggregatesForSchema :many
SELECT
n.nspname AS aggregate_schema,
p.proname AS aggregate_name,
pg_get_function_arguments(p.oid) AS aggregate_signature,
oidvectortypes(p.proargtypes) AS aggregate_arguments,
format_type(p.prorettype, NULL) AS aggregate_return_type,
-- Get transition function
COALESCE(tf.proname, '') AS transition_function,
COALESCE(tfn.nspname, '') AS transition_function_schema,
-- Get state type
format_type(a.aggtranstype, NULL) AS state_type,
-- Get initial condition
a.agginitval AS initial_condition,
-- Get final function if exists
COALESCE(ff.proname, '') AS final_function,
COALESCE(ffn.nspname, '') AS final_function_schema,
-- Comment
COALESCE(d.description, '') AS aggregate_comment
FROM pg_proc p
JOIN pg_namespace n ON p.pronamespace = n.oid
JOIN pg_aggregate a ON a.aggfnoid = p.oid
LEFT JOIN pg_proc tf ON a.aggtransfn = tf.oid
LEFT JOIN pg_namespace tfn ON tf.pronamespace = tfn.oid
LEFT JOIN pg_proc ff ON a.aggfinalfn = ff.oid
LEFT JOIN pg_namespace ffn ON ff.pronamespace = ffn.oid
LEFT JOIN pg_description d ON d.objoid = p.oid AND d.classoid = 'pg_proc'::regclass
WHERE p.prokind = 'a' -- Only aggregates
AND n.nspname = $1
AND NOT EXISTS (
SELECT 1 FROM pg_depend dep
WHERE dep.objid = p.oid AND dep.deptype = 'e'
) -- Exclude extension members
ORDER BY n.nspname, p.proname
`
type GetAggregatesForSchemaRow struct {
AggregateSchema string `db:"aggregate_schema" json:"aggregate_schema"`
AggregateName string `db:"aggregate_name" json:"aggregate_name"`
AggregateSignature sql.NullString `db:"aggregate_signature" json:"aggregate_signature"`
AggregateArguments sql.NullString `db:"aggregate_arguments" json:"aggregate_arguments"`
AggregateReturnType sql.NullString `db:"aggregate_return_type" json:"aggregate_return_type"`
TransitionFunction sql.NullString `db:"transition_function" json:"transition_function"`
TransitionFunctionSchema sql.NullString `db:"transition_function_schema" json:"transition_function_schema"`
StateType sql.NullString `db:"state_type" json:"state_type"`
InitialCondition sql.NullString `db:"initial_condition" json:"initial_condition"`
FinalFunction sql.NullString `db:"final_function" json:"final_function"`
FinalFunctionSchema sql.NullString `db:"final_function_schema" json:"final_function_schema"`
AggregateComment sql.NullString `db:"aggregate_comment" json:"aggregate_comment"`
}
// GetAggregatesForSchema retrieves all user-defined aggregates for a specific schema
func (q *Queries) GetAggregatesForSchema(ctx context.Context, dollar_1 sql.NullString) ([]GetAggregatesForSchemaRow, error) {
rows, err := q.db.QueryContext(ctx, getAggregatesForSchema, dollar_1)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetAggregatesForSchemaRow
for rows.Next() {
var i GetAggregatesForSchemaRow
if err := rows.Scan(
&i.AggregateSchema,
&i.AggregateName,
&i.AggregateSignature,
&i.AggregateArguments,
&i.AggregateReturnType,
&i.TransitionFunction,
&i.TransitionFunctionSchema,
&i.StateType,
&i.InitialCondition,
&i.FinalFunction,
&i.FinalFunctionSchema,
&i.AggregateComment,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getColumnPrivilegesForSchema = `-- name: GetColumnPrivilegesForSchema :many
WITH column_acls AS (
SELECT
c.relname AS table_name,
a.attname AS column_name,
a.attacl AS acl
FROM pg_attribute a
JOIN pg_class c ON a.attrelid = c.oid
JOIN pg_namespace n ON c.relnamespace = n.oid
WHERE n.nspname = $1
AND c.relkind IN ('r', 'v', 'm') -- tables, views, materialized views
AND a.attnum > 0 -- skip system columns
AND NOT a.attisdropped
AND a.attacl IS NOT NULL -- only columns with explicit ACL
)
SELECT
table_name,
column_name,
(aclexplode(acl)).grantee AS grantee_oid,
(aclexplode(acl)).privilege_type AS privilege_type,
(aclexplode(acl)).is_grantable AS is_grantable
FROM column_acls
ORDER BY table_name, column_name, grantee_oid, privilege_type
`
type GetColumnPrivilegesForSchemaRow struct {
TableName string `db:"table_name" json:"table_name"`
ColumnName string `db:"column_name" json:"column_name"`
GranteeOid interface{} `db:"grantee_oid" json:"grantee_oid"`
PrivilegeType sql.NullString `db:"privilege_type" json:"privilege_type"`
IsGrantable sql.NullBool `db:"is_grantable" json:"is_grantable"`
}
// GetColumnPrivilegesForSchema retrieves column-level privilege grants
// Column privileges are stored in pg_attribute.attacl and allow fine-grained access
func (q *Queries) GetColumnPrivilegesForSchema(ctx context.Context, dollar_1 sql.NullString) ([]GetColumnPrivilegesForSchemaRow, error) {
rows, err := q.db.QueryContext(ctx, getColumnPrivilegesForSchema, dollar_1)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetColumnPrivilegesForSchemaRow
for rows.Next() {
var i GetColumnPrivilegesForSchemaRow
if err := rows.Scan(
&i.TableName,
&i.ColumnName,
&i.GranteeOid,
&i.PrivilegeType,
&i.IsGrantable,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getColumns = `-- name: GetColumns :many
WITH column_base AS (
SELECT
c.table_schema,
c.table_name,
c.column_name,
c.ordinal_position,
c.column_default,
c.is_nullable,
c.data_type,
c.character_maximum_length,
c.numeric_precision,
c.numeric_scale,
c.udt_name,
COALESCE(d.description, '') AS column_comment,
CASE
WHEN dt.typtype = 'd' THEN
CASE WHEN dn.nspname = c.table_schema THEN dt.typname
ELSE dn.nspname || '.' || dt.typname
END
WHEN dt.typtype = 'e' OR dt.typtype = 'c' THEN
CASE WHEN dn.nspname = c.table_schema THEN dt.typname
ELSE dn.nspname || '.' || dt.typname
END
WHEN dt.typtype = 'b' AND dt.typcategory = 'A' THEN
-- Array types: apply same schema qualification logic to element type
-- Use typcategory = 'A' rather than typelem <> 0; the latter is true
-- for non-array fixed-length types like name (typelem points to char).
-- Use format_type to preserve typmod for element types (e.g., varchar(128)[] for character varying(128)[])
CASE
WHEN en.nspname = 'pg_catalog' THEN et.typname
WHEN en.nspname = c.table_schema THEN et.typname
ELSE en.nspname || '.' || et.typname
END || COALESCE(substring(format_type(a.atttypid, a.atttypmod) FROM '\([^)]*\)'), '') || '[]'
WHEN dt.typtype = 'b' THEN
-- Non-array base types: qualify if not in pg_catalog or table's schema
-- Use format_type to preserve typmod for extension types (e.g., vector(384) for pgvector)
CASE
WHEN dn.nspname = 'pg_catalog' THEN c.udt_name
WHEN dn.nspname = c.table_schema THEN
dt.typname || COALESCE(substring(format_type(a.atttypid, a.atttypmod) FROM '\([^)]*\)'), '')
ELSE
dn.nspname || '.' || dt.typname || COALESCE(substring(format_type(a.atttypid, a.atttypmod) FROM '\([^)]*\)'), '')
END
ELSE c.udt_name
END AS resolved_type,
c.is_identity,
c.identity_generation,
c.identity_start,
c.identity_increment,
c.identity_maximum,
c.identity_minimum,
c.identity_cycle,
a.attgenerated,
ad.adbin,
ad.adrelid
FROM information_schema.columns c
LEFT JOIN pg_namespace n ON n.nspname = c.table_schema
LEFT JOIN pg_class cl ON cl.relname = c.table_name AND cl.relnamespace = n.oid
LEFT JOIN pg_description d ON d.objoid = cl.oid AND d.classoid = 'pg_class'::regclass AND d.objsubid = c.ordinal_position
LEFT JOIN pg_attribute a ON a.attrelid = cl.oid AND a.attname = c.column_name
LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
LEFT JOIN pg_type dt ON dt.oid = a.atttypid
LEFT JOIN pg_namespace dn ON dt.typnamespace = dn.oid
LEFT JOIN pg_type et ON dt.typelem = et.oid
LEFT JOIN pg_namespace en ON et.typnamespace = en.oid
WHERE
c.table_schema NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
AND c.table_schema NOT LIKE 'pg_temp_%'
AND c.table_schema NOT LIKE 'pg_toast_temp_%'
)
SELECT
cb.table_schema,
cb.table_name,
cb.column_name,
cb.ordinal_position,
-- Use the column_default from LATERAL join which has proper search_path set
ge.column_default,
cb.is_nullable,
cb.data_type,
cb.character_maximum_length,
cb.numeric_precision,
cb.numeric_scale,
cb.udt_name,
cb.column_comment,
cb.resolved_type,
cb.is_identity,
cb.identity_generation,
cb.identity_start,
cb.identity_increment,
cb.identity_maximum,
cb.identity_minimum,
cb.identity_cycle,
cb.attgenerated,
-- Use LATERAL join to guarantee execution order:
-- 1. set_config sets search_path to only the table's schema
-- 2. pg_get_expr then uses that search_path
-- This ensures cross-schema type references in column defaults and generated columns
-- are properly qualified (Issue #218)
ge.generated_expr
FROM column_base cb
LEFT JOIN LATERAL (
SELECT
-- Set search_path to only pg_catalog to force pg_get_expr to include schema qualifiers
-- for all user-defined types and functions. The normalization code will then strip
-- same-schema function qualifiers while preserving type qualifiers (Issue #218)
set_config('search_path', 'pg_catalog', true) as dummy,
CASE
WHEN cb.attgenerated = 's' THEN NULL -- Generated columns don't have defaults
ELSE COALESCE(pg_get_expr(cb.adbin, cb.adrelid), cb.column_default)
END as column_default,
CASE
WHEN cb.attgenerated = 's' THEN pg_get_expr(cb.adbin, cb.adrelid)
ELSE NULL
END as generated_expr
) ge ON true
ORDER BY cb.table_schema, cb.table_name, cb.ordinal_position
`
type GetColumnsRow struct {
TableSchema interface{} `db:"table_schema" json:"table_schema"`
TableName interface{} `db:"table_name" json:"table_name"`
ColumnName interface{} `db:"column_name" json:"column_name"`
OrdinalPosition interface{} `db:"ordinal_position" json:"ordinal_position"`
ColumnDefault sql.NullString `db:"column_default" json:"column_default"`
IsNullable interface{} `db:"is_nullable" json:"is_nullable"`
DataType interface{} `db:"data_type" json:"data_type"`
CharacterMaximumLength interface{} `db:"character_maximum_length" json:"character_maximum_length"`
NumericPrecision interface{} `db:"numeric_precision" json:"numeric_precision"`
NumericScale interface{} `db:"numeric_scale" json:"numeric_scale"`
UdtName interface{} `db:"udt_name" json:"udt_name"`
ColumnComment sql.NullString `db:"column_comment" json:"column_comment"`
ResolvedType sql.NullString `db:"resolved_type" json:"resolved_type"`
IsIdentity interface{} `db:"is_identity" json:"is_identity"`
IdentityGeneration interface{} `db:"identity_generation" json:"identity_generation"`
IdentityStart interface{} `db:"identity_start" json:"identity_start"`
IdentityIncrement interface{} `db:"identity_increment" json:"identity_increment"`
IdentityMaximum interface{} `db:"identity_maximum" json:"identity_maximum"`
IdentityMinimum interface{} `db:"identity_minimum" json:"identity_minimum"`
IdentityCycle interface{} `db:"identity_cycle" json:"identity_cycle"`
Attgenerated interface{} `db:"attgenerated" json:"attgenerated"`
GeneratedExpr sql.NullString `db:"generated_expr" json:"generated_expr"`
}
// GetColumns retrieves all columns for all tables
func (q *Queries) GetColumns(ctx context.Context) ([]GetColumnsRow, error) {
rows, err := q.db.QueryContext(ctx, getColumns)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetColumnsRow
for rows.Next() {
var i GetColumnsRow
if err := rows.Scan(
&i.TableSchema,
&i.TableName,
&i.ColumnName,
&i.OrdinalPosition,
&i.ColumnDefault,
&i.IsNullable,
&i.DataType,
&i.CharacterMaximumLength,
&i.NumericPrecision,
&i.NumericScale,
&i.UdtName,
&i.ColumnComment,
&i.ResolvedType,
&i.IsIdentity,
&i.IdentityGeneration,
&i.IdentityStart,
&i.IdentityIncrement,
&i.IdentityMaximum,
&i.IdentityMinimum,
&i.IdentityCycle,
&i.Attgenerated,
&i.GeneratedExpr,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getColumnsForSchema = `-- name: GetColumnsForSchema :many
WITH column_base AS (
SELECT
c.table_schema,
c.table_name,
c.column_name,
c.ordinal_position,
c.column_default,
c.is_nullable,
c.data_type,
c.character_maximum_length,
c.numeric_precision,
c.numeric_scale,
c.udt_name,
COALESCE(d.description, '') AS column_comment,
CASE
WHEN dt.typtype = 'd' THEN
CASE WHEN dn.nspname = c.table_schema THEN dt.typname
ELSE dn.nspname || '.' || dt.typname
END
WHEN dt.typtype = 'e' OR dt.typtype = 'c' THEN
CASE WHEN dn.nspname = c.table_schema THEN dt.typname
ELSE dn.nspname || '.' || dt.typname
END
WHEN dt.typtype = 'b' AND dt.typcategory = 'A' THEN
-- Array types: apply same schema qualification logic to element type
-- Use typcategory = 'A' rather than typelem <> 0; the latter is true
-- for non-array fixed-length types like name (typelem points to char).
-- Use format_type to preserve typmod for element types (e.g., varchar(128)[] for character varying(128)[])
CASE
WHEN en.nspname = 'pg_catalog' THEN et.typname
WHEN en.nspname = c.table_schema THEN et.typname
ELSE en.nspname || '.' || et.typname
END || COALESCE(substring(format_type(a.atttypid, a.atttypmod) FROM '\([^)]*\)'), '') || '[]'
WHEN dt.typtype = 'b' THEN
-- Non-array base types: qualify if not in pg_catalog or table's schema
-- Use format_type to preserve typmod for extension types (e.g., vector(384) for pgvector)
CASE
WHEN dn.nspname = 'pg_catalog' THEN c.udt_name
WHEN dn.nspname = c.table_schema THEN
dt.typname || COALESCE(substring(format_type(a.atttypid, a.atttypmod) FROM '\([^)]*\)'), '')
ELSE
dn.nspname || '.' || dt.typname || COALESCE(substring(format_type(a.atttypid, a.atttypmod) FROM '\([^)]*\)'), '')
END
ELSE c.udt_name
END AS resolved_type,
c.is_identity,
c.identity_generation,
c.identity_start,
c.identity_increment,
c.identity_maximum,
c.identity_minimum,
c.identity_cycle,
a.attgenerated,
ad.adbin,
ad.adrelid,
cl.oid AS table_oid
FROM information_schema.columns c
LEFT JOIN pg_namespace n ON n.nspname = c.table_schema
LEFT JOIN pg_class cl ON cl.relname = c.table_name AND cl.relnamespace = n.oid
LEFT JOIN pg_description d ON d.objoid = cl.oid AND d.classoid = 'pg_class'::regclass AND d.objsubid = c.ordinal_position
LEFT JOIN pg_attribute a ON a.attrelid = cl.oid AND a.attname = c.column_name
LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
LEFT JOIN pg_type dt ON dt.oid = a.atttypid
LEFT JOIN pg_namespace dn ON dt.typnamespace = dn.oid
LEFT JOIN pg_type et ON dt.typelem = et.oid
LEFT JOIN pg_namespace en ON et.typnamespace = en.oid
WHERE
c.table_schema = $1
)
SELECT
cb.table_schema,
cb.table_name,
cb.column_name,
cb.ordinal_position,
-- Use the column_default from LATERAL join which has proper search_path set
ge.column_default,
cb.is_nullable,
cb.data_type,
cb.character_maximum_length,
cb.numeric_precision,
cb.numeric_scale,
cb.udt_name,
cb.column_comment,
cb.resolved_type,
cb.is_identity,
cb.identity_generation,
cb.identity_start,
cb.identity_increment,
cb.identity_maximum,
cb.identity_minimum,
cb.identity_cycle,
cb.attgenerated,
-- Use LATERAL join to guarantee execution order:
-- 1. set_config sets search_path to only pg_catalog
-- 2. pg_get_expr then uses that search_path and includes schema qualifiers for user types
-- This ensures type references in column defaults and generated columns are properly
-- qualified (Issue #218). The normalization code strips same-schema function qualifiers.
--
-- NOTE: The 'dummy' column in the LATERAL subquery forces set_config to execute
-- before pg_get_expr. PostgreSQL evaluates SELECT columns left-to-right within
-- a single query level. The LATERAL join guarantees this happens row-by-row,
-- and 'ON true' in the join condition ensures the LATERAL subquery executes for every row.
-- This pattern mirrors GetViewsForSchema (line 959-963) for consistency.
--
-- Alternative considered: Create a custom PostgreSQL function wrapping pg_get_expr
-- with search_path control. Rejected because:
-- 1. Requires creating database objects (function) on target database
-- 2. pgschema operates in read-only inspection mode
-- 3. LATERAL join pattern is PostgreSQL-native and well-documented
ge.generated_expr
FROM column_base cb
LEFT JOIN LATERAL (
SELECT
-- Set search_path to only pg_catalog to force pg_get_expr to include schema qualifiers
-- for all user-defined types and functions. The normalization code will then strip
-- same-schema function qualifiers while preserving type qualifiers (Issue #218)
set_config('search_path', 'pg_catalog', true) as dummy,
CASE
WHEN cb.attgenerated = 's' THEN NULL -- Generated columns don't have defaults
ELSE COALESCE(pg_get_expr(cb.adbin, cb.adrelid), cb.column_default)
END as column_default,
CASE
WHEN cb.attgenerated = 's' THEN pg_get_expr(cb.adbin, cb.adrelid)
ELSE NULL
END as generated_expr
) ge ON true
ORDER BY cb.table_name, cb.ordinal_position
`
type GetColumnsForSchemaRow struct {
TableSchema interface{} `db:"table_schema" json:"table_schema"`
TableName interface{} `db:"table_name" json:"table_name"`
ColumnName interface{} `db:"column_name" json:"column_name"`
OrdinalPosition interface{} `db:"ordinal_position" json:"ordinal_position"`
ColumnDefault sql.NullString `db:"column_default" json:"column_default"`
IsNullable interface{} `db:"is_nullable" json:"is_nullable"`
DataType interface{} `db:"data_type" json:"data_type"`
CharacterMaximumLength interface{} `db:"character_maximum_length" json:"character_maximum_length"`
NumericPrecision interface{} `db:"numeric_precision" json:"numeric_precision"`
NumericScale interface{} `db:"numeric_scale" json:"numeric_scale"`
UdtName interface{} `db:"udt_name" json:"udt_name"`
ColumnComment sql.NullString `db:"column_comment" json:"column_comment"`
ResolvedType sql.NullString `db:"resolved_type" json:"resolved_type"`
IsIdentity interface{} `db:"is_identity" json:"is_identity"`
IdentityGeneration interface{} `db:"identity_generation" json:"identity_generation"`
IdentityStart interface{} `db:"identity_start" json:"identity_start"`
IdentityIncrement interface{} `db:"identity_increment" json:"identity_increment"`
IdentityMaximum interface{} `db:"identity_maximum" json:"identity_maximum"`
IdentityMinimum interface{} `db:"identity_minimum" json:"identity_minimum"`
IdentityCycle interface{} `db:"identity_cycle" json:"identity_cycle"`
Attgenerated interface{} `db:"attgenerated" json:"attgenerated"`
GeneratedExpr sql.NullString `db:"generated_expr" json:"generated_expr"`
}
// GetColumnsForSchema retrieves all columns for tables in a specific schema
func (q *Queries) GetColumnsForSchema(ctx context.Context, tableSchema sql.NullString) ([]GetColumnsForSchemaRow, error) {
rows, err := q.db.QueryContext(ctx, getColumnsForSchema, tableSchema)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetColumnsForSchemaRow
for rows.Next() {
var i GetColumnsForSchemaRow
if err := rows.Scan(
&i.TableSchema,
&i.TableName,
&i.ColumnName,
&i.OrdinalPosition,
&i.ColumnDefault,
&i.IsNullable,
&i.DataType,
&i.CharacterMaximumLength,
&i.NumericPrecision,
&i.NumericScale,
&i.UdtName,
&i.ColumnComment,
&i.ResolvedType,
&i.IsIdentity,
&i.IdentityGeneration,
&i.IdentityStart,
&i.IdentityIncrement,
&i.IdentityMaximum,
&i.IdentityMinimum,
&i.IdentityCycle,
&i.Attgenerated,
&i.GeneratedExpr,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getCompositeTypeColumns = `-- name: GetCompositeTypeColumns :many
SELECT
n.nspname AS type_schema,
t.typname AS type_name,
a.attname AS column_name,
a.attnum AS column_position,
format_type(a.atttypid, a.atttypmod) AS column_type
FROM pg_type t
JOIN pg_namespace n ON t.typnamespace = n.oid
JOIN pg_class c ON t.typrelid = c.oid
JOIN pg_attribute a ON c.oid = a.attrelid
WHERE t.typtype = 'c' -- composite types only
AND c.relkind = 'c' -- only true composite types, not table types
AND a.attnum > 0 -- exclude system columns
AND NOT a.attisdropped -- exclude dropped columns
AND n.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
AND n.nspname NOT LIKE 'pg_temp_%'
AND n.nspname NOT LIKE 'pg_toast_temp_%'
ORDER BY n.nspname, t.typname, a.attnum
`
type GetCompositeTypeColumnsRow struct {
TypeSchema string `db:"type_schema" json:"type_schema"`
TypeName string `db:"type_name" json:"type_name"`
ColumnName string `db:"column_name" json:"column_name"`
ColumnPosition int16 `db:"column_position" json:"column_position"`
ColumnType sql.NullString `db:"column_type" json:"column_type"`
}
// GetCompositeTypeColumns retrieves columns for composite types
func (q *Queries) GetCompositeTypeColumns(ctx context.Context) ([]GetCompositeTypeColumnsRow, error) {
rows, err := q.db.QueryContext(ctx, getCompositeTypeColumns)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetCompositeTypeColumnsRow
for rows.Next() {
var i GetCompositeTypeColumnsRow
if err := rows.Scan(
&i.TypeSchema,
&i.TypeName,
&i.ColumnName,
&i.ColumnPosition,
&i.ColumnType,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getCompositeTypeColumnsForSchema = `-- name: GetCompositeTypeColumnsForSchema :many
SELECT
n.nspname AS type_schema,
t.typname AS type_name,
a.attname AS column_name,
a.attnum AS column_position,
format_type(a.atttypid, a.atttypmod) AS column_type
FROM pg_type t
JOIN pg_namespace n ON t.typnamespace = n.oid
JOIN pg_class c ON t.typrelid = c.oid
JOIN pg_attribute a ON c.oid = a.attrelid
WHERE t.typtype = 'c' -- composite types only
AND c.relkind = 'c' -- only true composite types, not table types
AND a.attnum > 0 -- exclude system columns
AND NOT a.attisdropped -- exclude dropped columns
AND n.nspname = $1
ORDER BY n.nspname, t.typname, a.attnum
`
type GetCompositeTypeColumnsForSchemaRow struct {
TypeSchema string `db:"type_schema" json:"type_schema"`
TypeName string `db:"type_name" json:"type_name"`
ColumnName string `db:"column_name" json:"column_name"`
ColumnPosition int16 `db:"column_position" json:"column_position"`
ColumnType sql.NullString `db:"column_type" json:"column_type"`
}
// GetCompositeTypeColumnsForSchema retrieves columns for composite types in a specific schema
func (q *Queries) GetCompositeTypeColumnsForSchema(ctx context.Context, dollar_1 sql.NullString) ([]GetCompositeTypeColumnsForSchemaRow, error) {
rows, err := q.db.QueryContext(ctx, getCompositeTypeColumnsForSchema, dollar_1)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetCompositeTypeColumnsForSchemaRow
for rows.Next() {
var i GetCompositeTypeColumnsForSchemaRow
if err := rows.Scan(
&i.TypeSchema,
&i.TypeName,
&i.ColumnName,
&i.ColumnPosition,
&i.ColumnType,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getConstraints = `-- name: GetConstraints :many
SELECT
n.nspname AS table_schema,
cl.relname AS table_name,
c.conname AS constraint_name,
CASE c.contype
WHEN 'c' THEN 'CHECK'
WHEN 'f' THEN 'FOREIGN KEY'
WHEN 'p' THEN 'PRIMARY KEY'
WHEN 'u' THEN 'UNIQUE'
WHEN 'x' THEN 'EXCLUDE'
ELSE 'UNKNOWN'
END AS constraint_type,
COALESCE(a.attname, '') AS column_name,
COALESCE(a.attnum, 0) AS ordinal_position,
COALESCE(fn.nspname, '') AS foreign_table_schema,
COALESCE(fcl.relname, '') AS foreign_table_name,
COALESCE(fa.attname, '') AS foreign_column_name,
COALESCE(fa.attnum, 0) AS foreign_ordinal_position,
CASE WHEN c.contype = 'c' THEN pg_get_constraintdef(c.oid, true) ELSE NULL END AS check_clause,
CASE WHEN c.contype = 'x' THEN pg_get_constraintdef(c.oid, true) ELSE NULL END AS exclusion_definition,
CASE c.confdeltype
WHEN 'a' THEN 'NO ACTION'
WHEN 'r' THEN 'RESTRICT'
WHEN 'c' THEN 'CASCADE'
WHEN 'n' THEN 'SET NULL'
WHEN 'd' THEN 'SET DEFAULT'
ELSE NULL
END AS delete_rule,
CASE c.confupdtype
WHEN 'a' THEN 'NO ACTION'
WHEN 'r' THEN 'RESTRICT'
WHEN 'c' THEN 'CASCADE'
WHEN 'n' THEN 'SET NULL'
WHEN 'd' THEN 'SET DEFAULT'
ELSE NULL
END AS update_rule,
c.condeferrable AS deferrable,
c.condeferred AS initially_deferred,
c.convalidated AS is_valid,
COALESCE((to_jsonb(c) ->> 'conperiod')::boolean, false) AS is_period,
c.connoinherit AS no_inherit
FROM pg_constraint c
JOIN pg_class cl ON c.conrelid = cl.oid
JOIN pg_namespace n ON cl.relnamespace = n.oid
LEFT JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = ANY(c.conkey)
LEFT JOIN pg_class fcl ON c.confrelid = fcl.oid
LEFT JOIN pg_namespace fn ON fcl.relnamespace = fn.oid
LEFT JOIN pg_attribute fa ON fa.attrelid = c.confrelid AND fa.attnum = c.confkey[array_position(c.conkey, a.attnum)]
WHERE n.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
AND n.nspname NOT LIKE 'pg_temp_%'
AND n.nspname NOT LIKE 'pg_toast_temp_%'
ORDER BY n.nspname, cl.relname, c.contype, c.conname, a.attnum
`
type GetConstraintsRow struct {
TableSchema string `db:"table_schema" json:"table_schema"`
TableName string `db:"table_name" json:"table_name"`
ConstraintName string `db:"constraint_name" json:"constraint_name"`
ConstraintType sql.NullString `db:"constraint_type" json:"constraint_type"`
ColumnName sql.NullString `db:"column_name" json:"column_name"`
OrdinalPosition sql.NullInt32 `db:"ordinal_position" json:"ordinal_position"`
ForeignTableSchema sql.NullString `db:"foreign_table_schema" json:"foreign_table_schema"`
ForeignTableName sql.NullString `db:"foreign_table_name" json:"foreign_table_name"`
ForeignColumnName sql.NullString `db:"foreign_column_name" json:"foreign_column_name"`
ForeignOrdinalPosition sql.NullInt32 `db:"foreign_ordinal_position" json:"foreign_ordinal_position"`
CheckClause sql.NullString `db:"check_clause" json:"check_clause"`
ExclusionDefinition sql.NullString `db:"exclusion_definition" json:"exclusion_definition"`
DeleteRule sql.NullString `db:"delete_rule" json:"delete_rule"`
UpdateRule sql.NullString `db:"update_rule" json:"update_rule"`
Deferrable bool `db:"deferrable" json:"deferrable"`
InitiallyDeferred bool `db:"initially_deferred" json:"initially_deferred"`
IsValid bool `db:"is_valid" json:"is_valid"`
IsPeriod sql.NullBool `db:"is_period" json:"is_period"`
NoInherit bool `db:"no_inherit" json:"no_inherit"`
}
// GetConstraints retrieves all table constraints
func (q *Queries) GetConstraints(ctx context.Context) ([]GetConstraintsRow, error) {
rows, err := q.db.QueryContext(ctx, getConstraints)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetConstraintsRow
for rows.Next() {
var i GetConstraintsRow
if err := rows.Scan(
&i.TableSchema,
&i.TableName,
&i.ConstraintName,
&i.ConstraintType,
&i.ColumnName,
&i.OrdinalPosition,
&i.ForeignTableSchema,
&i.ForeignTableName,
&i.ForeignColumnName,
&i.ForeignOrdinalPosition,
&i.CheckClause,
&i.ExclusionDefinition,
&i.DeleteRule,
&i.UpdateRule,
&i.Deferrable,
&i.InitiallyDeferred,
&i.IsValid,
&i.IsPeriod,
&i.NoInherit,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getConstraintsForSchema = `-- name: GetConstraintsForSchema :many
SELECT
n.nspname AS table_schema,
cl.relname AS table_name,
c.conname AS constraint_name,
CASE c.contype
WHEN 'c' THEN 'CHECK'
WHEN 'f' THEN 'FOREIGN KEY'
WHEN 'p' THEN 'PRIMARY KEY'
WHEN 'u' THEN 'UNIQUE'
WHEN 'x' THEN 'EXCLUDE'
ELSE 'UNKNOWN'
END AS constraint_type,
COALESCE(a.attname, '') AS column_name,
COALESCE(a.attnum, 0) AS ordinal_position,
COALESCE(fn.nspname, '') AS foreign_table_schema,
COALESCE(fcl.relname, '') AS foreign_table_name,
COALESCE(fa.attname, '') AS foreign_column_name,
COALESCE(fa.attnum, 0) AS foreign_ordinal_position,
CASE WHEN c.contype = 'c' THEN pg_get_constraintdef(c.oid, true) ELSE NULL END AS check_clause,
CASE WHEN c.contype = 'x' THEN pg_get_constraintdef(c.oid, true) ELSE NULL END AS exclusion_definition,
CASE c.confdeltype
WHEN 'a' THEN 'NO ACTION'
WHEN 'r' THEN 'RESTRICT'
WHEN 'c' THEN 'CASCADE'
WHEN 'n' THEN 'SET NULL'
WHEN 'd' THEN 'SET DEFAULT'
ELSE NULL
END AS delete_rule,
CASE c.confupdtype
WHEN 'a' THEN 'NO ACTION'
WHEN 'r' THEN 'RESTRICT'
WHEN 'c' THEN 'CASCADE'
WHEN 'n' THEN 'SET NULL'
WHEN 'd' THEN 'SET DEFAULT'
ELSE NULL
END AS update_rule,
c.condeferrable AS deferrable,
c.condeferred AS initially_deferred,
c.convalidated AS is_valid,
COALESCE((to_jsonb(c) ->> 'conperiod')::boolean, false) AS is_period,
c.connoinherit AS no_inherit,
-- pg_index.indnullsnotdistinct is PG15+. Use to_jsonb so the column reference
-- doesn't fail to plan on PG14 (where the attribute does not exist on pg_index).
COALESCE((to_jsonb(i) ->> 'indnullsnotdistinct')::boolean, false) AS nulls_not_distinct
FROM pg_constraint c
JOIN pg_class cl ON c.conrelid = cl.oid
JOIN pg_namespace n ON cl.relnamespace = n.oid
LEFT JOIN pg_attribute a ON a.attrelid = c.conrelid AND a.attnum = ANY(c.conkey)
LEFT JOIN pg_class fcl ON c.confrelid = fcl.oid
LEFT JOIN pg_namespace fn ON fcl.relnamespace = fn.oid
LEFT JOIN pg_attribute fa ON fa.attrelid = c.confrelid AND fa.attnum = c.confkey[array_position(c.conkey, a.attnum)]
LEFT JOIN pg_index i ON i.indexrelid = c.conindid
WHERE n.nspname = $1
ORDER BY n.nspname, cl.relname, c.contype, c.conname, a.attnum
`
type GetConstraintsForSchemaRow struct {
TableSchema string `db:"table_schema" json:"table_schema"`
TableName string `db:"table_name" json:"table_name"`
ConstraintName string `db:"constraint_name" json:"constraint_name"`
ConstraintType sql.NullString `db:"constraint_type" json:"constraint_type"`
ColumnName sql.NullString `db:"column_name" json:"column_name"`
OrdinalPosition sql.NullInt32 `db:"ordinal_position" json:"ordinal_position"`
ForeignTableSchema sql.NullString `db:"foreign_table_schema" json:"foreign_table_schema"`
ForeignTableName sql.NullString `db:"foreign_table_name" json:"foreign_table_name"`
ForeignColumnName sql.NullString `db:"foreign_column_name" json:"foreign_column_name"`
ForeignOrdinalPosition sql.NullInt32 `db:"foreign_ordinal_position" json:"foreign_ordinal_position"`
CheckClause sql.NullString `db:"check_clause" json:"check_clause"`
ExclusionDefinition sql.NullString `db:"exclusion_definition" json:"exclusion_definition"`
DeleteRule sql.NullString `db:"delete_rule" json:"delete_rule"`
UpdateRule sql.NullString `db:"update_rule" json:"update_rule"`
Deferrable bool `db:"deferrable" json:"deferrable"`
InitiallyDeferred bool `db:"initially_deferred" json:"initially_deferred"`
IsValid bool `db:"is_valid" json:"is_valid"`
IsPeriod sql.NullBool `db:"is_period" json:"is_period"`
NoInherit bool `db:"no_inherit" json:"no_inherit"`
NullsNotDistinct sql.NullBool `db:"nulls_not_distinct" json:"nulls_not_distinct"`
}
// GetConstraintsForSchema retrieves all table constraints for a specific schema
func (q *Queries) GetConstraintsForSchema(ctx context.Context, dollar_1 sql.NullString) ([]GetConstraintsForSchemaRow, error) {
rows, err := q.db.QueryContext(ctx, getConstraintsForSchema, dollar_1)
if err != nil {
return nil, err
}
defer rows.Close()
var items []GetConstraintsForSchemaRow
for rows.Next() {
var i GetConstraintsForSchemaRow
if err := rows.Scan(
&i.TableSchema,
&i.TableName,
&i.ConstraintName,
&i.ConstraintType,
&i.ColumnName,
&i.OrdinalPosition,
&i.ForeignTableSchema,
&i.ForeignTableName,
&i.ForeignColumnName,
&i.ForeignOrdinalPosition,
&i.CheckClause,
&i.ExclusionDefinition,
&i.DeleteRule,
&i.UpdateRule,
&i.Deferrable,
&i.InitiallyDeferred,
&i.IsValid,
&i.IsPeriod,
&i.NoInherit,
&i.NullsNotDistinct,
); err != nil {
return nil, err
}