#include <postgres.h>
#include <fmgr.h>
#include <funcapi.h>
#include <utils/elog.h>
#include <commands/trigger.h>

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif


void _PG_init()
{
}

PG_FUNCTION_INFO_V1(report_update_fields);

Datum
report_update_fields(PG_FUNCTION_ARGS)
{
	TriggerData *trigdata = (TriggerData *) fcinfo->context;
	int			tgnargs,
				ret;
	char	   *rgids,
			   *gtname,
			   *nspname,
			   *rname;
	HeapTuple	rettuple = NULL;
	List	   *rgidlist = NIL;
	ListCell   *lc;

	if (!CALLED_AS_TRIGGER(fcinfo) || /*!TRIGGER_FIRED_AFTER(trigdata->tg_event) || */ TRIGGER_FIRED_FOR_STATEMENT(trigdata->tg_event) || !TRIGGER_FIRED_BY_UPDATE(trigdata->tg_event))
		elog(ERROR, "usage unsupported");
	
	if ((trigdata->tg_trigtuple != NULL) && (trigdata->tg_newtuple != NULL))
	{
        int attnum = -1;
        int num_upd_attrs = 0;
        
        while ((attnum = bms_next_member(trigdata->tg_updatedcols, attnum)) >= 0)
            if (attnum + FirstLowInvalidHeapAttributeNumber > 0)
                num_upd_attrs++;
                
        elog(INFO, "updated %d attrs", num_upd_attrs);
    }
}

