#include "postgres.h" #include "fmgr.h" #ifdef PG_MODULE_MAGIC PG_MODULE_MAGIC; #endif Datum rownum(PG_FUNCTION_ARGS); PG_FUNCTION_INFO_V1(rownum); Datum rownum(PG_FUNCTION_ARGS) { int32 *ptr; ptr = (int32 *) fcinfo->flinfo->fn_extra; if (ptr == NULL) { /* First time through for the current query: allocate storage */ fcinfo->flinfo->fn_extra = MemoryContextAlloc(fcinfo->flinfo->fn_mcxt, sizeof(int32)); ptr = (int32 *) fcinfo->flinfo->fn_extra; /* ... and initialize counter */ *ptr = 1; } else (*ptr)++; PG_RETURN_INT32(*ptr); }