@@ -365,8 +365,15 @@ protected function loadTableIndexes(string $tableName): array
365365 FROM "pg_class" AS "tc"
366366 INNER JOIN "pg_namespace" AS "tcns"
367367 ON "tcns"."oid" = "tc"."relnamespace"
368+ LEFT JOIN pg_rewrite AS rw
369+ ON tc.relkind = 'v' AND rw.ev_class = tc.oid AND rw.rulename = '_RETURN'
368370 INNER JOIN "pg_index" AS "i"
369371 ON "i"."indrelid" = "tc"."oid"
372+ OR rw.ev_action IS NOT NULL
373+ AND (SELECT regexp_matches(
374+ rw.ev_action,
375+ '{TARGETENTRY .*? :resorigtbl ' || "i"."indrelid" || ' :resorigcol ' || "i"."indkey"[0] || ' '
376+ )) IS NOT NULL
370377 INNER JOIN "pg_class" AS "ic"
371378 ON "ic"."oid" = "i"."indexrelid"
372379 INNER JOIN "pg_attribute" AS "ia"
@@ -711,7 +718,7 @@ protected function findColumns(TableSchemaInterface $table): bool
711718 COALESCE(td.oid, tb.oid, a.atttypid),
712719 information_schema._pg_truetypmod(a, t)
713720 ) AS size,
714- a.attnum = any (ct.conkey) as is_pkey,
721+ ct.oid IS NOT NULL AS is_pkey,
715722 COALESCE(NULLIF(a.attndims, 0), NULLIF(t.typndims, 0), (t.typcategory='A')::int) AS dimension
716723 FROM
717724 pg_class c
@@ -722,7 +729,12 @@ protected function findColumns(TableSchemaInterface $table): bool
722729 OR t.typbasetype > 0 AND t.typbasetype = tb.oid
723730 LEFT JOIN pg_type td ON t.typndims > 0 AND t.typbasetype > 0 AND tb.typelem = td.oid
724731 LEFT JOIN pg_namespace d ON d.oid = c.relnamespace
725- LEFT JOIN pg_constraint ct ON ct.conrelid = c.oid AND ct.contype = 'p'
732+ LEFT JOIN pg_rewrite rw ON c.relkind = 'v' AND rw.ev_class = c.oid AND rw.rulename = '_RETURN'
733+ LEFT JOIN pg_constraint ct ON ct.conrelid = c.oid AND ct.contype = 'p' AND a.attnum = ANY (ct.conkey)
734+ OR rw.ev_action IS NOT NULL AND ct.contype = 'p'
735+ AND (ARRAY(
736+ SELECT regexp_matches(rw.ev_action, '{TARGETENTRY .*? :resorigtbl (\d+) :resorigcol (\d+) ', 'g')
737+ ))[a.attnum:a.attnum] <@ (ct.conrelid::text || ct.conkey::text[])
726738 WHERE
727739 a.attnum > 0 AND t.typname != '' AND NOT a.attisdropped
728740 AND c.relname = :tableName
@@ -903,10 +915,16 @@ private function loadTableConstraints(string $tableName, string $returnType): ar
903915 FROM "pg_class" AS "tc"
904916 INNER JOIN "pg_namespace" AS "tcns"
905917 ON "tcns"."oid" = "tc"."relnamespace"
906- INNER JOIN "pg_constraint" AS "c"
907- ON "c"."conrelid" = "tc"."oid"
908918 INNER JOIN "pg_attribute" AS "a"
909- ON "a"."attrelid" = "c"."conrelid" AND "a"."attnum" = ANY ("c"."conkey")
919+ ON "a"."attrelid" = "tc"."oid"
920+ LEFT JOIN pg_rewrite AS rw
921+ ON "tc"."relkind" = 'v' AND "rw"."ev_class" = "tc"."oid" AND "rw"."rulename" = '_RETURN'
922+ INNER JOIN "pg_constraint" AS "c"
923+ ON "c"."conrelid" = "tc"."oid" AND "a"."attnum" = ANY ("c"."conkey")
924+ OR "rw"."ev_action" IS NOT NULL AND "c"."conrelid" != 0
925+ AND (ARRAY(
926+ SELECT regexp_matches("rw"."ev_action", '{TARGETENTRY .*? :resorigtbl (\d+) :resorigcol (\d+) ', 'g')
927+ ))["a"."attnum":"a"."attnum"] <@ ("c"."conrelid"::text || "c"."conkey"::text[])
910928 LEFT JOIN "pg_class" AS "ftc"
911929 ON "ftc"."oid" = "c"."confrelid"
912930 LEFT JOIN "pg_namespace" AS "ftcns"
0 commit comments