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 db95be3
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 5 deletions.
95 changes: 90 additions & 5 deletions ogr_fdw.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,27 +264,112 @@ 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];

#if PG_VERSION_NUM < 120000
Relation rel = heap_open(ExtensionRelationId, AccessShareLock);
ScanKeyInit(&entry[0],
ObjectIdAttributeNumber,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(extOid));
#else
Relation rel = table_open(ExtensionRelationId, AccessShareLock);
ScanKeyInit(&entry[0],
Anum_pg_extension_oid,
BTEqualStrategyNumber, F_OIDEQ,
ObjectIdGetDatum(extOid));
#endif /* PG_VERSION_NUM */

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);

#if PG_VERSION_NUM < 120000
heap_close(rel, AccessShareLock);
#else
table_close(rel, AccessShareLock);
#endif

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,
#if PG_VERSION_NUM >= 120000
Anum_pg_type_oid,
#endif
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
4 changes: 4 additions & 0 deletions ogr_fdw.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
#include "access/reloptions.h"
#include "access/sysattr.h"
#include "access/transam.h"
#include "catalog/indexing.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 +35,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 +52,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 db95be3

Please sign in to comment.