Skip to content

Commit

Permalink
Fix rare case where geometry oid cannot be looked up during materiali…
Browse files Browse the repository at this point in the history
…zed view refresh, causing NULL return values in the matview. Closes #263
  • Loading branch information
pramsey committed Dec 18, 2024
1 parent 91731ce commit 6c7adbb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
78 changes: 73 additions & 5 deletions ogr_fdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,27 +264,95 @@ ogr_fdw_exit(int code, Datum arg)
}

/*
* Function to get the geometry OID if required
* Given extension oid, lookup installation namespace oid.
* This side steps search_path issues with
* TypenameGetTypid encountered in
* https://github.com/pramsey/pgsql-ogr-fdw/issues/263
*/
static Oid
get_extension_nsp_oid(Oid extOid)
{
Oid result;
SysScanDesc scandesc;
HeapTuple tuple;
ScanKeyData entry[1];

Relation rel = table_open(ExtensionRelationId, AccessShareLock);
ScanKeyInit(&entry[0],
Anum_pg_extension_oid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(extOid));

scandesc = systable_beginscan(rel, ExtensionOidIndexId, true,
NULL, 1, entry);

tuple = systable_getnext(scandesc);

/* We assume that there can be at most one matching tuple */
if (HeapTupleIsValid(tuple))
result = ((Form_pg_extension) GETSTRUCT(tuple))->extnamespace;
else
result = InvalidOid;

systable_endscan(scandesc);

table_close(rel, AccessShareLock);

return result;
}


/*
* Get the geometry OID (if postgis is
* installed) and cache it for quick lookup.
*/
Oid
ogrGetGeometryOid(void)
{
/* Is value not set yet? */
if (GEOMETRYOID == InvalidOid)
{
Oid typoid = TypenameGetTypid("geometry");
if (OidIsValid(typoid) && get_typisdefined(typoid))
const char *extName = "postgis";
const char *typName = "geometry";
bool missing_ok = true;
Oid extOid, extNspOid, typOid;

/* Got postgis extension? */
extOid = get_extension_oid(extName, missing_ok);
if (!OidIsValid(extOid))
{
GEOMETRYOID = typoid;
elog(DEBUG2, "%s: lookup of extension '%s' failed", __func__, extName);
GEOMETRYOID = BYTEAOID;
return GEOMETRYOID;
}
else

/* Got namespace for extension? */
extNspOid = get_extension_nsp_oid(extOid);
if (!OidIsValid(extNspOid))
{
elog(DEBUG2, "%s: lookup of namespace for '%s' (%u) failed", __func__, extName, extOid);
GEOMETRYOID = BYTEAOID;
return GEOMETRYOID;
}

/* Got geometry type in namespace? */
typOid = GetSysCacheOid2(TYPENAMENSP, Anum_pg_type_oid,
PointerGetDatum(typName),
ObjectIdGetDatum(extNspOid));

elog(DEBUG2, "%s: lookup of type id for '%s' got %u", __func__, typName, typOid);

/* Geometry type is good? */
if (OidIsValid(typOid) && get_typisdefined(typOid))
GEOMETRYOID = typOid;
else
GEOMETRYOID = BYTEAOID;
}

return GEOMETRYOID;
}


/*
* Foreign-data wrapper handler function: return a struct with pointers
* to my callback routines.
Expand Down
3 changes: 3 additions & 0 deletions ogr_fdw.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "access/transam.h"
#include "catalog/namespace.h"
#include "catalog/pg_collation.h"
#include "catalog/pg_extension.h"
#include "catalog/pg_foreign_table.h"
#include "catalog/pg_foreign_server.h"
#include "catalog/pg_namespace.h"
Expand All @@ -33,6 +34,7 @@
#include "commands/copy.h"
#include "commands/defrem.h"
#include "commands/explain.h"
#include "commands/extension.h"
#include "commands/vacuum.h"
#include "foreign/fdwapi.h"
#include "foreign/foreign.h"
Expand All @@ -49,6 +51,7 @@
#include "storage/ipc.h"
#include "utils/builtins.h"
#include "utils/date.h"
#include "utils/fmgroids.h"
#include "utils/lsyscache.h"
#include "utils/memutils.h"
#include "utils/numeric.h"
Expand Down

0 comments on commit 6c7adbb

Please sign in to comment.