versa) without going through DatumGetPointer.
Gavin Sherry, with Feng Tian.
                else if (att[i]->attlen == -1 &&
                                 att[i]->attalign == 'd' &&
                                 att[i]->attndims == 0 &&
-                                !VARATT_IS_EXTENDED(values[i]))
+                                !VARATT_IS_EXTENDED(DatumGetPointer(values[i])))
                {
                        values[i] = toast_flatten_tuple_attribute(values[i],
                                                                                                          att[i]->atttypid,
                else if (att[i]->attlen == -1 &&
                                 att[i]->attalign == 'd' &&
                                 att[i]->attndims == 0 &&
-                                !VARATT_IS_EXTENDED(values[i]))
+                                !VARATT_IS_EXTENDED(DatumGetPointer(values[i])))
                {
                        values[i] = toast_flatten_tuple_attribute(values[i],
                                                                                                          att[i]->atttypid,
 
                 * If value is stored EXTERNAL, must fetch it so we are not depending
                 * on outside storage.  This should be improved someday.
                 */
-               if (VARATT_IS_EXTERNAL(values[i]))
+               if (VARATT_IS_EXTERNAL(DatumGetPointer(values[i])))
                {
                        untoasted_values[i] =
                                PointerGetDatum(heap_tuple_fetch_attr((struct varlena *)
                 * If value is above size target, and is of a compressible datatype,
                 * try to compress it in-line.
                 */
-               if (!VARATT_IS_EXTENDED(untoasted_values[i]) &&
-                       VARSIZE(untoasted_values[i]) > TOAST_INDEX_TARGET &&
+               if (!VARATT_IS_EXTENDED(DatumGetPointer(untoasted_values[i])) &&
+                       VARSIZE(DatumGetPointer(untoasted_values[i])) > TOAST_INDEX_TARGET &&
                        (att->attstorage == 'x' || att->attstorage == 'm'))
                {
                        Datum           cvalue = toast_compress_datum(untoasted_values[i]);
 
                }
 
                /* Clean up detoasted copy, if any */
-               if (attr != origattr)
+               if (DatumGetPointer(attr) != DatumGetPointer(origattr))
                        pfree(DatumGetPointer(attr));
        }
 
                pfree(outputstr);
 
                /* Clean up detoasted copy, if any */
-               if (attr != origattr)
+               if (DatumGetPointer(attr) != DatumGetPointer(origattr))
                        pfree(DatumGetPointer(attr));
        }
 
                pfree(value);
 
                /* Clean up detoasted copy, if any */
-               if (attr != origattr)
+               if (DatumGetPointer(attr) != DatumGetPointer(origattr))
                        pfree(DatumGetPointer(attr));
        }
        printf("\t----\n");
                pfree(outputbytes);
 
                /* Clean up detoasted copy, if any */
-               if (attr != origattr)
+               if (DatumGetPointer(attr) != DatumGetPointer(origattr))
                        pfree(DatumGetPointer(attr));
        }
 
 
        astate = NULL;
 
        /* Copy any oldOptions that aren't to be replaced */
-       if (oldOptions != (Datum) 0)
+       if (PointerIsValid(DatumGetPointer(oldOptions)))
        {
                ArrayType  *array = DatumGetArrayTypeP(oldOptions);
                Datum      *oldoptions;
        int                     i;
 
        /* Nothing to do if no options */
-       if (options == (Datum) 0)
+       if (!PointerIsValid(DatumGetPointer(options)))
                return result;
 
        array = DatumGetArrayTypeP(options);
        MemSet(values, 0, numkeywords * sizeof(char *));
 
        /* Done if no options */
-       if (options == (Datum) 0)
+       if (!PointerIsValid(DatumGetPointer(options)))
                return;
 
        array = DatumGetArrayTypeP(options);
        Assert(RegProcedureIsValid(amoptions));
 
        /* Assume function is strict */
-       if (reloptions == (Datum) 0)
+       if (!PointerIsValid(DatumGetPointer(reloptions)))
                return NULL;
 
        /* Can't use OidFunctionCallN because we might get a NULL result */
 
                {
                        Datum           value = toast_values[i];
 
-                       if (!toast_isnull[i] && VARATT_IS_EXTERNAL(value))
+                       if (!toast_isnull[i] && VARATT_IS_EXTERNAL(PointerGetDatum(value)))
                                toast_delete_datum(rel, value);
                }
        }
                {
                        if (toast_action[i] != ' ')
                                continue;
-                       if (VARATT_IS_EXTERNAL(toast_values[i]))
+                       if (VARATT_IS_EXTERNAL(DatumGetPointer(toast_values[i])))
                                continue;               /* can't happen, toast_action would be 'p' */
-                       if (VARATT_IS_COMPRESSED(toast_values[i]))
+                       if (VARATT_IS_COMPRESSED(DatumGetPointer(toast_values[i])))
                                continue;
                        if (att[i]->attstorage != 'x' && att[i]->attstorage != 'e')
                                continue;
                                        pfree(DatumGetPointer(old_value));
                                toast_values[i] = new_value;
                                toast_free[i] = true;
-                               toast_sizes[i] = VARSIZE(toast_values[i]);
+                               toast_sizes[i] = VARSIZE(DatumGetPointer(toast_values[i]));
                                need_change = true;
                                need_free = true;
                        }
                {
                        if (toast_action[i] == 'p')
                                continue;
-                       if (VARATT_IS_EXTERNAL(toast_values[i]))
+                       if (VARATT_IS_EXTERNAL(DatumGetPointer(toast_values[i])))
                                continue;               /* can't happen, toast_action would be 'p' */
                        if (att[i]->attstorage != 'x' && att[i]->attstorage != 'e')
                                continue;
                {
                        if (toast_action[i] != ' ')
                                continue;
-                       if (VARATT_IS_EXTERNAL(toast_values[i]))
+                       if (VARATT_IS_EXTERNAL(DatumGetPointer(toast_values[i])))
                                continue;               /* can't happen, toast_action would be 'p' */
-                       if (VARATT_IS_COMPRESSED(toast_values[i]))
+                       if (VARATT_IS_COMPRESSED(DatumGetPointer(toast_values[i])))
                                continue;
                        if (att[i]->attstorage != 'm')
                                continue;
                                pfree(DatumGetPointer(old_value));
                        toast_values[i] = new_value;
                        toast_free[i] = true;
-                       toast_sizes[i] = VARSIZE(toast_values[i]);
+                       toast_sizes[i] = VARSIZE(DatumGetPointer(toast_values[i]));
                        need_change = true;
                        need_free = true;
                }
                {
                        if (toast_action[i] == 'p')
                                continue;
-                       if (VARATT_IS_EXTERNAL(toast_values[i]))
+                       if (VARATT_IS_EXTERNAL(DatumGetPointer(toast_values[i])))
                                continue;               /* can't happen, toast_action would be 'p' */
                        if (att[i]->attstorage != 'm')
                                continue;
 toast_compress_datum(Datum value)
 {
        struct varlena *tmp;
-       int32           valsize = VARSIZE_ANY_EXHDR(value);
+       int32           valsize = VARSIZE_ANY_EXHDR(DatumGetPointer(value));
 
-       Assert(!VARATT_IS_EXTERNAL(value));
-       Assert(!VARATT_IS_COMPRESSED(value));
+       Assert(!VARATT_IS_EXTERNAL(DatumGetPointer(value)));
+       Assert(!VARATT_IS_COMPRESSED(DatumGetPointer(value)));
 
        /*
         * No point in wasting a palloc cycle if value size is out of the
         * header byte and no padding if the value is short enough.  So we insist
         * on a savings of more than 2 bytes to ensure we have a gain.
         */
-       if (pglz_compress(VARDATA_ANY(value), valsize,
+       if (pglz_compress(VARDATA_ANY(DatumGetPointer(value)), valsize,
                                          (PGLZ_Header *) tmp, PGLZ_strategy_default) &&
                VARSIZE(tmp) < valsize - 2)
        {
        int32           chunk_seq = 0;
        char       *data_p;
        int32           data_todo;
+       Pointer         dval = DatumGetPointer(value);
 
        /*
         * Open the toast relation and its index.  We can use the index to check
         *
         * va_extsize is the actual size of the data payload in the toast records.
         */
-       if (VARATT_IS_SHORT(value))
+       if (VARATT_IS_SHORT(dval))
        {
-               data_p = VARDATA_SHORT(value);
-               data_todo = VARSIZE_SHORT(value) - VARHDRSZ_SHORT;
+               data_p = VARDATA_SHORT(dval);
+               data_todo = VARSIZE_SHORT(dval) - VARHDRSZ_SHORT;
                toast_pointer.va_rawsize = data_todo + VARHDRSZ;                /* as if not short */
                toast_pointer.va_extsize = data_todo;
        }
-       else if (VARATT_IS_COMPRESSED(value))
+       else if (VARATT_IS_COMPRESSED(dval))
        {
-               data_p = VARDATA(value);
-               data_todo = VARSIZE(value) - VARHDRSZ;
+               data_p = VARDATA(dval);
+               data_todo = VARSIZE(dval) - VARHDRSZ;
                /* rawsize in a compressed datum is just the size of the payload */
-               toast_pointer.va_rawsize = VARRAWSIZE_4B_C(value) + VARHDRSZ;
+               toast_pointer.va_rawsize = VARRAWSIZE_4B_C(dval) + VARHDRSZ;
                toast_pointer.va_extsize = data_todo;
                /* Assert that the numbers look like it's compressed */
                Assert(VARATT_EXTERNAL_IS_COMPRESSED(toast_pointer));
        }
        else
        {
-               data_p = VARDATA(value);
-               data_todo = VARSIZE(value) - VARHDRSZ;
-               toast_pointer.va_rawsize = VARSIZE(value);
+               data_p = VARDATA(dval);
+               data_todo = VARSIZE(dval) - VARHDRSZ;
+               toast_pointer.va_rawsize = VARSIZE(dval);
                toast_pointer.va_extsize = data_todo;
        }
 
 
                        break;
        }
 
-       return (Datum) returnValue;
+       return PointerGetDatum(returnValue);
 }
 
 
                                        flinfo.fn_oid, n_arguments, FUNC_MAX_ARGS)));
                va_start(pvar, procedureId);
                for (i = 0; i < n_arguments; i++)
-                       fcinfo.arg[i] = (Datum) va_arg(pvar, char *);
+                       fcinfo.arg[i] = PointerGetDatum(va_arg(pvar, char *));
                va_end(pvar);
        }
 
        if (fcinfo.isnull)
                elog(ERROR, "function %u returned NULL", flinfo.fn_oid);
 
-       return (char *) result;
+       return DatumGetPointer(result);
 }
 
 
 
                         * sure it is labeled with the caller-supplied tuple type.
                         */
                        estate.retval =
-                               PointerGetDatum(SPI_returntuple((HeapTuple) (estate.retval),
+                               PointerGetDatum(SPI_returntuple((HeapTuple)DatumGetPointer(estate.retval),
                                                                                                tupdesc));
                }
                else
                                        (errcode(ERRCODE_DATATYPE_MISMATCH),
                                         errmsg("returned tuple structure does not match table of trigger event")));
                /* Copy tuple to upper executor memory */
-               rettup = SPI_copytuple((HeapTuple) (estate.retval));
+               rettup = SPI_copytuple((HeapTuple) DatumGetPointer(estate.retval));
        }
 
        /*
 
                                        if (HeapTupleIsValid(rec->tup))
                                        {
-                                               estate->retval = (Datum) rec->tup;
+                                               estate->retval = PointerGetDatum(rec->tup);
                                                estate->rettupdesc = rec->tupdesc;
                                                estate->retisnull = false;
                                        }
                                        PLpgSQL_row *row = (PLpgSQL_row *) retvar;
 
                                        Assert(row->rowtupdesc);
-                                       estate->retval = (Datum) make_tuple_from_row(estate, row,
-                                                                                                                       row->rowtupdesc);
-                                       if (estate->retval == (Datum) NULL) /* should not happen */
+                                       estate->retval = 
+                                               PointerGetDatum(make_tuple_from_row(estate, row,
+                                                                                                                       row->rowtupdesc));
+                                       if (DatumGetPointer(estate->retval) == NULL) /* should not happen */
                                                elog(ERROR, "row not compatible with its own tupdesc");
                                        estate->rettupdesc = row->rowtupdesc;
                                        estate->retisnull = false;
                        exec_run_select(estate, stmt->expr, 1, NULL);
                        if (estate->eval_processed > 0)
                        {
-                               estate->retval = (Datum) estate->eval_tuptable->vals[0];
+                               estate->retval = PointerGetDatum(estate->eval_tuptable->vals[0]);
                                estate->rettupdesc = estate->eval_tuptable->tupdesc;
                                estate->retisnull = false;
                        }
        PLpgSQL_var *var;
 
        var = (PLpgSQL_var *) (estate->datums[estate->found_varno]);
-       var->value = (Datum) state;
+       var->value = PointerGetDatum(state);
        var->isnull = false;
 }