mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
New findoidjoins examines oid columns to find join relationships.
This commit is contained in:
93
contrib/findoidjoins/findoidjoins.c
Normal file
93
contrib/findoidjoins/findoidjoins.c
Normal file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* findoidjoins.c, required pgsql/contrib/pginterface
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "halt.h"
|
||||
#include <libpq-fe.h>
|
||||
#include "pginterface.h"
|
||||
|
||||
PGresult *attres, *relres;
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
char query[4000];
|
||||
char relname[256];
|
||||
char relname2[256];
|
||||
char attname[256];
|
||||
int count;
|
||||
|
||||
if (argc != 2)
|
||||
halt("Usage: %s database\n", argv[0]);
|
||||
|
||||
connectdb(argv[1], NULL, NULL, NULL, NULL);
|
||||
on_error_continue();
|
||||
on_error_stop();
|
||||
|
||||
doquery("BEGIN WORK");
|
||||
doquery("\
|
||||
DECLARE c_attributes BINARY CURSOR FOR \
|
||||
SELECT relname, a.attname \
|
||||
FROM pg_class c, pg_attribute a, pg_type t \
|
||||
WHERE a.attnum > 0 AND \
|
||||
relkind = 'r' AND \
|
||||
typname = 'oid' AND \
|
||||
a.attrelid = c.oid AND \
|
||||
a.atttypid = t.oid \
|
||||
ORDER BY 1; \
|
||||
");
|
||||
doquery("FETCH ALL IN c_attributes");
|
||||
attres = get_result();
|
||||
|
||||
doquery("\
|
||||
DECLARE c_relations BINARY CURSOR FOR \
|
||||
SELECT relname \
|
||||
FROM pg_class c \
|
||||
WHERE relkind = 'r' AND \
|
||||
relname != 'pg_user' \
|
||||
ORDER BY 1; \
|
||||
");
|
||||
doquery("FETCH ALL IN c_relations");
|
||||
relres = get_result();
|
||||
|
||||
set_result(attres);
|
||||
while (fetch(relname, attname) != END_OF_TUPLES)
|
||||
{
|
||||
set_result(relres);
|
||||
reset_fetch();
|
||||
while (fetch(relname2) != END_OF_TUPLES)
|
||||
{
|
||||
unset_result(relres);
|
||||
sprintf(query,"\
|
||||
DECLARE c_matches BINARY CURSOR FOR \
|
||||
SELECT count(*)
|
||||
FROM %s t1, %s t2 \
|
||||
WHERE t1.%s = t2.oid", relname, relname2, attname);
|
||||
|
||||
doquery(query);
|
||||
doquery("FETCH ALL IN c_matches");
|
||||
fetch(&count);
|
||||
if (count != 0)
|
||||
printf("Join %s.%s => %s.oid\n", relname, attname, relname2);
|
||||
doquery("CLOSE c_matches");
|
||||
set_result(relres);
|
||||
}
|
||||
set_result(attres);
|
||||
}
|
||||
|
||||
set_result(relres);
|
||||
doquery("CLOSE c_relations");
|
||||
PQclear(relres);
|
||||
|
||||
set_result(attres);
|
||||
doquery("CLOSE c_attributes");
|
||||
PQclear(attres);
|
||||
unset_result(attres);
|
||||
|
||||
doquery("COMMIT WORK");
|
||||
|
||||
disconnectdb();
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user